我喜歡一般的「測試步驟」的想法。我認爲它是「增量」測試,它在功能測試場景恕我直言,是最有意義的。
這裏是一個不依賴於pytest的內部細節(除了官方鉤擴展)的實現:
import pytest
def pytest_runtest_makereport(item, call):
if "incremental" in item.keywords:
if call.excinfo is not None:
parent = item.parent
parent._previousfailed = item
def pytest_runtest_setup(item):
previousfailed = getattr(item.parent, "_previousfailed", None)
if previousfailed is not None:
pytest.xfail("previous test failed (%s)" %previousfailed.name)
如果你現在有一個「test_step.py」是這樣的:
import pytest
@pytest.mark.incremental
class TestUserHandling:
def test_login(self):
pass
def test_modification(self):
assert 0
def test_deletion(self):
pass
然後運行它看起來像這樣(使用-rx對xfail原因報告):
(1)[email protected]:~/p/pytest/doc/en/example/teststep$ py.test -rx
============================= test session starts ==============================
platform linux2 -- Python 2.7.3 -- pytest-2.3.0.dev17
plugins: xdist, bugzilla, cache, oejskit, cli, pep8, cov, timeout
collected 3 items
test_step.py .Fx
=================================== FAILURES ===================================
______________________ TestUserHandling.test_modification ______________________
self = <test_step.TestUserHandling instance at 0x1e0d9e0>
def test_modification(self):
> assert 0
E assert 0
test_step.py:8: AssertionError
=========================== short test summary info ============================
XFAIL test_step.py::TestUserHandling::()::test_deletion
reason: previous test failed (test_modification)
================ 1 failed, 1 passed, 1 xfailed in 0.02 seconds =================
我在這裏使用「xfail」,因爲跳過的原因是錯誤的環境或缺少依賴關係,錯誤的解釋器版本。
編輯:請注意,您的示例和我的示例都不能直接與分佈式測試一起使用。爲此,pytest-xdist插件需要增加一種方法來定義要發送到一個測試從屬設備的組/類,而不是通常將類的測試功能發送到不同從屬設備的當前模式。
難道你不能只將--maxfail標誌設置爲1?如果一次測試失敗,那麼py.test會結束。 – Nacht
@Nacht的想法是繼續,儘管他們中的一個出現故障測試等測試案例,但停在失敗的測試用例 –