0
0
PyTesttesting~20 mins

Test modules in PyTest - Practice Problems & Coding Challenges

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