Bird
Raised Fist0
PyTesttesting~20 mins

pyproject.toml configuration in PyTest - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Pytest Configuration Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of pytest with custom testpaths in pyproject.toml
Given this pyproject.toml configuration, what will pytest discover and run?
PyTest
[tool.pytest.ini_options]
testpaths = ["tests/integration", "tests/unit"]
APytest runs tests in all folders including 'tests/integration' and 'tests/unit'.
BPytest runs tests in all folders except 'tests/integration' and 'tests/unit'.
CPytest runs tests only in the root folder, ignoring subfolders.
DPytest runs tests only in 'tests/integration' and 'tests/unit' folders.
Attempts:
2 left
💡 Hint
The testpaths option limits where pytest looks for tests.
❓ assertion
intermediate
2:00remaining
Correct assertion for pytest capturing output
You want to check that your function prints 'Hello World' when run. Which pytest assertion correctly tests this using capsys?
PyTest
def greet():
    print("Hello World")

def test_greet(capsys):
    greet()
    captured = capsys.readouterr()
    # Which assertion below is correct?
Aassert captured.err == "Hello World\n"
Bassert captured.out == "Hello World"
Cassert captured.out == "Hello World\n"
Dassert captured.out.contains("Hello World")
Attempts:
2 left
💡 Hint
Remember print adds a newline character at the end.
❓ locator
advanced
2:00remaining
Identify the correct pytest marker configuration in pyproject.toml
Which pyproject.toml snippet correctly registers a custom marker named 'slow' with a description?
A
[tool.pytest.ini_options]
markers = "slow: marks tests as slow to run"
B
[tool.pytest.ini_options]
markers = ["slow: marks tests as slow to run"]
C
[tool.pytest.ini_options]
markers = {slow = "marks tests as slow to run"}
D
[tool.pytest.ini_options]
markers = slow: marks tests as slow to run
Attempts:
2 left
💡 Hint
Markers must be a list of strings with 'name: description' format.
🔧 Debug
advanced
2:00remaining
Why does pytest ignore tests in a subfolder despite pyproject.toml settings?
You set testpaths = ["tests"] in pyproject.toml, but pytest does not find tests in tests/api. What is the most likely cause?
AThe test files in <code>tests/api</code> do not start with 'test_' or end with '_test.py'.
BThe <code>pyproject.toml</code> file is not in the project root directory.
CThe <code>tests/api</code> folder lacks an __init__.py file, so pytest skips it.
DPytest requires explicit inclusion of subfolders in <code>testpaths</code>.
Attempts:
2 left
💡 Hint
Pytest discovers tests by file name patterns by default.
❓ framework
expert
2:00remaining
Effect of adding 'addopts' in pyproject.toml on pytest runs
Given this pyproject.toml snippet, what is the effect of the addopts option when running pytest?
PyTest
[tool.pytest.ini_options]
addopts = "-v --maxfail=2"
APytest runs in verbose mode and stops after 2 test failures.
BPytest runs with 2 workers in parallel and verbose output.
CPytest runs with verbose output and retries failed tests twice.
DPytest runs with maximum 2 tests executed and verbose output.
Attempts:
2 left
💡 Hint
The --maxfail option limits how many failures stop the test run.

Practice

(1/5)
1. What is the main purpose of the pyproject.toml file in pytest?
easy
A. To store pytest configuration settings in one place
B. To write test functions directly
C. To install pytest dependencies
D. To generate test reports automatically

Solution

  1. Step 1: Understand the role of pyproject.toml

    This file is used to keep configuration settings for pytest centralized.
  2. Step 2: Differentiate from other files

    It does not contain test code or install dependencies but configures how pytest runs.
  3. Final Answer:

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

    Configuration file [OK]
Hint: Remember: pyproject.toml holds settings, not test code [OK]
Common Mistakes:
  • Confusing configuration file with test code file
  • Thinking it installs packages
  • Assuming it generates reports automatically
