Combining python_files and --ignore in pytest.ini

Viewed 109

I would like pytest to collect test functions and classes from all python files, except for python files that begin with a given prefix.

I would like pytest to not even load those files which I wish to ignore, while it is scanning for collection, since it seems to be loading and initializing module code while scanning source files for test functions.

I currently fail to accomplish that. When I try to combine these two competing elements in my pytest.ini file, pytest would still load all python files, bearing the side-effects of module initialization of the files which I would like to avoid being loaded.

Here is my pytest.ini:

[pytest]
addopts = --ignore-glob=notebook_*
python_files = *.py

(Flipping the order of the competing definitions does not seem to matter).

The files of the form notebook_*.py still get loaded, which I would like to avoid as they are only artefacts generated for the sake of git history from jupyter notebook files. I'd like them to be ignored by pytest alltogether, but it seems to load them anyway while collecting.

Can it be accomplished?

1 Answers

Looks like collapsing to one line having an exclusion criteria to its glob expression works for this particular case:

[pytest]
python_files = [!notebook_]*.py

However, I'm not aware that glob syntax allows excluding multiple patterns in the same expression, so this is probably not a general answer as to how to combine inclusion and exclusion patterns in the same run or config.

Related