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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值