Bird
0
0

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📝 framework Q15 of 15
PyTest - Markers
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?
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')
Step-by-Step Solution
Solution:
  1. Step 1: Understand environment variable access

    Use os.getenv('TEST_ENV') to safely get the variable; it returns None if not set.
  2. Step 2: Write correct skip condition

    Skip only if value equals 'production', so condition is os.getenv('TEST_ENV') == 'production'.
  3. 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.
  4. Final Answer:

    @pytest.mark.skipif(os.getenv('TEST_ENV') == 'production', reason='Skip in production') -> Option B
  5. Quick Check:

    Use os.getenv and == 'production' for skipif [OK]
Quick Trick: Use os.getenv for safe env check in skipif [OK]
Common Mistakes:
MISTAKES
  • Using os.environ without handling missing keys
  • Comparing strings directly instead of env variable
  • Reversing the skip condition logic

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PyTest Quizzes