0
0
PyTesttesting~20 mins

Custom markers in PyTest - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Custom Marker 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 marker skip
What will be the output when running this pytest code with the custom marker @pytest.mark.skip(reason="Not ready") applied to test_feature()?
PyTest
import pytest

@pytest.mark.skip(reason="Not ready")
def test_feature():
    assert 1 == 1

def test_other():
    assert 2 == 2
ABoth tests run and pass; overall result: 2 passed
Btest_feature raises an error; test_other passes; overall result: 1 passed, 1 error
Ctest_feature fails due to skip marker; test_other passes; overall result: 1 passed, 1 failed
Dtest_feature is skipped; test_other passes; overall result: 1 passed, 1 skipped
Attempts:
2 left
💡 Hint
The skip marker prevents the test from running but counts as skipped, not failed.
assertion
intermediate
2:00remaining
Correct assertion for custom marker usage
Which assertion correctly verifies that a test function is marked with a custom marker named slow in pytest?
PyTest
import pytest

def test_example():
    pass

# Assume test_example is marked with @pytest.mark.slow
Aassert test_example.has_marker('slow')
Bassert any(marker.name == 'slow' for marker in test_example.pytestmark)
Cassert 'slow' in test_example.pytestmark
Dassert test_example.marker == 'slow'
Attempts:
2 left
💡 Hint
pytest stores markers as objects with a name attribute in the pytestmark list.
🔧 Debug
advanced
2:00remaining
Debugging marker registration error
Given this pytest configuration snippet, what is the cause of the error when trying to use the custom marker integration?
PyTest
# content of pytest.ini
[pytest]
markers =
    integration: mark test as integration test

# test_sample.py
import pytest

@pytest.mark.integration
def test_api():
    assert True
AIndentation error before test_api function definition causes syntax error
BMarker 'integration' is not registered in pytest.ini
CMissing import of integration marker causes NameError
Dpytest.ini file is ignored unless placed in root directory
Attempts:
2 left
💡 Hint
Check the indentation of the test function definition line.
framework
advanced
2:00remaining
Using custom markers to select tests
Which pytest command line option runs only tests marked with the custom marker database?
Apytest -m database
Bpytest --run-database
Cpytest --markers=database
Dpytest -k database
Attempts:
2 left
💡 Hint
The -m option filters tests by marker expression.
🧠 Conceptual
expert
2:00remaining
Best practice for custom marker documentation
Why is it important to declare custom markers in the pytest.ini file under the markers section?
ATo allow markers to be used only in test classes but not in functions
BTo register markers as built-in pytest markers for performance optimization
CTo avoid pytest warnings about unknown markers and provide documentation for users
DTo enable pytest to automatically skip tests with those markers
Attempts:
2 left
💡 Hint
pytest warns if it encounters markers not declared in configuration.