Challenge - 5 Problems
Pytest Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of pytest with a failing test
What will be the output summary when running pytest on the following test file containing one passing and one failing test?
PyTest
def test_pass(): assert 1 == 1 def test_fail(): assert 2 == 3
Attempts:
2 left
💡 Hint
pytest reports the number of tests passed and failed after running.
✗ Incorrect
pytest runs both tests. One passes, one fails, so the summary shows '1 passed, 1 failed'.
❓ assertion
intermediate2:00remaining
Correct assertion to check a function raises an exception
Which pytest assertion correctly tests that calling function foo() raises a ValueError?
PyTest
def foo(): raise ValueError('error')
Attempts:
2 left
💡 Hint
pytest has a special context manager for checking exceptions.
✗ Incorrect
pytest.raises is the correct way to assert exceptions in pytest. Other options are invalid syntax or incorrect.
🔧 Debug
advanced2:00remaining
Why does pytest not discover tests in this file?
Given this test file named test_sample.py, pytest does not find any tests. Why?
PyTest
def sample_test(): assert True
Attempts:
2 left
💡 Hint
pytest discovers tests by function name patterns.
✗ Incorrect
pytest only discovers functions starting with 'test_'. 'sample_test' does not match, so no tests found.
❓ framework
advanced2:00remaining
How to run pytest with detailed output and stop after first failure
Which pytest command runs tests showing detailed info and stops after the first failure?
Attempts:
2 left
💡 Hint
Check pytest options for verbosity and stopping on failure.
✗ Incorrect
-v is verbose, -x stops after first failure. Other options are invalid or contradictory.
🧠 Conceptual
expert2:00remaining
Understanding pytest fixture scope impact on test runs
If a pytest fixture has scope='module', how many times is it set up and torn down when running 5 tests in the same module?
Attempts:
2 left
💡 Hint
Fixture scope controls how often setup/teardown runs.
✗ Incorrect
Module scope means fixture runs once per module, so setup and teardown happen once for all tests in that module.