0
0
PyTesttesting~20 mins

@pytest.mark.skipif with condition - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Skipif Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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 True
PyTest
import sys
import pytest

@pytest.mark.skipif(sys.platform == 'win32', reason='Skip on Windows')
def test_example():
    assert True
ATest is skipped only on Windows platform
BTest runs and passes on all platforms
CTest fails on Windows platform
DTest is skipped on all platforms
Attempts:
2 left
💡 Hint
Think about what sys.platform returns and how skipif uses it.
assertion
intermediate
2: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 == 4
PyTest
import sys
import pytest

@pytest.mark.skipif(sys.platform != 'linux', reason='Only run on Linux')
def test_value():
    assert 2 + 2 == 4
ATest is skipped on Linux
BTest runs and assertion passes on Linux
CTest runs and assertion fails on Linux
DTest is skipped on all platforms
Attempts:
2 left
💡 Hint
Check the condition sys.platform != 'linux' and what it means on Linux.
🔧 Debug
advanced
2: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 True
PyTest
import pytest

@pytest.mark.skipif('sys.platform == "darwin"', reason='Skip on Mac')
def test_func():
    assert True
ATypeError because condition is a string, not a boolean
BTest is skipped on MacOS
CTest runs and passes on all platforms
DSyntaxError due to wrong quotes
Attempts:
2 left
💡 Hint
Check the type of the condition argument in skipif.
framework
advanced
2: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
Aos.getenv('TEST_ENV') == 'prod'
Bos.environ['TEST_ENV'] == 'prod'
Cos.environ.get('TEST_ENV') == 'prod'
Dos.getenv('TEST_ENV') != 'prod'
Attempts:
2 left
💡 Hint
Remember skipif skips when condition is True.
🧠 Conceptual
expert
2:00remaining
Behavior of skipif with dynamic condition
Consider this test code:
import pytest
import random

@pytest.mark.skipif(random.choice([True, False]), reason='Random skip')
def test_random():
    assert True

What 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
ATest is randomly skipped or run each pytest run
BTest is always run because skipif ignores dynamic conditions
CTest is always skipped because condition is evaluated once at import
DTest raises an error due to random in skipif
Attempts:
2 left
💡 Hint
Think about when skipif condition is evaluated.