pythonunit-testingtensorflowpytest

How to mark one function not a test for pytest?


I'm using pytest to test some code based on TensorFlow.

A TestCase is defined for simplicity like:

class TestCase(tf.test.TestCase):
    # ...

The problem is tf.test.TestCase provide a useful function self.test_session(), which was treated as a test method in pytest since its name starts with test_.

The result pytest report more succeed tests than test methods I defined due to test_session() methods.

I use the following code to skip test_session:

class TestCase(tf.test.TestCase):
    @pytest.mark.skip
    @contextmanager
    def test_session(self):
        with super().test_session() as sess:
            yield sess

However there would be some "s" in test report indicating there are some skip tests.

Is there anyway I can mark one exact method not a test method without changing pytest test discovery rules globally?


Solution

  • Filter out false positives after the test items are collected: create a conftest.py in your tests directory with the custom post-collection hook:

    # conftest.py
    def pytest_collection_modifyitems(session, config, items):
        items[:] = [item for item in items if item.name != 'test_session']
    

    pytest will still collect the test_session methods (you will notice that in the pytest report line collected n tests), but not execute them as tests and not consider them anywhere in the test run.


    Related: fix for unittest-style tests

    Check out this answer.