Test Overview
This test demonstrates how to use @pytest.mark.skipif to skip a test when a specific condition is true. It verifies that the test is skipped when the condition matches.
This test demonstrates how to use @pytest.mark.skipif to skip a test when a specific condition is true. It verifies that the test is skipped when the condition matches.
import sys import pytest @pytest.mark.skipif(sys.platform == "win32", reason="Skip on Windows") def test_not_on_windows(): assert True
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Pytest starts test collection and identifies test_not_on_windows with skipif condition | Test suite ready to run, current platform detected as Linux | — | PASS |
| 2 | Pytest evaluates skipif condition sys.platform == 'win32' | Condition evaluates to False on Linux | — | PASS |
| 3 | Pytest runs test_not_on_windows function | Test executes normally | assert True | PASS |
| 4 | Test completes successfully without skipping | Test passed | — | PASS |
@pytest.mark.skipif in a test?@pytest.mark.skipif is used to skip tests conditionally.@pytest.mark.skipif, not @pytest.skipif.sys.version_info tuple; comparison uses < (less than) and tuple format (3, 8).import sys
import pytest
@pytest.mark.skipif(sys.platform != 'win32', reason='Only runs on Windows')
def test_windows_feature():
assert Truesys.platform != 'win32' means skip if platform is not Windows.sys.platform == 'win32', so condition is False, test runs. On other OS, condition True, test skipped.import pytest
@pytest.mark.skipif(reason='No condition provided')
def test_example():
assert True@pytest.mark.skipif requires a condition argument to decide when to skip.reason is provided, no condition, so syntax is invalid.TEST_ENV is set to production. Which of the following is the correct way to write this using @pytest.mark.skipif?os.getenv('TEST_ENV') to safely get the variable; it returns None if not set.os.getenv('TEST_ENV') == 'production'.Pass the test and this pattern moves unknown