@pytest.mark.skipif requires a condition argument to decide when to skip.
Step 2: Identify missing condition
Here, only reason is provided, no condition, so syntax is invalid.
Final Answer:
Missing condition argument in skipif decorator -> Option C
Quick Check:
skipif needs condition argument [OK]
Hint: Always provide condition before reason in skipif [OK]
Common Mistakes:
Omitting the condition argument
Placing reason before condition
Confusing skipif with skip
5. You want to skip a test only if the environment variable TEST_ENV is set to production. Which of the following is the correct way to write this using @pytest.mark.skipif?
hard
A. @pytest.mark.skipif(os.environ['TEST_ENV'] == 'production', reason='Skip in production')
B. @pytest.mark.skipif(os.getenv('TEST_ENV') == 'production', reason='Skip in production')
C. @pytest.mark.skipif('TEST_ENV' == 'production', reason='Skip in production')
D. @pytest.mark.skipif(os.getenv('TEST_ENV') != 'production', reason='Skip in production')
Solution
Step 1: Understand environment variable access
Use os.getenv('TEST_ENV') to safely get the variable; it returns None if not set.
Step 2: Write correct skip condition
Skip only if value equals 'production', so condition is os.getenv('TEST_ENV') == 'production'.
Step 3: Check other options
@pytest.mark.skipif('TEST_ENV' == 'production', reason='Skip in production') compares string literals, not env variable. @pytest.mark.skipif(os.environ['TEST_ENV'] == 'production', reason='Skip in production') may raise KeyError if variable missing. @pytest.mark.skipif(os.getenv('TEST_ENV') != 'production', reason='Skip in production') skips when not production, opposite of requirement.
Final Answer:
@pytest.mark.skipif(os.getenv('TEST_ENV') == 'production', reason='Skip in production') -> Option B
Quick Check:
Use os.getenv and == 'production' for skipif [OK]
Hint: Use os.getenv for safe env check in skipif [OK]
Common Mistakes:
Using os.environ without handling missing keys
Comparing strings directly instead of env variable
Reversing the skip condition logic
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.