0
0
PyTesttesting~5 mins

Single parameter in PyTest - Cheat Sheet & Quick Revision

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