What if you never had to remember long test commands again?
Why setup.cfg configuration in PyTest? - Purpose & Use Cases
Imagine you have many test settings scattered across different files or commands. Every time you run tests, you must remember and type long options manually.
This manual way is slow and easy to forget. You might mistype options or miss important settings, causing tests to behave inconsistently or fail unexpectedly.
Using setup.cfg lets you store all your pytest settings in one place. This file automatically applies your preferences every time you run tests, making your work faster and error-free.
pytest --maxfail=3 --disable-warnings --verbose[pytest]
maxfail = 3
disable_warnings = true
addopts = --verboseYou can run tests confidently without typing options each time, ensuring consistent and reliable test runs.
A developer working on a big project configures setup.cfg once. Now, every team member runs tests with the same settings, avoiding confusion and saving time.
Centralizes test settings for easy management.
Prevents mistakes from manual command typing.
Saves time by automating test configuration.
Practice
setup.cfg file in pytest?Solution
Step 1: Understand pytest configuration
Thesetup.cfgfile is used to keep pytest settings centralized.Step 2: Differentiate from other tasks
Writing tests or installing packages are not roles ofsetup.cfg.Final Answer:
To store pytest configuration settings in one place -> Option AQuick Check:
setup.cfg centralizes pytest settings [OK]
- Confusing setup.cfg with test files
- Thinking setup.cfg runs tests
- Assuming setup.cfg installs packages
setup.cfg?Solution
Step 1: Identify correct section and key
pytest settings go under[pytest]andpython_filessets test file patterns.Step 2: Check pattern correctness
python_files = test_*.pymatches typical test files.Final Answer:
[pytest]\npython_files = test_*.py -> Option BQuick Check:
python_files sets test file pattern [OK]
- Using wrong section header like [tool:pytest]
- Confusing markers with file patterns
- Setting testpaths instead of python_files for file pattern
setup.cfg snippet:[pytest] markers = slow: marks tests as slow python_files = test_*.py
What happens when you run
pytest -m slow?Solution
Step 1: Understand marker definition
The markerslowis defined to label slow tests.Step 2: Effect of
Running-m slowoptionpytest -m slowruns only tests marked with@pytest.mark.slow.Final Answer:
Only tests marked with @pytest.mark.slow run -> Option DQuick Check:
-m runs tests with matching marker [OK]
- Thinking -m filters by test name
- Assuming all tests run ignoring markers
- Confusing file pattern with marker filtering
setup.cfg:[pytest] markers = slow: marks slow tests python_files = test_*.py python_classes = Test* python_functions = test_*
But pytest gives a warning about unknown markers. What is the likely cause?
Solution
Step 1: Check marker registration format
Markers must be registered exactly with correct spelling and description.Step 2: Understand warning cause
Warnings usually occur if marker name is misspelled or description is missing.Final Answer:
Markers need to be registered with full description and no typos -> Option AQuick Check:
Correct marker registration avoids warnings [OK]
- Placing markers under wrong section
- Forgetting marker description after colon
- Assuming python_files affects markers
legacy_tests folder without typing command line options. How do you configure setup.cfg to do this?Solution
Step 1: Identify option to ignore folders
The--ignore=folderoption tells pytest to skip that folder.Step 2: Set addopts in setup.cfg
Addingaddopts = --ignore=legacy_testsruns pytest with this option automatically.Final Answer:
[pytest]\naddopts = --ignore=legacy_tests -> Option CQuick Check:
addopts sets default command line options [OK]
- Using ignore key instead of addopts
- Confusing markers with ignore settings
- Using filterwarnings incorrectly for ignoring tests
Ready to prove it?
Pass the test and this pattern moves unknown
