A. The skipif condition is a string instead of a boolean expression
B. Missing import of sys module
C. pytest.param cannot be used inside parametrize
D. The reason argument is missing in skipif
Solution
Step 1: Check skipif condition type
The condition is passed as a string, but skipif expects a boolean expression, not a string.
Step 2: Confirm other parts
sys module is not imported, but the main error is the string condition. pytest.param usage and reason argument are correct.
Final Answer:
The skipif condition is a string instead of a boolean expression -> Option A
Quick Check:
skipif condition must be boolean, not string [OK]
Hint: skipif condition must be a boolean expression, not a string [OK]
Common Mistakes:
Passing condition as string instead of boolean
Ignoring missing sys import (not main error here)
Misusing pytest.param outside parametrize
5. You want to run a test with parameters mode set to 'fast', 'slow', and 'medium'. However, you want to skip the 'slow' mode test only if an environment variable SKIP_SLOW is set to '1'. Which is the correct way to write this conditional parametrize in pytest?
hard
A. import os
@pytest.mark.parametrize('mode', [
'fast',
pytest.param('slow', marks=pytest.mark.skipif('os.getenv("SKIP_SLOW") == "1"')),
'medium'
])
B. import os
@pytest.mark.parametrize('mode', [
'fast',
pytest.param('slow', skipif=os.getenv('SKIP_SLOW') == '1'),
'medium'
])
C. import os
@pytest.mark.parametrize('mode', [
'fast',
pytest.param('slow', marks=pytest.skipif(os.getenv('SKIP_SLOW') == '1')),
'medium'
])
D. import os
@pytest.mark.parametrize('mode', [
'fast',
pytest.param('slow', marks=pytest.mark.skipif(os.getenv('SKIP_SLOW') == '1', reason='Skip slow mode')),
'medium'
])
Solution
Step 1: Use os.getenv to check environment variable
os.getenv('SKIP_SLOW') returns the value of the environment variable as a string.
Step 2: Apply skipif mark correctly
Use pytest.mark.skipif with a boolean condition and a reason inside marks parameter of pytest.param.