Bird
Raised Fist0
PyTesttesting~20 mins

Marker registration in PyTest - Practice Problems & Coding Challenges

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
1:30remaining
Purpose of marker registration in pytest
Why is it important to register custom markers in pytest's pytest.ini or pyproject.toml file?
ATo avoid warnings about unknown markers during test runs and enable marker-based test selection.
BTo automatically generate test reports without additional plugins.
CTo speed up test execution by caching marker results.
DTo disable all tests that use custom markers by default.
Attempts:
2 left
💡 Hint
Think about what happens if pytest sees a marker it does not recognize.
❓ Predict Output
intermediate
1:30remaining
Output of pytest when using unregistered marker
What output will pytest produce when running this test with an unregistered marker @pytest.mark.slow?
PyTest
import pytest

@pytest.mark.slow
def test_example():
    assert True
ASkips the test silently without any warning.
BFails the test immediately with a SyntaxError.
CRuns the test without any warning or message.
DRuns the test and shows a warning: 'PytestUnknownMarkWarning: Unknown pytest.mark.slow - is this a typo?'.
Attempts:
2 left
💡 Hint
Think about pytest's behavior when it sees a marker it does not know.
❓ locator
advanced
1:30remaining
Correct location to register a custom marker
Where should you add the registration for a custom marker named integration to avoid warnings in pytest?
AIn the test function's docstring describing the test purpose.
BDirectly inside each test file as a comment above the test function.
CIn the <code>pytest.ini</code> file under the <code>[pytest]</code> section with <code>markers = integration: marks tests as integration tests</code>.
DIn the <code>setup.py</code> file as an entry point for pytest.
Attempts:
2 left
💡 Hint
Think about pytest configuration files that control test behavior globally.
❓ assertion
advanced
2:00remaining
Assertion to verify marker presence on a test function
Given a test function decorated with @pytest.mark.api, which assertion correctly checks for the presence of the 'api' marker in a pytest hook?
PyTest
def pytest_collection_modifyitems(items):
    for item in items:
        # Which assertion is correct here?
        pass
Aassert 'api' in item.keywords
Bassert any(mark.name == 'api' for mark in item.iter_markers())
Cassert item.marker == 'api'
Dassert item.has_marker('api')
Attempts:
2 left
💡 Hint
Look at how pytest exposes markers on collected test items.
❓ framework
expert
2:00remaining
Effect of missing marker registration on test selection
You have registered a custom marker db in pytest.ini. You run pytest -m db to select tests with this marker. What happens if you remove the marker registration but still run pytest -m db?
APytest runs only tests with the <code>db</code> marker but shows a warning about unknown marker.
BPytest runs all tests ignoring the <code>-m db</code> option silently.
CPytest raises an error and stops because the marker is unknown and cannot be used for selection.
DPytest skips all tests because no tests match the unregistered marker.
Attempts:
2 left
💡 Hint
Consider pytest's behavior when selecting tests by an unregistered marker.

Practice

(1/5)
1. What is the main purpose of registering markers in pytest.ini?
easy
A. To avoid warnings when using custom markers in tests
B. To automatically run all tests without specifying markers
C. To disable all markers in the test suite
D. To change the default test discovery pattern

Solution

  1. Step 1: Understand marker registration role

    Markers must be registered in pytest.ini to inform pytest about custom markers.
  2. Step 2: Identify effect of registration

    Without registration, pytest shows warnings about unknown markers; registration prevents these warnings.
  3. Final Answer:

    To avoid warnings when using custom markers in tests -> Option A
  4. Quick Check:

    Marker registration = avoid warnings [OK]
Hint: Register markers in pytest.ini to stop warnings [OK]
Common Mistakes:
  • Thinking registration runs tests automatically
  • Believing registration disables markers
  • Confusing marker registration with test discovery
