Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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], …

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading