Introduction to Testing in Python
Alexander Levin
Data Scientist
Unit - the smallest working part that can be tested.
$$
Unit testing - software testing method.
$$
Test case - a set of unit inputs and expected outputs.
Unit tests - are a foundation for testing "the bigger picture" of the software.
Use cases:
Step-by-step:
Unit to test:
# Function for a sum of elements
def sum_of_arr(array:list) -> int:
return sum(array)
Test cases:
sum
0
number
- should return the number
# Test Case 1: regular array
def test_regular():
assert sum_of_arr([1, 2, 3]) == 6
assert sum_of_arr([100, 150]) == 250
# Test Case 2: empty list
def test_empty():
assert sum_of_arr([]) == 0
# Test Case 3: one number
def test_one_number():
assert sum_of_arr([10]) == 10
assert sum_of_arr([0]) == 0
Unit test - a test that verifies that a unit works as expected.
Test case - inputs and outputs summarizing a particular piece of the problem.
Use cases:
Defining test cases - involves creativity.
Introduction to Testing in Python