2. Which of the following is the correct syntax to register a custom marker named slow in pytest.ini?
easy
A. [pytest] markers = slow: marks tests as slow
B. [markers] slow = marks tests as slow
C. [pytest] marker = slow: marks tests as slow
D. [pytest] markers = slow marks tests as slow

Solution

  1. Step 1: Recall pytest.ini marker syntax

    Markers are registered under the [pytest] section with the key 'markers' followed by marker descriptions.
  2. Step 2: Match correct syntax

    [pytest] markers = slow: marks tests as slow correctly uses '[pytest]' section and 'markers = slow: marks tests as slow' format.
  3. Final Answer:

    [pytest] markers = slow: marks tests as slow -> Option A
  4. Quick Check:

    Correct section and key = [pytest] markers = slow: marks tests as slow [OK]
Hint: Use [pytest] and 'markers =' to register markers [OK]
Common Mistakes:
  • Using wrong section name like [markers]
  • Misspelling 'markers' key
  • Omitting colon after marker name
3. Given the following pytest.ini content and test code, what will be the result when running pytest -m slow?
[pytest]
markers = slow: marks tests as slow
import pytest

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

def test_fast():
    assert True
medium
A. Only test_fast runs and passes
B. Both test_example and test_fast run and pass
C. Only test_example runs and passes
D. No tests run due to marker misconfiguration

Solution

  1. Step 1: Understand -m slow option

    The -m slow option runs only tests marked with @pytest.mark.slow.
  2. Step 2: Identify marked tests

    test_example is marked with @pytest.mark.slow, test_fast is not.
  3. Final Answer:

    Only test_example runs and passes -> Option C
  4. Quick Check:

    Run with -m slow = only slow tests run [OK]
Hint: -m runs only tests with matching marker [OK]
Common Mistakes:
  • Assuming all tests run with -m option
  • Confusing marker name with test function name
  • Ignoring marker registration effect
4. You added a custom marker database to your tests but pytest shows a warning: PytestUnknownMarkWarning. What is the most likely cause and how to fix it?
medium
A. You must run pytest with --disable-warnings to hide the message
B. You forgot to register the database marker in pytest.ini; add it under [pytest] markers
C. You need to rename the marker to db to avoid warnings
D. You used @pytest.mark.database incorrectly; remove the decorator

Solution

  1. Step 1: Identify cause of PytestUnknownMarkWarning

    This warning appears when a custom marker is used but not registered in pytest.ini.
  2. Step 2: Fix by registering marker

    Add database marker under the [pytest] section in pytest.ini to register it and remove the warning.
  3. Final Answer:

    You forgot to register the database marker in pytest.ini; add it under [pytest] markers -> Option B
  4. Quick Check:

    Warning = missing marker registration [OK]
Hint: Register all custom markers in pytest.ini to avoid warnings [OK]
Common Mistakes:
  • Ignoring the warning and continuing
  • Removing marker decorator instead of registering
  • Trying to rename marker instead of registering
5. You want to run tests marked as api but skip those marked as slow. Your pytest.ini contains:
[pytest]
markers =
    api: marks API tests
    slow: marks slow tests
Which command correctly runs only api tests excluding slow tests?
hard
A. pytest -m "not api and slow"
B. pytest -m "api or slow"
C. pytest -m "slow and not api"
D. pytest -m "api and not slow"

Solution

  1. Step 1: Understand marker expressions with -m

    The -m option accepts boolean expressions combining markers using and, or, and not.
  2. Step 2: Build expression for api tests excluding slow

    We want tests marked api but not slow, so expression is api and not slow.
  3. Final Answer:

    pytest -m "api and not slow" -> Option D
  4. Quick Check:

    Run api tests excluding slow = pytest -m "api and not slow" [OK]
Hint: Use -m "api and not slow" to include/exclude markers [OK]
Common Mistakes:
  • Using 'or' instead of 'and not' excludes wrong tests
  • Misplacing 'not' operator
  • Confusing marker names in expression
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