Bird
Raised Fist0
PyTesttesting~5 mins

Single parameter in PyTest - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What is the purpose of using a single parameter in pytest test functions?
A single parameter allows you to pass different values to the test function, enabling the same test to run multiple times with different inputs.
Click to reveal answer
beginner
How do you mark a test function to run multiple times with different values for a single parameter in pytest?
Use the @pytest.mark.parametrize decorator with the parameter name and a list of values to test.
Click to reveal answer
beginner
Example: What does this pytest code do?
@pytest.mark.parametrize('x', [1, 2, 3])
def test_example(x):
    assert x > 0
It runs test_example three times with x equal to 1, 2, and 3. Each time it checks that x is greater than 0.
Click to reveal answer
beginner
Why is using a single parameter with @pytest.mark.parametrize helpful in testing?
It saves time by avoiding writing multiple similar test functions and helps check how code behaves with different inputs.
Click to reveal answer
beginner
What happens if one of the parameter values causes the assertion to fail in a single parameter pytest test?
Only the test run with that failing value will fail; other runs with different values will still pass if their assertions hold.
Click to reveal answer
Which pytest decorator is used to run a test multiple times with different values for a single parameter?
A@pytest.mark.repeat
B@pytest.mark.parametrize
C@pytest.mark.singleparam
D@pytest.mark.testcase
How do you specify the parameter name in @pytest.mark.parametrize for a single parameter?
ANo need to specify parameter names
BAs a list of parameter names
CAs a dictionary of parameter names and values
DAs a string with the parameter name, e.g., 'x'
What will happen if you run this test? @pytest.mark.parametrize('num', [0, 1, 2]) def test_positive(num): assert num > 0
AThe test will pass for all values
BThe test will fail for all values
CThe test will fail for num=0 and pass for num=1 and num=2
DThe test will only run once
Why might you prefer using a single parameter with @pytest.mark.parametrize instead of writing multiple test functions?
AIt reduces code duplication and makes tests easier to maintain
BIt makes tests run slower
CIt hides test failures
DIt requires more code
In pytest, what type of values can you pass to a single parameter in @pytest.mark.parametrize?
AAny iterable of values like list, tuple, or set
BOnly integers
COnly strings
DOnly dictionaries
Explain how to use a single parameter with @pytest.mark.parametrize to run a test multiple times with different inputs.
Think about how to tell pytest to repeat a test with different values.
You got /5 concepts.
    Describe what happens when one of the parameter values causes an assertion failure in a pytest test using a single parameter.
    Consider how pytest handles multiple test runs with different inputs.
    You got /4 concepts.

      Practice

      (1/5)
      1. What is the purpose of using a single parameter in a pytest test function?
      easy
      A. To define multiple test functions at once
      B. To make the test function run only once
      C. To skip the test function during execution
      D. To pass different values to the test function for repeated testing

      Solution

      1. Step 1: Understand pytest parameterization

        Pytest allows passing parameters to test functions to run the same test with different inputs.
      2. Step 2: Role of a single parameter

        A single parameter lets you supply multiple values to the test, so it runs multiple times with each value.
      3. Final Answer:

        To pass different values to the test function for repeated testing -> Option D
      4. Quick Check:

        Single parameter = multiple test runs [OK]
      Hint: Single parameter means testing with different inputs [OK]
      Common Mistakes:
      • Thinking single parameter defines multiple tests
      • Confusing parameter with skipping tests
      • Assuming single parameter runs test once only
      2. Which of the following is the correct syntax to use a single parameter with pytest's @pytest.mark.parametrize decorator?
      easy
      A. @pytest.mark.parametrize('num', 1, 2, 3)
      B. @pytest.mark.parametrize(num, [1, 2, 3])
      C. @pytest.mark.parametrize('num', [1, 2, 3])
      D. @pytest.mark.parametrize(['num'], 1, 2, 3)

      Solution

      1. Step 1: Check parameter name format

        The parameter name must be a string inside quotes, e.g., 'num'.
      2. Step 2: Check values format

        The values must be inside a list or iterable, e.g., [1, 2, 3].
      3. Final Answer:

        @pytest.mark.parametrize('num', [1, 2, 3]) -> Option C
      4. Quick Check:

        Parametrize syntax = string + list [OK]
      Hint: Parameter name in quotes, values in list brackets [OK]
      Common Mistakes:
      • Not quoting the parameter name
      • Passing values without list brackets
      • Using wrong argument structure
      3. What will be the output when running this pytest code?
      import pytest
      
      @pytest.mark.parametrize('x', [2, 4])
      def test_double(x):
          assert x * 2 == 4
      medium
      A. First test passes, second test fails
      B. Both tests fail
      C. First test fails, second test passes
      D. Both tests pass

      Solution

      1. Step 1: Evaluate first test with x=2

        2 * 2 = 4, assertion is true, test passes.
      2. Step 2: Evaluate second test with x=4

        4 * 2 = 8, assertion 8 == 4 is false, test fails.
      3. Final Answer:

        First test passes, second test fails -> Option A
      4. Quick Check:

        2*2=4 pass, 4*2=8 fail [OK]
      Hint: Check each input separately for assertion [OK]
      Common Mistakes:
      • Assuming all tests pass without checking values
      • Mixing up which input causes failure
      • Ignoring assertion logic
      4. Identify the error in this pytest code using a single parameter:
      import pytest
      
      @pytest.mark.parametrize('val', 1, 2, 3)
      def test_positive(val):
          assert val > 0
      medium
      A. Parameter name should not be in quotes
      B. Missing list brackets around parameter values
      C. Test function must have two parameters
      D. Assertion should use == instead of >

      Solution

      1. Step 1: Check parametrize values format

        Values must be inside a list or iterable, e.g., [1, 2, 3].
      2. Step 2: Identify missing brackets

        Code passes values as separate arguments instead of a single list, causing syntax error.
      3. Final Answer:

        Missing list brackets around parameter values -> Option B
      4. Quick Check:

        Values need list brackets [OK]
      Hint: Values must be in a list or tuple [OK]
      Common Mistakes:
      • Passing values without brackets
      • Removing quotes from parameter name
      • Wrong assertion operator
      5. Given this test function, how can you modify it to test multiple string inputs using a single parameter?
      def test_is_uppercase(s):
          assert s.isupper()
      hard
      A. Add @pytest.mark.parametrize('s', ['HELLO', 'WORLD']) above the function
      B. Change function to accept two parameters s and t
      C. Use a for loop inside the test to iterate over strings
      D. Call test_is_uppercase multiple times manually

      Solution

      1. Step 1: Understand parameterizing tests

        Using @pytest.mark.parametrize allows running the test multiple times with different inputs.
      2. Step 2: Apply parametrize decorator with string list

        Adding @pytest.mark.parametrize('s', ['HELLO', 'WORLD']) runs test_is_uppercase twice with each string.
      3. Final Answer:

        Add @pytest.mark.parametrize('s', ['HELLO', 'WORLD']) above the function -> Option A
      4. Quick Check:

        Parametrize decorator runs test with multiple inputs [OK]
      Hint: Use @pytest.mark.parametrize with list of inputs [OK]
      Common Mistakes:
      • Trying to pass multiple parameters instead of one
      • Using loops inside test instead of parametrize
      • Calling test manually instead of using pytest features
      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