Python 如何在pytest中跳过断言时的拆卸
在pytest中发生断言时如何跳过拆卸Python 如何在pytest中跳过断言时的拆卸,python,pytest,teardown,Python,Pytest,Teardown,在pytest中发生断言时如何跳过拆卸 import logging import pytest log = logging.getLogger(__name__) @pytest.fixture() def myfixture(request): self = request.node.cls setup(self) yield teardown() def setup(self): log.info("In setup") def tear
import logging
import pytest
log = logging.getLogger(__name__)
@pytest.fixture()
def myfixture(request):
self = request.node.cls
setup(self)
yield
teardown()
def setup(self):
log.info("In setup")
def teardown():
log.info("In teardown but I don't want to execute this after assertion")
class TestCase():
def test_case1(self, myfixture):
log.info("Running test_case1")
assert 3 == 7
我希望通过conftest.py实现这一点,这样它就不会影响现有的测试用例
conftest.py
import pytest
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
# execute all other hooks to obtain the report object
outcome = yield
rep = outcome.get_result()
if rep.when == "call" and rep.failed:
pytest.skip("skipping")
但是它仍然运行拆卸,并且它的内部错误如下所示
INTERNALERROR> raise Skipped(msg=msg, allow_module_level=allow_module_level)
INTERNALERROR> Skipped: skipping
----------------------------------------------------------------------------------- live log sessionfinish -----------------------------------------------------------------------------------
INFO - In teardown but I don't want to execute this after assertion
================================================================================ no tests ran in 0.19 seconds ================================================================================
使用定制的
pytest\u runtest\u makereport
hook,您已经走上了正确的轨道;看看这一部分。那没有帮助。为什么没有?您将测试结果记录在钩子中,并根据记录的结果在fixture中执行拆卸。当测试用例失败时,我需要跳过拆卸(来自fixture)。使用自定义pytest\u runtest\u makereport
hook,您处于正确的轨道上;看看这一部分。那没有帮助。为什么没有?您将测试结果记录在钩子中,并根据记录的结果在fixture中执行拆卸。当测试用例失败时,我需要跳过拆卸(来自fixture)