关键字:-x(--exitfirst)
pytest参数 -x(--exitfirst),即在测试用例调试运行过程中,遇到用例失败,则停止运行,方便我们调试脚本,对于大型项目而言,可以节省很多时间。举例如下,共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 == 4
def test_case_04(self):
assert 1 == 1
if __name__ == "__main__":
pytest.main(["-x", "test_case_pytest_features.py"])
"""
运行结果
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
test_case_pytest_features.py ..F
================================== FAILURES ===================================
_______________________ TestPytestFeatures.test_case_03 _______________________
self = <help.test_case_pytest_features.TestPytestFeatures object at 0x000001E15F9307F0>
def test_case_03(self):
> assert 1 == 4
E assert 1 == 4
test_case_pytest_features.py:19: AssertionError
=========================== short test summary info ===========================
FAILED test_case_pytest_features.py::TestPytestFeatures::test_case_03 - asser...
!!!!!!!!!!!!!!!!!!!!!!!!!! stopping after 1 failures !!!!!!!!!!!!!!!!!!!!!!!!!!
========================= 1 failed, 2 passed in 0.09s =========================
Process finished with exit code 0
"""