AlgoMaster Logo

Testing - Quiz

Last Updated: December 6, 2025

Testing Exercises

31 quizzes

1
Code Completion

Complete the code so the test checks that the value is exactly 10 using a unittest assertion

python
1
self.(result, 10)

Click an option to fill the blank:

2
Code Completion

Complete the code to mark this test so that pytest expects a ValueError from risky_divide

python
1
with pytest.(ValueError):

Click an option to fill the blank:

3
Code Completion

Complete the code so that mock_client.get is replaced by a mock object during this test

python
1
with patch('service_client.') as fake_get:

Click an option to fill the blank:

4
Multiple Choice

Which statement best describes unit testing?

5
Multiple Choice

In unittest, which method name will be automatically collected as a test?

6
Multiple Choice

With pytest, which file name will be automatically discovered as a test module?

7
Multiple Choice

What is the main benefit of using fixtures in pytest?

8
Multiple Choice

Which unittest assertion is most appropriate for checking that a function raises a TypeError?

9
Multiple Choice

In pytest, how do you mark a test as expected to fail but still run it?

10
Multiple Choice

What is a common reason to use mocking in tests?

11
Multiple Choice

Which coverage metric tells you what percentage of executable lines ran during tests?

12
Multiple Choice

What is a key feature of doctest?

13
Multiple Choice

In TDD's Red-Green-Refactor cycle, what happens in the Green step?

14
Multiple Choice

When using coverage.py with pytest, which command both runs tests and measures coverage?

15
Sequencing

Order the steps to create a unittest.TestCase that tests a multiply function and then run it.

Drag and drop to reorder, or use the arrows.

16
Sequencing

Order the steps to apply TDD when adding a new subtract function to an existing calculator module using pytest.

Drag and drop to reorder, or use the arrows.

17
Output Prediction

What is the output of this code using unittest assertions?

1import unittest
2
3class Demo(unittest.TestCase):
4    def test_truth(self):
5        self.assertTrue(2 < 5)
6
7suite = unittest.TestLoader().loadTestsFromTestCase(Demo)
8result = unittest.TextTestRunner(stream=None, verbosity=0).run(suite)
9print(result.wasSuccessful())
18
Output Prediction

What is the output when using a pytest-style helper function?

1def add(a, b):
2    return a + b
3
4result = add(2, 3) == 5
5print(result)
19
Output Prediction

What is the output of this doctest-based script when run as a module?

1import doctest
2
3def square(x):
4    """Return x squared.
5
6    >>> square(3)
7    9
8    """
9    return x * x
10
11if __name__ == '__main__':
12    failures, _ = doctest.testmod()
13    print(failures == 0)
20
Output Prediction

What is printed when using a simple mock object?

1from unittest.mock import Mock
2
3api = Mock()
4api.fetch_user.return_value = {'id': 1, 'name': 'Alice'}
5
6user = api.fetch_user(1)
7print(user['name'])
21
Output Prediction

What does this small coverage-related snippet print?

1def choose(flag):
2    if flag:
3        return 'covered'
4    return 'also covered'
5
6print(choose(False))
22
Bug Spotting

Find the bug in this unittest setup method that prepares data for multiple tests.

Click on the line(s) that contain the bug.

python
1
import unittest
2
 
3
class TestData(unittest.TestCase):
4
    def setUp(self):
5
        shared_list = []
6
        shared_list.append('item')
7
 
8
    def test_length(self):
9
        self.assertEqual(len(self.shared_list), 1)
10
 
23
Bug Spotting

Find the bug in this code that attempts to use pytest fixtures.

Click on the line(s) that contain the bug.

python
1
import pytest
2
 
3
@pytest.fixture
4
def user_data():
5
    return {'name': 'Bob'}
6
 
7
class TestUser:
8
    def test_name(self):
9
        assert user_data['name'] == 'Bob'
10
 
24
Matching

Match each testing concept to its description.

Click an item on the left, then click its match on the right. Click a matched item to unmatch.

25
Matching

Match the unittest assertion with what it verifies.

Click an item on the left, then click its match on the right. Click a matched item to unmatch.

26
Fill in the Blanks

Complete the pytest fixture and test to ensure a temporary configuration dictionary is provided to the test.

python
1
import pytest
2
3
@pytest.fixture
4
def config():
5
return
6
7
def test_config_has_debug(config):
8
assert config['debug'] is

Click an option to fill blank 1:

27
Fill in the Blanks

Complete this doctest-enabled function so the example passes.

python
1
import doctest
2
3
def greet(name):
4
"""Return a greeting message.
5
6
>>> greet('Ada')
7
'Hello, Ada!'
8
"""
9
return + name +
10
11
if __name__ == '__main__':
12
doctest.testmod()

Click an option to fill blank 1:

28
Fill in the Blanks

Complete this TDD-style unittest that currently only has the test and a placeholder implementation.

python
1
import unittest
2
3
class TestIncrement(unittest.TestCase):
4
def test_increment(self):
5
self.assertEqual(increment(3), 4)
6
7
def increment(value):
8
9
10
if __name__ == '__main__':
11

Click an option to fill blank 1:

29
Fill in the Blanks

Complete this helper that will be used to improve coverage by exercising both branches.

python
1
def label_score(score):
2
if score >= 50:
3
return
4
return

Click an option to fill blank 1:

30
Hotspot Selection

Click the line that will cause a failing assertion in this unittest.

Click on the line to select.

python
1
import unittest
2
 
3
class TestMath(unittest.TestCase):
4
    def test_addition(self):
5
        result = 2 + 2
6
        self.assertEqual(result, 5)
7
 
8
suite = unittest.TestLoader().loadTestsFromTestCase(TestMath)
9
unittest.TextTestRunner().run(suite)
31
Hotspot Selection

Click the line that will raise an exception due to misuse of a mock.

Click on the line to select.

python
1
from unittest.mock import Mock
2
 
3
client = Mock()
4
client.get_user.return_value = {'id': 1}
5
 
6
user = client.get_user(1)
7
print(user['name'])

Premium Content

Subscribe to unlock full access to this content and more premium articles.