Bird
Raised Fist0
PyTesttesting~20 mins

pytest.ini configuration - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
pytest.ini Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of pytest with custom markers in pytest.ini
Given the following pytest.ini configuration and test code, what will be the output when running pytest with the marker filter -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
AOnly <code>test_slow</code> runs and passes; <code>test_fast</code> is skipped.
BBoth <code>test_slow</code> and <code>test_fast</code> run and pass.
COnly <code>test_fast</code> runs and passes; <code>test_slow</code> is skipped.
DNo tests run because marker <code>slow</code> is not recognized.
Attempts:
2 left
💡 Hint
The -m option runs tests matching the given marker.
❓ assertion
intermediate
1:30remaining
Assertion behavior with pytest.ini addopts
If pytest.ini contains addopts = --maxfail=1 --tb=short, what will happen when two tests fail in a test suite?
APytest stops after the first failure but shows full tracebacks.
BPytest runs all tests and shows full tracebacks for failures.
CPytest runs all tests and shows short tracebacks.
DPytest stops after the first failure and shows a short traceback.
Attempts:
2 left
💡 Hint
The --maxfail option limits failures before stopping.
❓ locator
advanced
1:30remaining
Correct pytest.ini section for configuring test paths
Which section header is correct in pytest.ini to specify the directory where pytest should look for tests using testpaths?
A
[pytest]
testpaths = tests
B
[pytest.ini]
testpaths = tests
C
[config]
testpaths = tests
D
[tool:pytest]
testpaths = tests
Attempts:
2 left
💡 Hint
The official pytest configuration section is [pytest].
🔧 Debug
advanced
2:00remaining
Identify the error in pytest.ini configuration
Given this pytest.ini content, what error will pytest raise when running?
PyTest
[pytest]
markers =
    slow: marks tests as slow
    fast marks tests as fast
Apytest ignores the invalid marker and runs all tests normally.
Bpytest raises a ConfigError due to missing colon in marker declaration.
Cpytest raises a SyntaxError parsing pytest.ini.
Dpytest runs but warns about unknown marker 'fast'.
Attempts:
2 left
💡 Hint
Check the marker syntax carefully.
❓ framework
expert
2:30remaining
Effect of pytest.ini on test discovery and execution
Consider this pytest.ini: [pytest] testpaths = tests python_files = check_*.py addopts = -v --disable-warnings What is the combined effect of these settings when running pytest without arguments?
APytest looks only in the 'tests' folder for all test files, runs tests quietly, and suppresses warnings.
BPytest looks in all folders for files starting with 'check_' and ending with '.py', runs tests verbosely, and shows warnings.
CPytest looks only in the 'tests' folder for files starting with 'check_' and ending with '.py', runs tests verbosely, and suppresses warnings.
DPytest looks in all folders for all test files, runs tests quietly, and shows warnings.
Attempts:
2 left
💡 Hint
Check each pytest.ini option's effect on discovery and output.

Practice

(1/5)
1. What is the primary purpose of the pytest.ini file in a pytest project?
easy
A. To install pytest dependencies automatically
B. To store configuration settings for pytest test runs
C. To write test functions and test cases
D. To generate test reports after running tests

Solution

  1. Step 1: Understand pytest.ini role

    The pytest.ini file is used to configure pytest behavior before running tests.
  2. Step 2: Differentiate from other files

    Test functions are written in Python files, not in pytest.ini. Dependency installation and report generation are handled elsewhere.
  3. Final Answer:

    To store configuration settings for pytest test runs -> Option B
  4. Quick Check:

    pytest.ini = config file [OK]
Hint: pytest.ini holds settings, not test code or reports [OK]
Common Mistakes:
  • Confusing pytest.ini with test script files
  • Thinking pytest.ini installs packages
  • Assuming pytest.ini generates reports
2. Which of the following is the correct way to set the verbosity level to 2 in pytest.ini?
easy
A. [pytest] addopts = -vv
B. [pytest] verbosity = 2
C. [pytest] verbosity: 2
D. [pytest] addopts = --verbosity=2

Solution

  1. Step 1: Recall pytest verbosity setting

    pytest uses addopts to pass command-line options like -vv for verbosity level 2.
  2. Step 2: Check syntax correctness

    [pytest] addopts = -vv correctly uses addopts = -vv. [pytest] verbosity = 2 uses an invalid key verbosity. [pytest] verbosity: 2 uses wrong syntax with colon. [pytest] addopts = --verbosity=2 uses an invalid flag.
  3. Final Answer:

    [pytest] addopts = -vv -> Option A
  4. Quick Check:

    Verbosity level 2 via addopts = -vv [OK]
