Bird
Raised Fist0
PyTesttesting~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of using markers in pytest?
easy
A. To write tests faster by auto-generating code
B. To change the Python version used for tests
C. To automatically fix failing tests
D. To group tests so you can run specific sets easily

Solution

  1. Step 1: Understand what markers do in pytest

    Markers are labels you add to tests to group or categorize them.
  2. Step 2: Identify the main use of markers

    Markers help you run only certain groups of tests, saving time and focusing effort.
  3. Final Answer:

    To group tests so you can run specific sets easily -> Option D
  4. Quick Check:

    Markers group tests = B [OK]
Hint: Markers help run selected tests quickly [OK]
Common Mistakes:
  • Thinking markers auto-fix tests
  • Confusing markers with code generation
  • Believing markers change Python version
2. Which of the following is the correct way to mark a test as 'slow' in pytest?
easy
A. @pytest.slow def test_example(): pass
B. @pytest.mark.slow def test_example(): pass
C. @slow_mark def test_example(): pass
D. @mark.slow def test_example(): pass

Solution

  1. Step 1: Recall pytest marker syntax

    Markers are used with @pytest.mark.<marker_name> before the test function.
  2. Step 2: Match the correct syntax

    Only '@pytest.mark.slow' is the correct way to mark a test as slow.
  3. Final Answer:

    @pytest.mark.slow\ndef test_example(): pass -> Option B
  4. Quick Check:

    Use @pytest.mark.marker_name = D [OK]
Hint: Use @pytest.mark.marker_name to mark tests [OK]
Common Mistakes:
  • Omitting 'pytest' before 'mark'
  • Using wrong decorator names
  • Confusing marker syntax with other decorators
3. Given the code below, what tests will run when executing pytest -m "slow and not network"?
@pytest.mark.slow
def test_a(): pass

@pytest.mark.network
def test_b(): pass

@pytest.mark.slow
@pytest.mark.network
def test_c(): pass

def test_d(): pass
medium
A. test_b and test_c
B. test_a and test_c
C. Only test_a
D. test_a, test_b, test_c, and test_d

Solution

  1. Step 1: Understand the marker expression

    The command runs tests marked 'slow' but NOT marked 'network'.
  2. Step 2: Check each test's markers

    test_a is 'slow' only (runs), test_b is 'network' only (excluded), test_c is both 'slow' and 'network' (excluded), test_d has no markers (excluded).
  3. Final Answer:

    Only test_a -> Option C
  4. Quick Check:

    Run tests with slow and not network = C [OK]
Hint: Use 'and not' to exclude markers in pytest -m [OK]
Common Mistakes:
  • Including tests with 'network' marker
  • Running unmarked tests by default
  • Misunderstanding boolean logic in marker expressions
4. What is wrong with this pytest marker usage?
@pytest.mark.skipif(sys.platform == 'win32')
def test_func():
    assert True
medium
A. Missing import of sys module
B. Incorrect marker name 'skipif'
C. Missing parentheses after skipif
D. Cannot use conditions in markers

Solution

  1. Step 1: Check the code for required imports

    The code uses 'sys.platform' but does not import 'sys'.
  2. Step 2: Verify marker syntax

    '@pytest.mark.skipif(condition)' is correct syntax, so no error there.
  3. Final Answer:

    Missing import of sys module -> Option A
  4. Quick Check:

    Use imports for conditions in markers [OK]
Hint: Always import modules used in marker conditions [OK]
Common Mistakes:
  • Forgetting to import modules like sys
  • Misnaming skipif marker
  • Using skipif without condition parentheses
5. You want to run only tests marked as 'database' but skip those also marked 'slow'. Which pytest command achieves this?
hard
A. pytest -m "database and not slow"
B. pytest -m "slow and not database"
C. pytest -m "database or slow"
D. pytest -m "not database and slow"

Solution

  1. Step 1: Understand the requirement

    Run tests with 'database' marker but exclude those with 'slow'.
  2. Step 2: Translate to pytest marker expression

    Use 'database and not slow' to select tests marked database but not slow.
  3. Final Answer:

    pytest -m "database and not slow" -> Option A
  4. Quick Check:

    Use 'and not' to exclude markers [OK]
Hint: Combine markers with 'and not' to exclude tests [OK]
Common Mistakes:
  • Swapping marker names in the expression
  • Using 'or' instead of 'and not'
  • Misunderstanding marker logic in pytest -m
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