How can I ignore fixtures when I run Pytest –collect-only?

I am collecting test names and writing them to a text file with:

pytest --collect-only -quiet >list_of_tests.txt

One of my tests has a fixture that runs the test 67 times. When I collect my tests, I end up with 67 versions of this test appended with [reply_type0], [reply_type1], [reply_type2], …

Is there a way to collect tests while ignoring fixtures so I only get one test collected without the fixture appended to the end?

>Solution :

That is not really possible in pytest, you should filter the collected test items by adding a custom pytest plugin with a hook that modifies the items during the collection process.

Just create a conftest.py file in your project directory with this inside:

def pytest_collection_modifyitems(config, items):
    filtered_items = []

    for item in items:
        test_name = item.nodeid.split('[')[0]
        if test_name not in [filtered_item.nodeid for filtered_item in filtered_items]:
            filtered_items.append(item)

    items[:] = filtered_items

Leave a Reply