Pytest --collect-only -q to ignore tests that skip

Viewed 383

I am working with pytest and I need to collect test cases. For this there exists the following command:

pytest --collect-only -q

I am only interested in the tests that would be actually executed, if I would perform for example:

pytest tests/my_tests

(and not the skipped ones). How can I avoid collecting the tests that would skip?

2 Answers

Invoke the mark filter on the command line:

pytest --collect-only -q -m "not skip"

This avoids having to create or modify a conftest.py file.

In conftest.py, override the pytest_itemcollected method to add some output, then use findstr/grep (depending on your OS) to filter the output for those lines:

def pytest_itemcollected(item):
    if len(list(item.iter_markers(name="skip"))) == 0:
        print("not skipped: " + item.name)

Then in your command line: "pytest --collect-only -q | findstr "not skipped:"

Related