Bird
Raised Fist0
PyTesttesting~20 mins

Test modules in PyTest - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Test Module Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of pytest test discovery
Given the following test module, what will pytest report when run?
PyTest
def test_addition():
    assert 1 + 1 == 2

def test_subtraction():
    assert 2 - 1 == 1

class TestMath:
    def test_multiply(self):
        assert 2 * 3 == 6

    def helper(self):
        return 5
A3 passed tests
B2 passed tests, 1 skipped
C2 passed tests
D1 passed test, 2 failed
Attempts:
2 left
💡 Hint
pytest discovers functions starting with 'test_' and methods inside classes named 'Test' starting with 'test_'.
❓ assertion
intermediate
2:00remaining
Correct assertion for checking exception in pytest
Which assertion correctly tests that a function raises a ValueError when called with invalid input?
PyTest
def func(x):
    if x < 0:
        raise ValueError("Negative value")
    return x
Aassert func(-1) raises ValueError
Bassert func(-1) == ValueError
C
with pytest.raises(ValueError):
    func(-1)
DassertRaises(ValueError, func, -1)
Attempts:
2 left
💡 Hint
Use pytest's context manager to check exceptions.
🔧 Debug
advanced
2:00remaining
Identify the cause of test not running
Why does pytest not run the test method in this module?
PyTest
class math_tests:
    def test_divide(self):
        assert 10 / 2 == 5
AClass name does not start with 'Test', so pytest ignores it.
BThe assert statement is incorrect and causes a syntax error.
CMethod name does not start with 'test_', so pytest ignores it.
DThe division operator is invalid in Python.
Attempts:
2 left
💡 Hint
pytest requires test classes to start with 'Test'.
❓ framework
advanced
2:00remaining
Best practice for test module organization
Which option describes the best practice for organizing test modules in a pytest project?
AWrite all tests in a single large file named 'all_tests.py'.
BPut all tests inside the main application modules to keep code together.
CName test files with any name and place them randomly in the project.
DPlace test files in a separate 'tests' directory with filenames starting with 'test_'.
Attempts:
2 left
💡 Hint
pytest automatically discovers tests in files named 'test_*.py' or '*_test.py'.
🧠 Conceptual
expert
2:00remaining
Understanding pytest fixture scope impact on test modules
Consider a pytest fixture defined with scope='module'. What is the impact of this scope on tests within the same module?
AThe fixture is created once for the entire test session and shared globally.
BThe fixture is created once per module and shared by all tests in that module.
CThe fixture is created once per test class and shared only within that class.
DThe fixture is created once per test function and destroyed after each test.
Attempts:
2 left
💡 Hint
Fixture scopes control how often the fixture setup runs.

Practice

(1/5)
1. What is the correct way to name a Python file so that pytest recognizes it as a test module?
easy
A. The file name can be anything as long as it contains tests.
B. The file name should start with check_.
C. The file name should be pytest_module.py.
D. The file name should start with test_ or end with _test.py.

Solution

  1. Step 1: Understand pytest file naming conventions

    pytest automatically discovers test files if their names start with test_ or end with _test.py.
  2. Step 2: Evaluate each option

    Only The file name should start with test_ or end with _test.py. matches the correct naming pattern for pytest to find test modules automatically.
  3. Final Answer:

    The file name should start with test_ or end with _test.py. -> Option D
  4. Quick Check:

    pytest test file naming = start with test_ or end with _test.py [OK]
Hint: Remember test files start with test_ or end with _test.py [OK]
Common Mistakes:
  • Assuming any file with tests is recognized
  • Using incorrect prefixes like check_
  • Naming files without .py extension
2. Which of the following is the correct syntax for a test function inside a pytest test module?
easy
A. def test_function():
B. def check_function():
C. def function_test():
D. def testing_function():

Solution

  1. Step 1: Recall pytest test function naming rules

    pytest runs functions that start with test_ automatically as tests.
  2. Step 2: Match options with naming rule

    Only def test_function(): defines a function starting with test_, so pytest will recognize it as a test.
  3. Final Answer:

    def test_function(): -> Option A
  4. Quick Check:

    Test functions start with test_ [OK]
Hint: Test functions must start with test_ to run automatically [OK]
Common Mistakes:
  • Naming test functions without test_ prefix
  • Using other prefixes like check_ or testing_
  • Forgetting to define functions with def
3. Given the following test module test_sample.py with these functions:
def test_add():
    assert 1 + 1 == 2

def add():
    return 1 + 1

def test_subtract():
    assert 2 - 1 == 1

Which functions will pytest run as tests?
medium
A. All three functions
B. Only add
C. test_add and test_subtract
D. Only test_add

Solution

  1. Step 1: Identify pytest test function naming

    pytest runs functions starting with test_ automatically as tests.
  2. Step 2: Check each function name

    test_add and test_subtract start with test_, so pytest runs them. add does not start with test_, so it is ignored.
  3. Final Answer:

    test_add and test_subtract -> Option C
  4. Quick Check:

    Functions starting with test_ run as tests [OK]
Hint: Only functions starting with test_ run as tests [OK]
Common Mistakes:
  • Thinking all functions in test file run as tests
  • Confusing helper functions with test functions
  • Ignoring function name prefixes
4. You have a test module named test_math.py with this code:
def test_multiply():
    assert 2 * 3 == 5

When you run pytest, what will happen?
medium
A. The test will fail because the assertion is incorrect.
B. pytest will not find any tests to run.
C. pytest will skip the test because of a syntax error.
D. The test will pass because 2 * 3 equals 6.

Solution

  1. Step 1: Analyze the assertion in test_multiply

    The assertion checks if 2 * 3 == 5, which is false because 2 * 3 equals 6.
  2. Step 2: Understand pytest test result

    pytest runs the test and finds the assertion false, so the test fails.
  3. Final Answer:

    The test will fail because the assertion is incorrect. -> Option A
  4. Quick Check:

    False assertion causes test failure [OK]
Hint: Check if assertion condition is true or false [OK]
Common Mistakes:
  • Assuming test passes despite wrong assertion
  • Confusing syntax errors with assertion failures
  • Thinking pytest skips failing tests
5. You want to organize tests for a calculator app. You create two files: test_add.py and test_subtract.py. Each file has multiple test functions starting with test_. How does pytest handle these test modules when you run pytest in the folder?
hard
A. pytest requires you to specify each test module to run tests.
B. pytest runs all test functions from both modules automatically.
C. pytest runs only tests from the first module alphabetically.
D. pytest runs tests only if you import modules manually.

Solution

  1. Step 1: Understand pytest test discovery

    pytest automatically finds all test modules named with test_ prefix and runs all test functions inside them.
  2. Step 2: Apply to multiple test modules

    When running pytest in the folder, it collects tests from both test_add.py and test_subtract.py and runs all test functions starting with test_.
  3. Final Answer:

    pytest runs all test functions from both modules automatically. -> Option B
  4. Quick Check:

    pytest auto-discovers all test modules and functions [OK]
Hint: pytest runs all test_ modules and functions in folder automatically [OK]
Common Mistakes:
  • Thinking pytest runs only one module at a time
  • Believing manual imports are needed for tests
  • Assuming tests must be specified explicitly
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