Questions
General
- How do you create a test function? What identifies the function as a test?
- How do you create a file to write all your unit tests?
- What does the assert statement do?
- Explain the difference between a use case and an edge case. Give an example of both within a function.
Unit Test Writing
- Suppose you have the following function, designed to return the index of the first even number in a list.
def find_even(nums: list[int]) -> int:
int = 0
idx: while idx < len(nums):
if nums[idx] % 2 == 0:
return idx
+= 1
idx return -1
Fill in this unit test with a use case.
def test_find_even_use_case() -> None:
"""Put code here."""
- Suppose you have the following function, designed to calculate the sum of the elements in a list.
def sum_numbers(numbers: list[int]) -> int:
if len(numbers) == 0:
raise Exception("Empty list - no elements to add")
int = numbers[0]
total: for i in range(1, len(numbers)):
+= numbers[i]
total
return total
Fill in this unit test with a use case.
def test_list_sum_use_case() -> None:
"""Put code here."""
- Suppose you have the following function, designed to determine if a number is prime.
def is_prime(n: int) -> bool:
if n < 2:
return False
for i in range(2, int(n ** 0.5) + 1):
if n % i == 0:
return False
return True
Fill in this unit test with a use case.
def test_is_prime_use_case() -> None:
"""Put code here."""
- Suppose you want to test that a list of dictionaries will be mutated correctly. Here’s a function that mutates a list of dictionaries by adding a new key-value pair to each dictionary in the list.
def add_key_to_dicts(dicts: list[dict], key: str, value: int) -> None:
for d in dicts:
= value d[key]
Fill in this unit test with a use case to verify that the list of dictionaries is mutated correctly.
def test_add_key_to_dicts_use_case() -> None:
"""Put code here."""
- Suppose you want to test that a dictionary will be mutated correctly. Here’s a function that mutates a dictionary by incrementing the value of a given key by 1.
def increment_dict_value(d: dict[str, int], key: str) -> None:
if key in d:
+= 1
d[key] else:
= 1 d[key]
Fill in this unit test with a use case to verify that the dictionary is mutated correctly.
def test_increment_dict_value_use_case() -> None:
"""Put code here."""
- Suppose you have the following function, designed to sum the elements in a dictionary of list values and return the key with the largest summed value.
def max_sum_dict(d: dict[str, list[int]]) -> str:
= []
keys for key in d:
keys.append(key)
= d[keys[0]]
values_list_1 = d[keys[1]]
values_list_2
= 0
total_1 for value in values_list_1:
+= value
total_1
= 0
total_2 for value in values_list_2:
+= value
total_2
if total_1 > total_2:
return keys[0]
else:
return keys[1]
Fill in this unit test with a use case to verify that the function returns the key with the largest summed value.
def test_max_sum_dict_use_case() -> None:
"""Put code here."""
Solutions
Conceptual Solutions
A test function is created just like any other Python function, but it is identified as a test by starting its name with
test_
. In frameworks like pytest, any function that starts withtest_
is automatically detected and run as a test.Create a new Python file, often named
<module_name>_test.py
, in the same directory as your module. Write all your test functions in this file.The assert statement checks if a condition is true. If the condition is false, an AssertionError is raised, indicating that the test has failed.
A use case is a typical scenario where the function is expected to work as intended. For example, in a function that sums a list, a use case would be passing a list like
[1, 2, 3]
. An edge case is a situation where the function might struggle or behave differently, like passing an empty list[]
to a sum function.
Unit Test Writing
- Solution below:
def test_find_even_use_case() -> None:
= [1, 3, 5, 4, 7]
nums assert find_even(nums) == 3
- Solution below:
def test_list_sum_use_case() -> None:
# Test case 1: Normal list of positive numbers
assert sum_numbers([1, 2, 3, 4, 5]) == 15
# Test case 2: List with negative numbers
assert sum_numbers([-1, -2, -3, -4, -5]) == -15
# Test case 3: Mixed positive and negative numbers
assert sum_numbers([1, -1, 2, -2, 3, -3]) == 0
# Test case 4: List with a single element
assert sum_numbers([10]) == 10
# Do not worry about handling the exception!
# That is out of the scope of the class :)
- Solution below:
def test_is_prime_use_case() -> None:
assert is_prime(7) is True
assert is_prime(8) is False
- Solution below:
def test_add_key_to_dicts_use_case() -> None:
= [{"a": 1}, {"b": 2}]
dicts "c", 3)
add_key_to_dicts(dicts, assert dicts == [{"a": 1, "c": 3}, {"b": 2, "c": 3}]
- Solution below:
def test_increment_dict_value_use_case() -> None:
= {"a": 1, "b": 2}
d "a")
increment_dict_value(d, assert d["a"] == 2
"c")
increment_dict_value(d, assert d["c"] == 1
- Solution below:
def test_max_sum_dict_use_case() -> None:
= {"a": [1, 2, 3], "b": [4, 5]}
d assert max_sum_dict(d) == "b"