Bird
Raised Fist0
PyTesttesting~10 mins

Custom markers in PyTest - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to mark the test with a custom marker named 'slow'.

PyTest
import pytest

@pytest.mark.[1]
def test_example():
    assert 1 + 1 == 2
Drag options to blanks, or click blank then click option'
Aslow
Bfast
Cskip
Dparametrize
Attempts:
3 left
💡 Hint
Common Mistakes
Using a built-in marker like 'skip' instead of the custom marker 'slow'.
Forgetting to use '@pytest.mark.' before the marker name.
2fill in blank
medium

Complete the code to run only tests marked with 'database' using pytest command line option.

PyTest
pytest -m [1]
Drag options to blanks, or click blank then click option'
Aslow
Bnetwork
Cskip
Ddatabase
Attempts:
3 left
💡 Hint
Common Mistakes
Using a marker name that does not exist on any test.
Omitting the -m option.
3fill in blank
hard

Fix the error in the code to register a custom marker 'api' in pytest.ini.

PyTest
[pytest]
markers =
    [1]: mark test as API test
Drag options to blanks, or click blank then click option'
Aapi
BAPI
CApi
DapI
Attempts:
3 left
💡 Hint
Common Mistakes
Using uppercase or mixed case marker names in pytest.ini that don't match the code.
Not registering the marker in pytest.ini causing warnings.
4fill in blank
hard

Fill both blanks to mark a test with 'integration' and skip it conditionally when a variable is False.

PyTest
import pytest

@pytest.mark.[1]
@pytest.mark.skipif(not is_ready, reason='Not ready')
def test_integration():
    assert True
Drag options to blanks, or click blank then click option'
Aintegration
Bunit
Cslow
Dskip
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'skip' instead of 'skipif' for conditional skipping.
Using wrong marker names.
5fill in blank
hard

Fill all three blanks to define and use a custom marker 'performance' with pytest.mark and run only those tests.

PyTest
import pytest

@pytest.mark.[1]
def test_speed():
    assert 100 < 200

# Run tests with: pytest -m [2]

# Register marker in pytest.ini:
#[pytest]
#markers =
#    [3]: mark test as performance test
Drag options to blanks, or click blank then click option'
Aperformance
Dslow
Attempts:
3 left
💡 Hint
Common Mistakes
Using different marker names in code and pytest.ini.
Forgetting to register the marker in pytest.ini.

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