微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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 ==============================


"""

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