Last Updated: December 6, 2025
31 quizzes
Complete the code so the test checks that the value is exactly 10 using a unittest assertion
self.(result, 10)Click an option to fill the blank:
Complete the code to mark this test so that pytest expects a ValueError from risky_divide
with pytest.(ValueError):Click an option to fill the blank:
Complete the code so that mock_client.get is replaced by a mock object during this test
with patch('service_client.') as fake_get:Click an option to fill the blank:
Which statement best describes unit testing?
In unittest, which method name will be automatically collected as a test?
With pytest, which file name will be automatically discovered as a test module?
What is the main benefit of using fixtures in pytest?
Which unittest assertion is most appropriate for checking that a function raises a TypeError?
In pytest, how do you mark a test as expected to fail but still run it?
What is a common reason to use mocking in tests?
Which coverage metric tells you what percentage of executable lines ran during tests?
What is a key feature of doctest?
In TDD's Red-Green-Refactor cycle, what happens in the Green step?
When using coverage.py with pytest, which command both runs tests and measures coverage?
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.
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.
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())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)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)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'])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))Find the bug in this unittest setup method that prepares data for multiple tests.
Click on the line(s) that contain the bug.
import unittest class TestData(unittest.TestCase): def setUp(self): shared_list = [] shared_list.append('item') def test_length(self): self.assertEqual(len(self.shared_list), 1) Find the bug in this code that attempts to use pytest fixtures.
Click on the line(s) that contain the bug.
import pytest @pytest.fixturedef user_data(): return {'name': 'Bob'} class TestUser: def test_name(self): assert user_data['name'] == 'Bob' 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.
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.
Complete the pytest fixture and test to ensure a temporary configuration dictionary is provided to the test.
import pytest@pytest.fixturedef config(): return def test_config_has_debug(config): assert config['debug'] is Click an option to fill blank 1:
Complete this doctest-enabled function so the example passes.
import doctestdef greet(name): """Return a greeting message. >>> greet('Ada') 'Hello, Ada!' """ return + name + if __name__ == '__main__': doctest.testmod()Click an option to fill blank 1:
Complete this TDD-style unittest that currently only has the test and a placeholder implementation.
import unittestclass TestIncrement(unittest.TestCase): def test_increment(self): self.assertEqual(increment(3), 4)def increment(value): if __name__ == '__main__': Click an option to fill blank 1:
Complete this helper that will be used to improve coverage by exercising both branches.
def label_score(score): if score >= 50: return return Click an option to fill blank 1:
Click the line that will cause a failing assertion in this unittest.
Click on the line to select.
import unittest class TestMath(unittest.TestCase): def test_addition(self): result = 2 + 2 self.assertEqual(result, 5) suite = unittest.TestLoader().loadTestsFromTestCase(TestMath)unittest.TextTestRunner().run(suite)Click the line that will raise an exception due to misuse of a mock.
Click on the line to select.
from unittest.mock import Mock client = Mock()client.get_user.return_value = {'id': 1} user = client.get_user(1)print(user['name'])