2

How to skip teardown when assertion occured in 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 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

I would like to achieve this through conftest.py so that it won't effect the existing test cases.

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

But it still runs the teardown and also its getting Internal Error like below

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

  • You are on the right track with the custom `pytest_runtest_makereport` hook; check out the section [Making test result information available in fixtures](https://docs.pytest.org/en/latest/example/simple.html#making-test-result-information-available-in-fixtures). – hoefling Jun 17 '19 at 16:34
  • thats not helped – user10600681 Jun 18 '19 at 13:25
  • Why not? You record the test outcome in the hook and execute teardown in fixture depending on the recorded outcome. – hoefling Jun 18 '19 at 13:37
  • I need to skip the teardown ( coming from fixture ) when the test case fails – user10600681 Jun 18 '19 at 15:17

0 Answers0