Hint: Use addopts with -vv for verbosity level 2 in pytest.ini [OK]
Common Mistakes:
  • Using 'verbosity' instead of 'addopts'
  • Incorrect syntax with colon instead of equals
  • Using unsupported flags like --verbosity
3. Given this pytest.ini content:
[pytest]
markers = slow: marks tests as slow
          smoke: marks smoke tests
addopts = -rs

What will be the effect of running pytest with this configuration?
medium
A. Tests marked as slow or smoke will be skipped automatically
B. pytest will run tests with no output
C. Only tests marked as slow will run
D. pytest will show extra summary info for skipped and failed tests

Solution

  1. Step 1: Understand markers and addopts

    The markers section defines labels for tests but does not skip them automatically. The addopts = -rs option tells pytest to show extra summary info for skipped and failed tests.
  2. Step 2: Analyze each option

    Tests marked as slow or smoke will be skipped automatically is wrong because markers alone don't skip tests. Only tests marked as slow will run is wrong because no selection is made. pytest will run tests with no output is wrong because -rs increases output detail.
  3. Final Answer:

    pytest will show extra summary info for skipped and failed tests -> Option D
  4. Quick Check:

    addopts = -rs shows skip/fail summary [OK]
Hint: addopts = -rs shows skip and fail summaries [OK]
Common Mistakes:
  • Thinking markers skip tests automatically
  • Confusing addopts flags with test selection
  • Assuming no output with -rs option
4. You added this to your pytest.ini file:
[pytest]
markers = regression
addopts = --maxfail=3

But when you run pytest, you get an error: ERROR: unknown option: --maxfail=3
What is the most likely cause?
medium
A. The addopts line should use -maxfail=3 instead
B. The pytest.ini file is missing the [pytest] header
C. The pytest version is outdated and does not support --maxfail
D. Markers must be defined after addopts in pytest.ini

Solution

  1. Step 1: Understand the error message

    The error says unknown option: --maxfail=3, meaning pytest does not recognize this option.
  2. Step 2: Check possible causes

    The addopts line should use -maxfail=3 instead is wrong because --maxfail requires two dashes. The pytest.ini file is missing the [pytest] header is wrong because the header is present. Markers must be defined after addopts in pytest.ini is wrong because order does not matter. The most common cause is an outdated pytest version that lacks support for --maxfail.
  3. Final Answer:

    The pytest version is outdated and does not support --maxfail -> Option C
  4. Quick Check:

    Unknown option usually means outdated pytest [OK]
Hint: Unknown option often means pytest version too old [OK]
Common Mistakes:
  • Changing option syntax incorrectly
  • Forgetting to include [pytest] header
  • Thinking order of keys matters in pytest.ini
5. You want to configure pytest.ini to:
1. Mark tests with slow and critical labels.
2. Run only tests matching the pattern test_*.py.
3. Stop after 2 failures.

Which of the following pytest.ini configurations achieves all three correctly?
hard
A. [pytest] markers = slow: slow tests critical: critical tests python_files = test_*.py addopts = --maxfail=2
B. [pytest] markers = slow critical python_files = test_*.py addopts = -x --maxfail=2
C. [pytest] markers = slow: slow tests critical: critical tests python_files = test_*.py addopts = -x --maxfail=2
D. [pytest] markers = slow: slow tests critical: critical tests python_files = test_*.py addopts = --maxfail=2 -x

Solution

  1. Step 1: Check markers syntax

    Options A, C, and D correctly define markers with descriptions. [pytest] markers = slow critical python_files = test_*.py addopts = -x --maxfail=2 lacks descriptions, which is allowed but less clear.
  2. Step 2: Verify test file pattern

    All options set python_files = test_*.py correctly.
  3. Step 3: Confirm failure stop and run options

    [pytest] markers = slow: slow tests critical: critical tests python_files = test_*.py addopts = --maxfail=2 uses addopts = --maxfail=2 which stops after 2 failures but does not stop immediately on first failure. Options C and D add -x which stops after first failure, conflicting with requirement to stop after 2 failures. [pytest] markers = slow critical python_files = test_*.py addopts = -x --maxfail=2 lacks descriptions in markers.
  4. Final Answer:

    Markers with descriptions, correct python_files, and stop after 2 failures -> Option A
  5. Quick Check:

    Use --maxfail=2 without -x to stop after 2 failures [OK]
Hint: Use --maxfail=2 without -x to stop after 2 failures [OK]
Common Mistakes:
  • Adding -x stops after 1 failure, not 2
  • Omitting marker descriptions
  • Misconfiguring python_files pattern
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