Challenge - 5 Problems
Skipif Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of test skipped condition
What will be the result of running this pytest test function?
import sys
import pytest
@pytest.mark.skipif(sys.platform == 'win32', reason='Skip on Windows')
def test_example():
assert TruePyTest
import sys import pytest @pytest.mark.skipif(sys.platform == 'win32', reason='Skip on Windows') def test_example(): assert True
Attempts:
2 left
💡 Hint
Think about what sys.platform returns and how skipif uses it.
✗ Incorrect
The decorator @pytest.mark.skipif skips the test only if the condition is True. Here, the condition is sys.platform == 'win32', so the test is skipped only on Windows.
❓ assertion
intermediate2:00remaining
Assertion outcome with skipif condition
Given this test code, what is the assertion outcome when run on Linux?
import sys
import pytest
@pytest.mark.skipif(sys.platform != 'linux', reason='Only run on Linux')
def test_value():
assert 2 + 2 == 4PyTest
import sys import pytest @pytest.mark.skipif(sys.platform != 'linux', reason='Only run on Linux') def test_value(): assert 2 + 2 == 4
Attempts:
2 left
💡 Hint
Check the condition sys.platform != 'linux' and what it means on Linux.
✗ Incorrect
The skipif condition is True if platform is not Linux. On Linux, condition is False, so test runs and assertion passes.
🔧 Debug
advanced2:00remaining
Identify error in skipif usage
What error will this pytest code raise when run?
import pytest
@pytest.mark.skipif('sys.platform == "darwin"', reason='Skip on Mac')
def test_func():
assert TruePyTest
import pytest @pytest.mark.skipif('sys.platform == "darwin"', reason='Skip on Mac') def test_func(): assert True
Attempts:
2 left
💡 Hint
Check the type of the condition argument in skipif.
✗ Incorrect
The condition argument must be a boolean expression, not a string. Passing a string causes TypeError.
❓ framework
advanced2:00remaining
Using skipif with environment variable
Which option correctly skips a test if the environment variable TEST_ENV is not set to 'prod'?
PyTest
import os import pytest @pytest.mark.skipif(____, reason='Skip if not prod environment') def test_env(): assert True
Attempts:
2 left
💡 Hint
Remember skipif skips when condition is True.
✗ Incorrect
We want to skip if TEST_ENV is not 'prod', so condition should be True when env is not 'prod'. Option D correctly expresses that.
🧠 Conceptual
expert2:00remaining
Behavior of skipif with dynamic condition
Consider this test code:
What is the expected behavior when running pytest multiple times?
import pytest
import random
@pytest.mark.skipif(random.choice([True, False]), reason='Random skip')
def test_random():
assert TrueWhat is the expected behavior when running pytest multiple times?
PyTest
import pytest import random @pytest.mark.skipif(random.choice([True, False]), reason='Random skip') def test_random(): assert True
Attempts:
2 left
💡 Hint
Think about when skipif condition is evaluated.
✗ Incorrect
skipif condition is evaluated once when tests are collected, not at each test run. So random.choice is called once per pytest session, making skip or run fixed per run.