Bird
Raised Fist0
PyTesttesting~5 mins

Custom markers in PyTest - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What is a custom marker in pytest?
A custom marker is a label you create to tag tests for special handling, like grouping or skipping them during test runs.
Click to reveal answer
beginner
How do you define a custom marker in pytest?
You define a custom marker by adding it to the pytest.ini file under the [pytest] section using the markers option.
Click to reveal answer
beginner
How do you apply a custom marker to a test function?
Use the @pytest.mark.<marker_name> decorator above the test function to apply the custom marker.
Click to reveal answer
beginner
What command runs only tests with a specific custom marker?
Use pytest -m '<marker_name>' to run only tests tagged with that custom marker.
Click to reveal answer
intermediate
Why should you register custom markers in pytest.ini?
Registering markers avoids warnings and helps pytest recognize your custom tags properly.
Click to reveal answer
How do you mark a test with a custom marker named 'slow'?
A@pytest.mark.slow
B@slow
Cmark('slow')
Dpytest.mark('slow')
Where do you register custom markers to avoid warnings?
Atest file header
Bconftest.py
Csetup.py
Dpytest.ini
What does the command 'pytest -m slow' do?
ASkips tests marked with 'slow'
BRuns only tests marked with 'slow'
CRuns all tests except those marked 'slow'
DRuns tests with names containing 'slow'
What happens if you use a custom marker without registering it?
ATests run but marker is ignored silently
BTests fail to run
CPytest shows a warning about unknown markers
DPytest crashes
Which file is best for defining markers used across many test files?
Apytest.ini
Btest_example.py
Crequirements.txt
DREADME.md
Explain how to create and use a custom marker in pytest.
Think about registration, tagging, and running tests.
You got /3 concepts.
    Why is it important to register custom markers in pytest.ini?
    Consider what happens if you don't register.
    You got /3 concepts.

      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