0
0
PyTesttesting~20 mins

Running tests by marker (-m) in PyTest - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Marker Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of pytest run with marker filter
Given the following pytest test file, what tests will run when executing pytest -m "slow"?
PyTest
import pytest

@pytest.mark.slow
def test_a():
    assert True

@pytest.mark.fast
def test_b():
    assert True

def test_c():
    assert True
AAll tests run
Btest_a and test_b run
Ctest_b and test_c run
DOnly test_a runs
Attempts:
2 left
💡 Hint
The -m option runs tests matching the given marker expression.
assertion
intermediate
2:00remaining
Correct assertion for marker presence in pytest
Which assertion correctly checks that a test function is marked with the marker 'database' in pytest?
PyTest
import pytest

@pytest.mark.database
def test_query():
    pass
Aassert any(marker.name == 'database' for marker in test_query.pytestmark)
Bassert 'database' in test_query.__pytest_markers__
Cassert 'database' in test_query.pytestmark
Dassert test_query.has_marker('database')
Attempts:
2 left
💡 Hint
pytest stores markers as objects in the pytestmark attribute list.
🔧 Debug
advanced
2:00remaining
Why does pytest -m 'api and not slow' run no tests?
Given these tests:
import pytest

@pytest.mark.api
@pytest.mark.slow
def test_api_slow():
    pass

@pytest.mark.api
def test_api_fast():
    pass

@pytest.mark.slow
def test_slow():
    pass
Why does pytest -m "api and not slow" run no tests?
ABecause the expression excludes all tests marked both api and slow, but test_api_fast is only api so it should run
BBecause 'not' is not a valid operator in pytest marker expressions
CBecause pytest marker expressions require quotes around 'not'
DBecause test_api_fast is not marked slow, so it should run but does not due to syntax error
Attempts:
2 left
💡 Hint
Check pytest documentation for valid marker expression operators.
framework
advanced
2:00remaining
How to run tests marked 'ui' or 'integration' but not 'slow' in pytest
Which pytest command runs tests marked with 'ui' or 'integration' but excludes those marked 'slow'?
Apytest -m "(ui or integration) and not slow"
Bpytest -m "ui or integration and slow"
Cpytest -m "ui or integration and not slow"
Dpytest -m "ui or integration and not slow" --disable-warnings
Attempts:
2 left
💡 Hint
Remember operator precedence and valid operators in pytest marker expressions.
🧠 Conceptual
expert
2:00remaining
Understanding pytest marker expression evaluation order
In pytest marker expressions, which operator has higher precedence?
A'and' has higher precedence than 'or'
B'or' has higher precedence than 'and'
C'and' and 'or' have equal precedence and are evaluated left to right
DPrecedence depends on the pytest version and is unpredictable
Attempts:
2 left
💡 Hint
Think about how logical operators usually work in programming languages.