How to mark one function not a test for pytest?

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.