Bird
Raised Fist0
PyTesttesting~20 mins

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

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

Practice

(1/5)
1. What does the -m option do when running pytest?
easy
A. Runs only tests with the specified marker
B. Runs tests in a specific directory
C. Runs tests in parallel
D. Generates a test report

Solution

  1. Step 1: Understand pytest markers

    Markers are labels you add to tests to group them logically.
  2. Step 2: Use -m option

    The -m option runs only tests that have the specified marker label.
  3. Final Answer:

    Runs only tests with the specified marker -> Option A
  4. Quick Check:

    pytest -m runs tests by marker = A [OK]
Hint: Remember: -m filters tests by marker name [OK]
Common Mistakes:
  • Confusing -m with directory or file options
  • Thinking -m runs all tests regardless of marker
  • Assuming -m runs tests in parallel
2. Which of the following is the correct syntax to run pytest tests marked as slow?
easy
A. pytest --marker slow
B. pytest -m slow
C. pytest -marker slow
D. pytest --m slow

Solution

  1. Step 1: Recall pytest command options

    The correct option to select tests by marker is -m, not --marker or others.
  2. Step 2: Match syntax to run tests with marker

    The correct command is pytest -m slow to run tests marked as slow.
  3. Final Answer:

    pytest -m slow -> Option B
  4. Quick Check:

    Use single dash -m for marker filter = D [OK]
Hint: Use single dash -m, not --marker [OK]
Common Mistakes:
  • Using --marker instead of -m
  • Adding extra dashes like --m
  • Confusing option names
3. Given these tests marked as fast and db:
@pytest.mark.fast
def test_a(): pass

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

@pytest.mark.fast
def test_c(): pass
What tests run with the command pytest -m "fast and not db"?
medium
A. test_b only
B. No tests run
C. test_a, test_b, and test_c
D. test_a and test_c

Solution

  1. Step 1: Understand the marker expression

    The command runs tests marked with fast AND NOT marked with db.
  2. Step 2: Check each test's markers

    test_a and test_c have fast only; test_b has db only.
  3. Final Answer:

    test_a and test_c -> Option D
  4. Quick Check:

    fast and not db = test_a, test_c [OK]
Hint: Use logical operators to combine markers correctly [OK]
Common Mistakes:
  • Including tests marked only with db
  • Ignoring the 'not' operator
  • Assuming all tests run
4. You run pytest -m slow but no tests run, even though some tests have @pytest.mark.slow. What is the most likely cause?
medium
A. Markers are not registered in pytest.ini
B. You used -m instead of --m
C. The tests are skipped by default
D. The test files are missing

Solution

  1. Step 1: Check marker registration

    Pytest requires custom markers to be registered in pytest.ini to avoid warnings and ensure correct filtering.
  2. Step 2: Understand effect of missing registration

    If markers are not registered, pytest may ignore them or not run tests filtered by them.
  3. Final Answer:

    Markers are not registered in pytest.ini -> Option A
  4. Quick Check:

    Unregistered markers cause no tests run = C [OK]
Hint: Always register custom markers in pytest.ini [OK]
Common Mistakes:
  • Confusing -m and --m options
  • Assuming tests are skipped by default
  • Not checking test file presence
5. You have tests marked as api, ui, and db. You want to run tests that are either api or ui, but not db. Which command achieves this?
hard
A. pytest -m "api or (ui and not db)"
B. pytest -m "api or ui and not db"
C. pytest -m "(api or ui) and not db"
D. pytest -m "api and ui and not db"

Solution

  1. Step 1: Understand operator precedence

    Logical AND has higher precedence than OR, so parentheses are needed to group api or ui together.
  2. Step 2: Analyze each option

    pytest -m "(api or ui) and not db" correctly groups (api or ui) and excludes db with and not db.
  3. Final Answer:

    pytest -m "(api or ui) and not db" -> Option C
  4. Quick Check:

    Use parentheses to group OR conditions = A [OK]
Hint: Use parentheses to group markers with OR before AND [OK]
Common Mistakes:
  • Ignoring operator precedence without parentheses
  • Using AND instead of OR between api and ui
  • Including db tests by mistake
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