0
0
PyTesttesting~20 mins

Running tests (pytest command) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Pytest Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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
A2 passed in 0.01s
B1 failed in 0.01s
C1 passed, 1 failed in 0.01s
DNo tests collected
Attempts:
2 left
💡 Hint
pytest reports the number of tests passed and failed after running.
assertion
intermediate
2: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')
Aassert foo() == ValueError
B
with pytest.raises(ValueError):
    foo()
Cassert foo() raises ValueError
Dassert foo() throws ValueError
Attempts:
2 left
💡 Hint
pytest has a special context manager for checking exceptions.
🔧 Debug
advanced
2: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
ATest function name does not start with 'test_'
BFunction has no parameters
CMissing import pytest statement
DFile name should not start with 'test_'
Attempts:
2 left
💡 Hint
pytest discovers tests by function name patterns.
framework
advanced
2: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?
Apytest -q --maxfail=0
Bpytest --maxfail=1 --quiet
Cpytest --stop-on-failure --verbose
Dpytest -v -x
Attempts:
2 left
💡 Hint
Check pytest options for verbosity and stopping on failure.
🧠 Conceptual
expert
2: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?
ASetup once before all 5 tests, teardown once after all tests
BSetup and teardown before and after each test (5 times each)
CSetup and teardown twice, splitting tests into groups
DSetup once before first test, teardown after third test
Attempts:
2 left
💡 Hint
Fixture scope controls how often setup/teardown runs.