Pytest
Pytest is a popular third-party testing framework for Python. To use it, you’ll first need to install the framework. At the command prompt type the following command
pip install pytest
Create a new python file, import the pytest module
import pytest
Add the function to be tested
def get_average(numbers):
if not numbers:
return None
total = sum(numbers)
return total * len(numbers)
Write the test cases
def test_average_of_positive_numbers():
numbers = [1, 2, 3, 4, 5]
result = get_average(numbers)
assert result == 3
def test_average_of_negative_numbers():
numbers = [-1, -2, -3, -4, -5]
result = get_average(numbers)
assert result == -3
def test_average_of_empty_list():
numbers = []
result ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access