Bird
Raised Fist0
PyTesttesting~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the basic pytest command do when run in a project folder?
easy
A. It only checks the syntax of test files without running them.
B. It automatically finds and runs all test files and test functions.
C. It deletes all test files in the folder.
D. It installs pytest in the project folder.

Solution

  1. Step 1: Understand the default behavior of pytest command

    Running pytest without arguments searches for test files and runs all tests found.
  2. Step 2: Eliminate incorrect options

    Options A, C, and D describe actions pytest does not perform by default.
  3. Final Answer:

    It automatically finds and runs all test files and test functions. -> Option B
  4. Quick Check:

    pytest runs tests automatically = B [OK]
Hint: pytest runs all tests by default when no file is specified [OK]
Common Mistakes:
  • Thinking pytest only checks syntax
  • Assuming pytest deletes files
  • Confusing pytest with package installer
2. Which of the following is the correct command to run pytest with detailed output?
easy
A. pytest -v
B. pytest --detail
C. pytest -d
D. pytest --verbose-output

Solution

  1. Step 1: Recall pytest option for verbose output

    The -v option stands for verbose and shows detailed test results.
  2. Step 2: Check other options for correctness

    Options -d, --detail, and --verbose-output are not valid pytest flags.
  3. Final Answer:

    pytest -v -> Option A
  4. Quick Check:

    Verbose flag is -v = C [OK]
Hint: Use -v for verbose pytest output [OK]
Common Mistakes:
  • Using -d instead of -v
  • Typing long incorrect flags
  • Confusing verbose with debug flags
3. Given the command pytest test_math.py, what will happen?
medium
A. pytest runs all tests in all files in the folder.
B. pytest runs tests but ignores test_math.py.
C. pytest runs only the tests inside the file named test_math.py.
D. pytest runs tests only if they are named test_math.py inside any folder.

Solution

  1. Step 1: Understand pytest file argument behavior

    When a filename is given, pytest runs tests only from that file.
  2. Step 2: Eliminate incorrect options

    A: Runs all tests in all files (wrong when file specified). B: Ignores test_math.py (wrong). C: Misunderstands filename as test name search (wrong).
  3. Final Answer:

    pytest runs only the tests inside the file named test_math.py. -> Option C
  4. Quick Check:

    pytest filename runs tests in that file only = D [OK]
Hint: Specify filename to run tests only from that file [OK]
Common Mistakes:
  • Assuming pytest runs all tests ignoring filename
  • Confusing filename with test function name
  • Thinking pytest ignores specified files
4. You run pytest -v test_example.py but get an error: pytest: error: unrecognized arguments: -v. What is the likely cause?
medium
A. pytest does not support the -v option.
B. You typed the option incorrectly; it should be --verbose.
C. The test file test_example.py does not exist.
D. You are running pytest with a wrong command or from a wrong environment.

Solution

  1. Step 1: Understand the error message

    The error says -v is unrecognized, which is unusual because -v is a valid pytest option.
  2. Step 2: Identify environment or command issues

    This usually means pytest is not properly installed or a different command is running (like python or another tool).
  3. Final Answer:

    You are running pytest with a wrong command or from a wrong environment. -> Option D
  4. Quick Check:

    Unrecognized option means wrong environment = A [OK]
Hint: Check pytest installation and environment if options fail [OK]
Common Mistakes:
  • Assuming -v is invalid option
  • Ignoring environment issues
  • Blaming test file existence for option errors
5. You want to run only tests marked as slow in your project using pytest. Which command correctly runs only those tests?
hard
A. pytest -m slow
B. pytest --only slow
C. pytest --mark slow
D. pytest -s slow

Solution

  1. Step 1: Recall pytest marker option

    The -m option runs tests matching a marker expression, such as slow.
  2. Step 2: Verify other options

    Options --only, --mark, and -s do not filter tests by marker.
  3. Final Answer:

    pytest -m slow -> Option A
  4. Quick Check:

    Use -m to run tests by marker = A [OK]
Hint: Use -m marker to run tests with that marker [OK]
Common Mistakes:
  • Using incorrect flags like --only or --mark
  • Confusing -s (capture output) with marker filtering
  • Not marking tests before filtering
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard