Bird
Raised Fist0
PyTesttesting~20 mins

setup.cfg configuration in PyTest - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
setup.cfg pytest Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of pytest with setup.cfg markers
Given the following setup.cfg configuration and test file, what will be the output when running pytest without any additional options?
PyTest
[pytest]
markers =
    slow: marks tests as slow

# test_sample.py
import pytest

@pytest.mark.slow
def test_slow():
    assert True

def test_fast():
    assert True
ABoth tests are skipped.
BOnly test_fast runs and passes; test_slow is skipped.
CBoth tests run and pass.
Dpytest raises an error due to unknown marker.
Attempts:
2 left
💡 Hint
Markers declared in setup.cfg do not skip tests by default.
❓ assertion
intermediate
2:00remaining
Correct assertion for test selection by keyword
With this setup.cfg snippet, which assertion correctly verifies that only tests marked with 'slow' run when using pytest -m slow?
PyTest
[pytest]
markers =
    slow: marks tests as slow

# test_sample.py
import pytest

@pytest.mark.slow
def test_slow():
    assert True

def test_fast():
    assert True
Aassert 'skipped' in result.stdout
Bassert 'test_fast' in result.stdout and 'test_slow' not in result.stdout
Cassert 'collected 2 items' in result.stdout
Dassert 'test_slow' in result.stdout and 'test_fast' not in result.stdout
Attempts:
2 left
💡 Hint
Using -m slow runs only tests with the slow marker.
🔧 Debug
advanced
2:00remaining
Why does pytest warn about unknown marker despite setup.cfg?
You have this setup.cfg: [pytest] markers = regression: marks regression tests But running tests marked with @pytest.mark.regression still shows a warning about unknown marker. What is the most likely cause?
AThe <code>setup.cfg</code> file is not in the root directory where pytest is run.
BThe marker name in the test code is misspelled as 'regresion'.
Cpytest requires markers to be declared in <code>pytest.ini</code>, not <code>setup.cfg</code>.
DThe test function is missing the <code>@pytest.mark.usefixtures</code> decorator.
Attempts:
2 left
💡 Hint
pytest reads setup.cfg only if it is in the current directory or above.
❓ framework
advanced
2:00remaining
Configuring pytest to ignore warnings via setup.cfg
Which setup.cfg snippet correctly configures pytest to ignore DeprecationWarning during test runs?
A
[pytest]
filterwarnings = ignore::DeprecationWarning
B
[pytest]
ignore_warnings = DeprecationWarning
C
[pytest]
filterwarnings = error::DeprecationWarning
D
[pytest]
warnings_ignore = DeprecationWarning
Attempts:
2 left
💡 Hint
pytest uses filterwarnings option with ignore action.
🧠 Conceptual
expert
3:00remaining
Effect of testpaths in setup.cfg on test discovery
Given this setup.cfg: [pytest] testpaths = tests integration What is the effect of this configuration when running pytest without arguments?
Apytest will discover tests in all directories except 'tests' and 'integration'.
Bpytest will only discover tests inside the 'tests' and 'integration' directories.
Cpytest will ignore the 'tests' and 'integration' directories and discover tests elsewhere.
Dpytest will run tests only in the current directory, ignoring subdirectories.
Attempts:
2 left
💡 Hint
testpaths limits where pytest looks for tests.

Practice

(1/5)
1. What is the main purpose of the setup.cfg file in pytest?
easy
A. To store pytest configuration settings in one place
B. To write test functions
C. To install pytest package
D. To run tests manually from the command line

Solution

  1. Step 1: Understand pytest configuration

    The setup.cfg file is used to keep pytest settings centralized.
  2. Step 2: Differentiate from other tasks

    Writing tests or installing packages are not roles of setup.cfg.
  3. Final Answer:

    To store pytest configuration settings in one place -> Option A
  4. Quick Check:

    setup.cfg centralizes pytest settings [OK]
Hint: Remember setup.cfg holds config, not test code [OK]
Common Mistakes:
  • Confusing setup.cfg with test files
  • Thinking setup.cfg runs tests
  • Assuming setup.cfg installs packages
2. Which of the following is the correct way to specify the test file pattern in setup.cfg?
easy
A. [pytest]\ntestpaths = tests
B. [pytest]\npython_files = test_*.py
C. [tool:pytest]\npython_files = *.txt
D. [pytest]\nmarkers = test_*.py

Solution

  1. Step 1: Identify correct section and key

    pytest settings go under [pytest] and python_files sets test file patterns.
  2. Step 2: Check pattern correctness

    python_files = test_*.py matches typical test files.
  3. Final Answer:

    [pytest]\npython_files = test_*.py -> Option B
  4. Quick Check:

    python_files sets test file pattern [OK]
Hint: Use python_files to set test file patterns [OK]
Common Mistakes:
  • Using wrong section header like [tool:pytest]
  • Confusing markers with file patterns
  • Setting testpaths instead of python_files for file pattern
3. Given this setup.cfg snippet:
[pytest]
markers = slow: marks tests as slow
python_files = test_*.py

What happens when you run pytest -m slow?
medium
A. Tests in files not matching test_*.py run
B. All tests run ignoring markers
C. Tests with names containing 'slow' run
D. Only tests marked with @pytest.mark.slow run

Solution

  1. Step 1: Understand marker definition

    The marker slow is defined to label slow tests.
  2. Step 2: Effect of -m slow option

    Running pytest -m slow runs only tests marked with @pytest.mark.slow.
  3. Final Answer:

    Only tests marked with @pytest.mark.slow run -> Option D
  4. Quick Check:

    -m runs tests with matching marker [OK]
Hint: -m runs tests with matching marker only [OK]
Common Mistakes:
  • Thinking -m filters by test name
  • Assuming all tests run ignoring markers
  • Confusing file pattern with marker filtering
4. You added this to 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?
medium
A. Markers need to be registered with full description and no typos
B. Missing a blank line after [pytest] section
C. Markers must be listed under [tool:pytest] section
D. python_files setting conflicts with markers

Solution

  1. Step 1: Check marker registration format

    Markers must be registered exactly with correct spelling and description.
  2. Step 2: Understand warning cause

    Warnings usually occur if marker name is misspelled or description is missing.
  3. Final Answer:

    Markers need to be registered with full description and no typos -> Option A
  4. Quick Check:

    Correct marker registration avoids warnings [OK]
Hint: Check marker spelling and description carefully [OK]
Common Mistakes:
  • Placing markers under wrong section
  • Forgetting marker description after colon
  • Assuming python_files affects markers
5. You want pytest to automatically ignore tests in the legacy_tests folder without typing command line options. How do you configure setup.cfg to do this?
hard
A. [pytest] markers = ignore: skip legacy tests
B. [pytest] python_files = test_*.py ignore = legacy_tests
C. [pytest] addopts = --ignore=legacy_tests
D. [pytest] filterwarnings = ignore::legacy_tests

Solution

  1. Step 1: Identify option to ignore folders

    The --ignore=folder option tells pytest to skip that folder.
  2. Step 2: Set addopts in setup.cfg

    Adding addopts = --ignore=legacy_tests runs pytest with this option automatically.
  3. Final Answer:

    [pytest]\naddopts = --ignore=legacy_tests -> Option C
  4. Quick Check:

    addopts sets default command line options [OK]
Hint: Use addopts to add default command line options [OK]
Common Mistakes:
  • Using ignore key instead of addopts
  • Confusing markers with ignore settings
  • Using filterwarnings incorrectly for ignoring tests
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard