Setting up py.test
suggest changepy.test is one of several third party testing libraries that are available for Python. It can be installed using pip with
pip install pytest
The Code to Test
Say we are testing an addition function in projectroot/module/code.py:
# projectroot/module/code.py
def add(a, b):
return a + b
The Testing Code
We create a test file in projectroot/tests/test_code.py. By default, pytest collects files named test_*.py or *_test.py.
# projectroot/tests/test_code.py
from module import code
def test_add():
assert code.add(1, 2) == 3
Running The Test
From projectroot we simply run py.test.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents