Python 2.pytest 命令参数

Maple · 2020年12月24日 · 1114 次阅读

失败后停止

使用下面的参数可以让测试在第 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\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 的用例

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
暂无回复。
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册