setup.cfg configuration in PyTest - Build an Automation Script
[pytest]
testpaths = tests
markers =
smoke: mark a test as smoke test
# test_sample.py file content
def test_example():
assert 1 + 1 == 2
# Command to run in terminal:
# pytest
# Expected output includes:
# - test discovery in 'tests' folder
# - test_example passes
The setup.cfg file is configured with a [pytest] section. It sets testpaths = tests so pytest looks for tests inside the tests folder. It also defines a custom marker smoke for demonstration.
The test_sample.py file contains a simple test function test_example that asserts 1 + 1 equals 2.
Running pytest in the terminal will use the setup.cfg configuration automatically. Pytest will discover tests in the tests directory and run test_example. The test should pass, confirming the configuration works.
This approach avoids passing command line options every time and centralizes test configuration.
Now add a custom marker 'smoke' in setup.cfg and write a test that uses this marker. Run pytest to verify the marker is recognized.
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
