3 Ways to Ignore Directory/Tests in Pytest
CLI
The easiest method to skip directories with tests you don’t need is by using Pytest’s --ignore
command-line option. This option lets you exclude specific directories during your tests.
pytest --ignore=tests_in_progress --ignore=tests_upcoming_modules
Using Configuration Files
Utilizing Pytest’s default configuration file, pytest.ini
, allow you to effortlessly specify the directories to ignore, as illustrated below:
[pytest]
testpaths =
tests
integration
addopts = -v
# Specifying the directory to ignore
norecursedirs = tests/in_progress tests/upcoming_modules
Using Markers (For Individual Tests)
To handle individual tests, you can apply markers like @pytest.mark.skip
or @pytest.mark.xfail
. The @pytest.mark.skip
decorator indicates skipping the test, while @pytest.mark.xfail
flags a test as expected to fail.
Best Practices for Ignoring Directories
Document Ignored Directories:
Clearly document why certain directories are ignored. Provide a detailed explanation for excluding specific directories from tests, offering valuable context for the team.
Maintain CI/CD Pipeline Consistency:
Ensure consistency in your CI/CD pipelines by employing a standard configuration for directories that are marked as ignored. This approach improves reliability and reproducibility in testing.
Consider Test Dependencies:
Be mindful of dependencies between tests in different directories. Ignoring a directory might have repercussions on tests in other directories. Ideally, tests should be unique and separated. If you need to share fixtures you can create a conftest.py
in that local directory and share it.
Regular Review and Updates:
As the project evolves, certain directories may regain relevance for testing. Regularly review and update the list of ignored directories to keep it aligned with the project’s changing needs.