Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclude tests in pytest configuration file

Tags:

python

pytest

I would like to be able to exclude instead of include certain python test files in the pytest.ini configuration file. According to the docs including tests boils down to something like this:

# content of pytest.ini
[pytest]
pytest_files=test_main.py test_common.py

To exclude files however, only command line options are suggested:

--ignore=test_common.py

How can I actually select files to ignore at the pytest.ini file level?

like image 561
user32882 Avatar asked Aug 24 '20 14:08

user32882


People also ask

How do I ignore a test in pytest?

The simplest way to skip a test function is to mark it with the skip decorator which may be passed an optional reason : @pytest. mark.

Where is pytest config file?

Many pytest settings can be set in a configuration file, which by convention resides on the root of your repository or in your tests folder.

How do I run a specific file in pytest?

Running pytest We can run a specific test file by giving its name as an argument. A specific function can be run by providing its name after the :: characters. Markers can be used to group tests. A marked grouped of tests is then run with pytest -m .

Where is the pytest INI file?

Users can customize some pytest behavior using a configuration file called pytest. ini . This file is usually placed at the root of the repository and contains a number of configuration values that are applied to all test runs for that project.


1 Answers

You can add any command line options in pytest.ini under addopts. In your case this should work:

pytest.ini

[pytest]
addopts = --ignore=test_common.py

As has been noted in the comments, --ignore takes a path (relative or absolute), not just a module name. From the output of pytest -h:

  --ignore=path         ignore path during collection (multi-allowed).
  --ignore-glob=path    ignore path pattern during collection (multi-allowed).
like image 165
MrBean Bremen Avatar answered Sep 23 '22 14:09

MrBean Bremen