2. Which of the following is the correct way to specify the test paths in pyproject.toml for pytest?
easy
A. [pytest] testpaths = "tests"
B. [tool.pytest] testpaths = ["tests"]
C. [tool.pytest] test_paths = ["tests"]
D. [tool.pytest] paths = "tests"

Solution

  1. Step 1: Identify correct section header

    pytest settings go under [tool.pytest] in pyproject.toml.
  2. Step 2: Check correct key and value format

    The key is testpaths and it expects a list of strings, e.g., ["tests"].
  3. Final Answer:

    [tool.pytest] testpaths = ["tests"] -> Option B
  4. Quick Check:

    Section and key names must be exact [OK]
Hint: Use [tool.pytest] and testpaths as a list [OK]
Common Mistakes:
  • Using wrong section header like [pytest]
  • Using wrong key names like test_paths
  • Assigning string instead of list for testpaths
3. Given this pyproject.toml snippet:
[tool.pytest]
markers = ["slow: marks tests as slow"]
addopts = "-v --maxfail=2"

What happens when you run pytest?
medium
A. Tests run verbosely but do not stop on failures; 'slow' marker ignored
B. Tests run silently and stop after 2 failures; 'slow' marker ignored
C. Tests run verbosely and stop after 2 failures; 'slow' marker is recognized
D. Tests run silently and continue on failures; 'slow' marker recognized

Solution

  1. Step 1: Understand addopts settings

    The -v flag makes output verbose; --maxfail=2 stops tests after 2 failures.
  2. Step 2: Understand markers setting

    Defining slow marker means pytest recognizes it and can filter or mark tests accordingly.
  3. Final Answer:

    Tests run verbosely and stop after 2 failures; 'slow' marker is recognized -> Option C
  4. Quick Check:

    Verbose + maxfail + marker recognized [OK]
Hint: addopts controls run options; markers define test tags [OK]
Common Mistakes:
  • Ignoring that -v means verbose output
  • Not knowing --maxfail stops after failures
  • Assuming markers are ignored without explicit use
4. You wrote this in pyproject.toml:
[tool.pytest]
markers = "slow: marks slow tests"
addopts = -v

What is the problem when running pytest?
medium
A. addopts ignored but markers work
B. No error; configuration works fine
C. Markers ignored but addopts works
D. Syntax error because markers should be a list, and addopts needs quotes

Solution

  1. Step 1: Check markers format

    Markers must be a list of strings, but here it is a single string without brackets.
  2. Step 2: Check addopts format

    addopts value must be a string in quotes; here it is unquoted, causing syntax error.
  3. Final Answer:

    Syntax error because markers should be a list, and addopts needs quotes -> Option D
  4. Quick Check:

    Lists need brackets; strings need quotes [OK]
Hint: Always use brackets for lists and quotes for strings in TOML [OK]
Common Mistakes:
  • Writing markers as a plain string instead of list
  • Forgetting quotes around string values
  • Assuming pytest ignores bad config silently
5. You want pytest to automatically run tests only in the integration_tests folder and recognize a custom marker api. Which pyproject.toml configuration is correct?
hard
A. [tool.pytest] testpaths = ["integration_tests"] markers = ["api: marks API tests"]
B. [tool.pytest] testpaths = "integration_tests" markers = "api: marks API tests"
C. [pytest] testpaths = ["integration_tests"] markers = ["api"]
D. [tool.pytest] paths = ["integration_tests"] markers = ["api: marks API tests"]

Solution

  1. Step 1: Set correct testpaths key and value

    Use testpaths as a list under [tool.pytest] to specify test folders.
  2. Step 2: Define markers properly

    Markers must be a list of strings describing each marker with a colon and description.
  3. Final Answer:

    [tool.pytest] testpaths = ["integration_tests"] markers = ["api: marks API tests"] -> Option A
  4. Quick Check:

    Correct keys, list format, and section [OK]
Hint: Use [tool.pytest], testpaths list, and markers list with descriptions [OK]
Common Mistakes:
  • Using string instead of list for testpaths
  • Wrong section header like [pytest]
  • Markers missing description or wrong format
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