Skip to content

teardown method called even for skipped tests (and setup isn't) #265

@pytestbot

Description

@pytestbot

Originally reported by: Torsten Landschoff (BitBucket: bluehorn, GitHub: bluehorn)


I tried to skip all tests in a class when the specialized implementation is not available:

#!python

import pytest

class TestGeneric(object):
    def test_nothing(self):
        """Tests the API of the implementation (for generic and specialized)."""

@pytest.mark.skipif("True", reason="Skip tests to check if teardown is skipped as well.")
class TestSkipTeardown(TestGeneric):

    def setup(self):
        """Sets up my specialized implementation for $COOL_PLATFORM."""
        raise Exception("should not call setup for skipped tests")

    def teardown(self):
        """Undoes the setup."""
        raise Exception("should not call teardown for skipped tests")

The output is quite interesting:

#!python

(loco2-precise)torsten@sharokan:~/pytesttest$ py.test -rs -v
==================================================================================== test session starts ====================================================================================
platform linux2 -- Python 2.7.3 -- pytest-2.3.2 -- /opt/dynasdk/loco2-precise/bin/python
plugins: cov, capturelog, xdist
collected 2 items 

test_skip_teardown.py:4: TestGeneric.test_nothing PASSED
test_skip_teardown.py:4: TestSkipTeardown.test_nothing SKIPPED
test_skip_teardown.py:4: TestSkipTeardown.test_nothing ERROR

========================================================================================== ERRORS ===========================================================================================
____________________________________________________________________ ERROR at teardown of TestSkipTeardown.test_nothing _____________________________________________________________________

self = <test_skip_teardown.TestSkipTeardown object at 0x1ab9650>

    def teardown(self):
        """Undoes the setup."""
>       raise Exception("should not call teardown for skipped tests")
E       Exception: should not call teardown for skipped tests

test_skip_teardown.py:16: Exception
================================================================================== short test summary info ==================================================================================
SKIP [1] /opt/dynasdk/loco2-precise/lib/python2.7/site-packages/_pytest/skipping.py:120: Skip tests to check if teardown is skipped as well.
======================================================================= 1 passed, 1 skipped, 1 error in 0.02 seconds ========================================================================

Note that the same tests is reported as skipped and error at the same time. I would have expected that neither setup nor teardown is called for this case. I could understand that setup and teardown is called for each test and the test is skipped inside, but the current behaviour does not make sense IMHO.

My workaround was to use setup_method and teardown_method (ignoring the method parameter).

Greetings, Torsten


Metadata

Metadata

Assignees

No one assigned

    Labels

    type: bugproblem that needs to be addressed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions