pytest特性记录:仅运行上一次运行失败的测试用例

关键字:--lf(--last-failed)

pytest运行参数 --lf(--last-failed),即每次运行测试套件时只运行上一次失败的测试用例,该参数是为了方便调试测试用例,提高测试效率。若第一次运行该测试套件或者上次运行时测试用例全部通过,则本次运行所有的测试用例。举例如下:

# 第一次运行时用例3和4执行失败,使用了参数 --lf,则本次运行只执行运行失败的用例,即用例3、4
import pytest


class TestPytestFeatures(object):
    def test_case_01(self):
        assert 1 == 1

    def test_case_02(self):
        assert isinstance(["a", "b"], list)

    def test_case_03(self):
        assert 1 == 2

    def test_case_04(self):
        assert 1 == 4


if __name__ == "__main__":
    pytest.main(["--lf", "test_case_pytest_features.py"])

"""
运行结果
============================= test session starts =============================
platform win32 -- Python 3.8.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: E:\icp_capp\help
plugins: allure-pytest-2.9.43
collected 4 items / 2 deselected / 2 selected
run-last-failure: rerun previous 2 failures

test_case_pytest_features.py FF                                          [100%]

================================== FAILURES ===================================
_______________________ TestPytestFeatures.test_case_03 _______________________

self = <help.test_case_pytest_features.TestPytestFeatures object at 0x000002173A6ABCA0>

    def test_case_03(self):
>       assert 1 == 2
E       assert 1 == 2

test_case_pytest_features.py:19: AssertionError
_______________________ TestPytestFeatures.test_case_04 _______________________

self = <help.test_case_pytest_features.TestPytestFeatures object at 0x000002173A6AB640>

    def test_case_04(self):
>       assert 1 == 4
E       assert 1 == 4

test_case_pytest_features.py:22: AssertionError
=========================== short test summary info ===========================
FAILED test_case_pytest_features.py::TestPytestFeatures::test_case_03 - asser...
FAILED test_case_pytest_features.py::TestPytestFeatures::test_case_04 - asser...
======================= 2 failed, 2 deselected in 0.06s =======================

Process finished with exit code 0

"""
# 第一次运行该测试套件,即使使用了--lf参数,仍然会运行全部测试用例
import pytest


class TestPytestFeatures(object):
    def test_case_01(self):
        assert 1 == 1

    def test_case_02(self):
        assert isinstance(["a", "b"], list)

    def test_case_03(self):
        assert 1 == 1

    def test_case_04(self):
        assert 1 == 1


if __name__ == "__main__":
    pytest.main(["--lf", "test_case_pytest_features.py"])

"""
运行结果
============================= test session starts =============================
platform win32 -- Python 3.8.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: E:\icp_capp\help
plugins: allure-pytest-2.9.43
collected 4 items
run-last-failure: no previously failed tests, not deselecting items.

test_case_pytest_features.py ....                                        [100%]

============================== 4 passed in 0.03s ==============================


"""

上一篇:搭建DNS服务器


下一篇:allure‘ 不是内部或外部命令,也不是可运行的程序 或批处理文件’