Bird
Raised Fist0
PyTesttesting~20 mins

Custom markers in PyTest - Practice Problems & Coding Challenges

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

Practice

(1/5)
1. What is the main purpose of using custom markers in pytest?
easy
A. To generate test data automatically
B. To change the Python interpreter version
C. To automatically fix test failures
D. To label tests for grouping and selective running

Solution

  1. Step 1: Understand what custom markers do

    Custom markers in pytest are used to tag tests with labels for easier management.
  2. Step 2: Identify the purpose of these labels

    These labels help run specific groups of tests selectively using the marker name.
  3. Final Answer:

    To label tests for grouping and selective running -> Option D
  4. Quick Check:

    Custom markers = test grouping [OK]
Hint: Markers tag tests to run groups easily [OK]
Common Mistakes:
  • Thinking markers change Python version
  • Assuming markers fix test errors automatically
  • Confusing markers with test data generation
2. Which of the following is the correct syntax to mark a test with a custom marker named slow?
easy
A. @pytest.mark.slow\ndef test_example(): pass
B. @pytest.slow.mark\ndef test_example(): pass
C. @pytest.mark[slow]\ndef test_example(): pass
D. @pytest.mark('slow')\ndef test_example(): pass

Solution

  1. Step 1: Recall pytest marker syntax

    Custom markers use dot notation: @pytest.mark.markername before the test function.
  2. Step 2: Match the correct syntax

    @pytest.mark.slow is the correct way to apply the 'slow' marker.
  3. Final Answer:

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

    Marker syntax = @pytest.mark.name [OK]
Hint: Use dot notation: @pytest.mark.name [OK]
Common Mistakes:
  • Using parentheses instead of dot notation
  • Using square brackets instead of dot notation
  • Placing marker after function name
3. Given the following test file, what will be the output when running pytest -m slow?
import pytest

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

def test_b():
    assert True

@pytest.mark.slow
def test_c():
    assert False
medium
A. Runs all tests; test_c fails
B. Runs only test_b; all pass
C. Runs test_a and test_c; test_c fails, test_a passes
D. Runs test_a and test_b; all pass

Solution

  1. Step 1: Understand what pytest -m slow does

    This command runs only tests marked with the 'slow' marker.
  2. Step 2: Identify marked tests in the code

    test_a and test_c have @pytest.mark.slow, so only these run.
  3. Step 3: Check test results

    test_a passes (assert True), test_c fails (assert False).
  4. Final Answer:

    Runs test_a and test_c; test_c fails, test_a passes -> Option C
  5. Quick Check:

    pytest -m slow runs marked tests only [OK]
Hint: -m runs only tests with that marker [OK]
Common Mistakes:
  • Assuming all tests run with -m option
  • Ignoring test failures in marked tests
  • Running unmarked tests by mistake
4. What is wrong with this pytest marker usage?
import pytest

@pytest.mark.slow()
def test_example():
    assert True
medium
A. Markers should not have parentheses unless parameters are used
B. Marker name must be uppercase
C. The test function must have parameters
D. pytest.mark cannot be used on test functions

Solution

  1. Step 1: Check marker syntax

    Markers without parameters should not have parentheses; @pytest.mark.slow() is incorrect.
  2. Step 2: Correct usage

    Use @pytest.mark.slow without parentheses for simple markers.
  3. Final Answer:

    Markers should not have parentheses unless parameters are used -> Option A
  4. Quick Check:

    Use parentheses only with marker args [OK]
Hint: No parentheses for simple markers [OK]
Common Mistakes:
  • Adding empty parentheses to markers
  • Using uppercase marker names incorrectly
  • Thinking markers require function parameters
5. You want to run tests marked as database but exclude those also marked as slow. Which pytest command achieves this?
hard
A. pytest -m "database or slow"
B. pytest -m "database and not slow"
C. pytest -m "not database and slow"
D. pytest -m "database and slow"

Solution

  1. Step 1: Understand marker expressions

    pytest supports boolean expressions with markers: and, or, not.
  2. Step 2: Formulate the correct expression

    To run tests marked 'database' but exclude 'slow', use "database and not slow".
  3. Final Answer:

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

    Use boolean logic in -m to include/exclude markers [OK]
Hint: Use boolean logic: -m "marker and not other" [OK]
Common Mistakes:
  • Using 'or' instead of 'and not' to exclude
  • Misplacing 'not' before the first marker
  • Combining markers without quotes
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