PyTest
The simplest way of testing is to use pytest.
Writing tests
Pytest automatically runs functions whose names begin with test. Use
assert to check that values are what you expect.
## double.py
def double(x): return 2*x
def test_double():
assert 10 == double(5)
assert 90 == double(45)
Running tests
There are two ways to run your tests: manually or automatically.
Method 1: Manually Run
Open the console and type pytest main.py. All of your test cases in
main.py will run.
Method 2: Automatically Run
-
Create a separate file called
test_something.pythat contains your testing code. The"test_"part of the filename is required. -
In the main file, run the pytest main method:
## main.py - automatically run when you hit "run"
import pytest
pytest.main()