0
0
PyTesttesting~20 mins

Why markers categorize and control tests in PyTest - Challenge Your Understanding

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
2:00remaining
Purpose of pytest markers

Why do testers use markers in pytest?

ATo group tests and selectively run them based on categories
BTo automatically fix bugs found during test runs
CTo convert tests into documentation pages
DTo speed up test execution by skipping all tests
Attempts:
2 left
💡 Hint

Think about how you might run only some tests instead of all.

Predict Output
intermediate
2:00remaining
Output of pytest marker usage

What will be the output when running pytest with the marker @pytest.mark.slow and using pytest -m slow?

PyTest
import pytest

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

def test_fast():
    assert True
ANo tests run because marker is invalid
BBoth <code>test_example</code> and <code>test_fast</code> run and pass
COnly <code>test_example</code> runs and passes; <code>test_fast</code> is skipped
DOnly <code>test_fast</code> runs and passes; <code>test_example</code> is skipped
Attempts:
2 left
💡 Hint

Using -m slow runs tests marked as slow only.

assertion
advanced
2:00remaining
Correct assertion for marker presence

Which assertion correctly checks if a test function has the marker database applied in pytest?

PyTest
import pytest

def test_sample():
    pass

# Assume test_sample is marked with @pytest.mark.database
Aassert test_sample.marker == 'database'
Bassert test_sample.has_marker('database')
Cassert 'database' in test_sample.pytestmark
Dassert any(mark.name == 'database' for mark in getattr(test_sample, 'pytestmark', []))
Attempts:
2 left
💡 Hint

Markers are stored in a list under pytestmark attribute.

🔧 Debug
advanced
2:00remaining
Debugging marker skip issue

A test marked with @pytest.mark.skipif(True, reason='skip') is still running. What is the likely cause?

PyTest
@pytest.mark.skipif(True, reason='skip')
def test_skip():
    assert True
AThe condition in <code>skipif</code> is always False
BThe test function name does not start with <code>test_</code>
CThe <code>skipif</code> marker is not imported from pytest
DThe marker is applied incorrectly; it should be <code>@pytest.skipif</code>
Attempts:
2 left
💡 Hint

Pytest only runs functions starting with test_.

framework
expert
2:00remaining
Combining markers for test control

How can you run pytest to execute tests marked as api but skip those also marked as slow?

Apytest -m "api and not slow"
Bpytest -m "api or slow"
Cpytest -m "slow and not api"
Dpytest -m "not api and slow"
Attempts:
2 left
💡 Hint

Use logical operators and, not in marker expressions.