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

公众号关注:测试宝藏库,一起交流


失败后停止


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


pytest ‐x # 第一次测试失败后停止测试 
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	est_02.py FF
================================== FAILURES ===================================
_____________________________ TestDemo02.test_01 ______________________________
...此处省略报错具体原因
testcase	est_02.py:11: AssertionError
_____________________________ TestDemo02.test_02 ______________________________
...此处省略报错具体原因
testcase	est_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	est_02.py ..

======================= 2 passed, 3 deselected in 0.03s =======================


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


通过节点id来进行测试


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


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


pytest testcase/test_02.py::TestDemo02:test_01


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


可以从结果看出,执行了test_02.py内TestDemo02类下的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	est_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的用例



发表评论
留言与评论(共有 0 条评论) “”
   
验证码:

相关文章

推荐文章