Bird
Raised Fist0
PyTesttesting~10 mins

Why configuration standardizes test behavior in PyTest - Test Your Understanding

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the pytest module correctly.

PyTest
import [1]
Drag options to blanks, or click blank then click option'
Apytest
Bunittest
Crequests
Dos
Attempts:
3 left
💡 Hint
Common Mistakes
Importing the wrong module like unittest or os.
2fill in blank
medium

Complete the code to define a pytest fixture that provides a fixed value.

PyTest
@pytest.fixture
def fixed_value():
    return [1]
Drag options to blanks, or click blank then click option'
A'random'
B42
CNone
DTrue
Attempts:
3 left
💡 Hint
Common Mistakes
Returning None or a string instead of a fixed number.
3fill in blank
hard

Fix the error in the test function to use the fixture correctly.

PyTest
def test_fixed(fixed_value):
    assert fixed_value [1] 42
Drag options to blanks, or click blank then click option'
A<
B!=
C==
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using != or comparison operators that cause test failure.
4fill in blank
hard

Fill both blanks to configure pytest to stop after first failure and show detailed info.

PyTest
def pytest_addoption(parser):
    parser.addoption('--maxfail', default=[1], help='Stop after max failures')
    parser.addoption('--tb', default=[2], help='Traceback style')
Drag options to blanks, or click blank then click option'
A1
B'short'
C'long'
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using maxfail=0 (no stop) or tb='short' (less info).
5fill in blank
hard

Fill all three blanks to create a pytest.ini file content that sets test paths, disables warnings, and sets log level.

PyTest
[pytest]
testpaths = [1]
filterwarnings = [2]
log_cli_level = [3]
Drag options to blanks, or click blank then click option'
Atests
Bignore::DeprecationWarning
CINFO
Dsrc
Attempts:
3 left
💡 Hint
Common Mistakes
Setting testpaths to 'src' or not filtering warnings.

Practice

(1/5)
1. Why is using a pytest.ini configuration file important for test behavior?
easy
A. It sets consistent options so tests run the same way every time.
B. It automatically fixes all test failures.
C. It replaces the need to write test functions.
D. It speeds up test execution by skipping all tests.

Solution

  1. Step 1: Understand the role of configuration files

    Configuration files like pytest.ini define settings that control how tests run, such as markers, test paths, or warnings.
  2. Step 2: Recognize the benefit of standardization

    By setting options in one place, all tests behave consistently across different runs and environments.
  3. Final Answer:

    It sets consistent options so tests run the same way every time. -> Option A
  4. Quick Check:

    Configuration standardizes test behavior = A [OK]
Hint: Config files keep test runs consistent everywhere [OK]
Common Mistakes:
  • Thinking config files fix test code errors automatically
  • Believing config files replace writing tests
  • Assuming config files skip tests to speed up runs
2. Which of the following is the correct way to specify a marker in pytest.ini to standardize test selection?
easy
A. [pytest] mark = slow: marks tests as slow
B. [pytest] markers slow: marks tests as slow
C. [pytest] markers = slow: marks tests as slow
D. [pytest] markers = slow marks tests as slow

Solution

  1. Step 1: Recall the correct syntax for markers in pytest.ini

    The correct section header is [pytest] and the key is markers with an equals sign and description.
  2. Step 2: Identify the properly formatted option

    [pytest] markers = slow: marks tests as slow uses markers = slow: marks tests as slow, which matches pytest documentation syntax.
  3. Final Answer:

    [pytest] markers = slow: marks tests as slow -> Option C
  4. Quick Check:

    Correct marker syntax = B [OK]
Hint: Markers use 'markers =' under [pytest] section [OK]
Common Mistakes:
  • Using 'mark' instead of 'markers'
  • Missing equals sign after 'markers'
  • Not enclosing section header in brackets
3. Given this pytest.ini content:
[pytest]
addopts = -v --maxfail=2

What will happen when you run pytest on a test suite with 5 failing tests?
medium
A. Pytest will ignore failures and pass all tests.
B. Pytest will run all tests and show minimal output.
C. Pytest will stop after 5 failures and show verbose output.
D. Pytest will stop after 2 failures and show verbose output.

Solution

  1. Step 1: Interpret the addopts options

    The option -v means verbose output. The option --maxfail=2 means pytest stops after 2 failures.
  2. Step 2: Predict test run behavior

    When running tests with 5 failures, pytest will stop after the second failure and show detailed output because of -v.
  3. Final Answer:

    Pytest will stop after 2 failures and show verbose output. -> Option D
  4. Quick Check:

    addopts '-v --maxfail=2' = stop at 2 fails + verbose [OK]
Hint: maxfail limits failures; -v shows details [OK]
Common Mistakes:
  • Thinking pytest runs all tests despite maxfail
  • Confusing verbose with minimal output
  • Assuming maxfail=2 means stop after 5 failures
4. You added filterwarnings = ignore::DeprecationWarning in pytest.ini but still see deprecation warnings during test runs. What is the likely cause?
medium
A. You need to add --disable-warnings to the command line instead.
B. The filterwarnings option is misspelled or misplaced in the config file.
C. Deprecation warnings cannot be ignored in pytest.
D. Pytest does not support filtering warnings via configuration.

Solution

  1. Step 1: Verify correct spelling and placement of filterwarnings

    The option must be spelled exactly as filterwarnings under the [pytest] section in pytest.ini.
  2. Step 2: Understand pytest's support for warning filters

    Pytest supports filtering warnings via config. If warnings still appear, likely the option is misspelled or in wrong section.
  3. Final Answer:

    The filterwarnings option is misspelled or misplaced in the config file. -> Option B
  4. Quick Check:

    Correct spelling and placement = D [OK]
Hint: Check spelling and section for config options [OK]
Common Mistakes:
  • Assuming pytest ignores config filters
  • Using wrong option names
  • Not placing options under [pytest] section
5. You want to ensure all tests marked @pytest.mark.slow are skipped by default unless explicitly included. Which pytest.ini configuration achieves this standardized behavior?
hard
A. [pytest] markers = slow: marks slow tests addopts = -m 'not slow'
B. [pytest] markers = slow: marks slow tests addopts = -m slow
C. [pytest] markers = slow: marks slow tests addopts = --skip slow
D. [pytest] markers = slow: marks slow tests addopts = --disable slow

Solution

  1. Step 1: Understand marker usage and addopts filtering

    To skip tests marked slow by default, use -m 'not slow' to run tests not marked slow.
  2. Step 2: Identify correct addopts syntax

    [pytest] markers = slow: marks slow tests addopts = -m 'not slow' uses addopts = -m 'not slow', which tells pytest to exclude slow tests unless overridden.
  3. Final Answer:

    [pytest] markers = slow: marks slow tests addopts = -m 'not slow' -> Option A
  4. Quick Check:

    Exclude slow tests by default = C [OK]
Hint: Use '-m not marker' to skip marked tests by default [OK]
Common Mistakes:
  • Using '-m slow' runs only slow tests, not skips them
  • Trying unsupported options like --skip or --disable
  • Not defining the marker in pytest.ini
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