0
0
PyTesttesting~20 mins

Marker registration in PyTest - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Marker Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Purpose of marker registration in pytest
Why is it important to register custom markers in pytest's pytest.ini or pyproject.toml file?
ATo avoid warnings about unknown markers during test runs and enable marker-based test selection.
BTo automatically generate test reports without additional plugins.
CTo speed up test execution by caching marker results.
DTo disable all tests that use custom markers by default.
Attempts:
2 left
💡 Hint
Think about what happens if pytest sees a marker it does not recognize.
Predict Output
intermediate
1:30remaining
Output of pytest when using unregistered marker
What output will pytest produce when running this test with an unregistered marker @pytest.mark.slow?
PyTest
import pytest

@pytest.mark.slow
def test_example():
    assert True
ASkips the test silently without any warning.
BFails the test immediately with a SyntaxError.
CRuns the test without any warning or message.
DRuns the test and shows a warning: 'PytestUnknownMarkWarning: Unknown pytest.mark.slow - is this a typo?'.
Attempts:
2 left
💡 Hint
Think about pytest's behavior when it sees a marker it does not know.
locator
advanced
1:30remaining
Correct location to register a custom marker
Where should you add the registration for a custom marker named integration to avoid warnings in pytest?
AIn the test function's docstring describing the test purpose.
BDirectly inside each test file as a comment above the test function.
CIn the <code>pytest.ini</code> file under the <code>[pytest]</code> section with <code>markers = integration: marks tests as integration tests</code>.
DIn the <code>setup.py</code> file as an entry point for pytest.
Attempts:
2 left
💡 Hint
Think about pytest configuration files that control test behavior globally.
assertion
advanced
2:00remaining
Assertion to verify marker presence on a test function
Given a test function decorated with @pytest.mark.api, which assertion correctly checks for the presence of the 'api' marker in a pytest hook?
PyTest
def pytest_collection_modifyitems(items):
    for item in items:
        # Which assertion is correct here?
        pass
Aassert 'api' in item.keywords
Bassert any(mark.name == 'api' for mark in item.iter_markers())
Cassert item.marker == 'api'
Dassert item.has_marker('api')
Attempts:
2 left
💡 Hint
Look at how pytest exposes markers on collected test items.
framework
expert
2:00remaining
Effect of missing marker registration on test selection
You have registered a custom marker db in pytest.ini. You run pytest -m db to select tests with this marker. What happens if you remove the marker registration but still run pytest -m db?
APytest runs only tests with the <code>db</code> marker but shows a warning about unknown marker.
BPytest runs all tests ignoring the <code>-m db</code> option silently.
CPytest raises an error and stops because the marker is unknown and cannot be used for selection.
DPytest skips all tests because no tests match the unregistered marker.
Attempts:
2 left
💡 Hint
Consider pytest's behavior when selecting tests by an unregistered marker.