自动化测试框架pytest命令参数

失败后停止

使用下面的参数可以让测试在第1(N)次测试失败后停止:

  1. pytest ‐x # 第一次测试失败后停止测试

  2. pytest ‐‐maxfail=2 # 第2次测试失败后停止测试

修改文件如下

# filename:test_02.py
import pytest
 
class TestDemo02:
    def func(self, x):
        return x + 1
 
    # 修改成断言失败
    def test_01(self):
        assert self.func(3) == 14
 
    # 修改成断言失败
    def test_02(self):
        assert self.func(3) == 5
 
 
if __name__ == '__main__':
    pytest.main(['-s', '-x', 'test_02.py'])
 

运行后观察结果

test_02.py F
 
================================== FAILURES ===================================
_____________________________ TestDemo02.test_01 ______________________________
 
...此处省略报错具体原因
 
test_02.py:10: AssertionError
=========================== short test summary info ===========================
FAILED test_02.py::TestDemo02::test_01 - assert 4 == 14
!!!!!!!!!!!!!!!!!!!!!!!!!! stopping after 1 failures !!!!!!!!!!!!!!!!!!!!!!!!!!
============================== 1 failed in 0.09s ==============================
 
Process finished with exit code 0

可以看出,由于第一条报错,2条用例只执行了第1条,使用‐‐maxfail=2可以失败两次后停止,大家自己尝试下。

指定执行范围

通过test_01.py文件执行测试

pytest test_01.py

通过文件夹执行测试

pytest testcase

创建testcase目录,然后把test_02.py移入目录下

然后执行命令,可以发现只执行了test_02.py下的用例

============================= test session starts =============================
platform win32 -- Python 3.7.1, pytest-6.0.2, py-1.9.0, pluggy-0.13.1
rootdir: D:\study\auto-pytest
collected 3 items
testcase\test_02.py FF
================================== FAILURES ===================================
_____________________________ TestDemo02.test_01 ______________________________
...此处省略报错具体原因
testcase\test_02.py:11: AssertionError
_____________________________ TestDemo02.test_02 ______________________________
...此处省略报错具体原因
testcase\test_02.py:15: AssertionError
=========================== short test summary info ===========================
FAILED testcase/test_02.py::TestDemo02::test_01 - assert 4 == 14
FAILED testcase/test_02.py::TestDemo02::test_02 - assert 4 == 5
!!!!!!!!!!!!!!!!!!!!!!!!!! stopping after 2 failures !!!!!!!!!!!!!!!!!!!!!!!!!!
============================== 2 failed in 0.04s ==============================

通过关键字表达式来进行测试

这种方式会执行文件名,类名以及函数名与给定的字符串表达式相匹配的测试用例。

pytest ‐k "Demo02 and not 01"

上面的用例会执 行TestDemo02.test_02但是不会执行TestDemo02.test_01,就像这样,另外3个没执行

============================= test session starts =============================
platform win32 -- Python 3.7.1, pytest-6.0.2, py-1.9.0, pluggy-0.13.1
rootdir: D:\study\auto-pytest
collected 5 items / 3 deselected / 2 selected
 
testcase\test_02.py ..
 
======================= 2 passed, 3 deselected in 0.03s =======================

通过节点id来进行测试

参数化的类名、函数名和参数,用::分隔。

可以通过下面的方式运行模块中的指定的测试用例

pytest testcase/test_02.py::TestDemo02:test_01

可以从结果看出,执行了test_02.pyTestDemo02类下的test_01方法

============================= test session starts =============================
platform win32 -- Python 3.7.1, pytest-6.0.2, py-1.9.0, pluggy-0.13.1
rootdir: D:\study\auto-pytest
collected 1 item
testcase\test_02.py .
============================== 1 passed in 0.01s ==============================

通过标记来执行

pytest ‐m tag01

这种方式会运行所有通过装饰器 @pytest.mark.tag进行装饰的测试用例,所以我们来修改test_02.py文件

# filename:test_02.py
import pytest
 
class TestDemo02():
    def func(self, x):
        return x + 1
 
    @pytest.mark.tag01
    def test_01(self):
        assert self.func(3) == 4
	
    def test_02(self):
        assert self.func(3) == 4
 
    def test_03(self):
        assert self.func(3) == 4

修改后,运行得到结果,可以看出只执行了打上标记的用例,篇幅问题,结果就不贴上来了

也可以通过pytest -m "tag01 or tag02"来执行标记为tag01和tag02的用例

感谢每一个认真阅读我文章的人,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走:

 

 这些资料,对于【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴上万个测试工程师们走过最艰难的路程,希望也能帮助到你!有需要的小伙伴可以点击下方小卡片领取 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值