Python Notes and Examples

Testing

To use PyTest, install via apt install python3-pytest.

Pytest finds and runs test_ functions in your source files. Put your tests into “test_{module-name}.py” files. You write assert statements in them.

Example, in the my-proj directory:

~/dev/my-proj$ tree
.
├── COPYING
├── my_proj/
│   ├── stuff.py
│   └── test_stuff.py
├── my-proj.py*
└── README.md

my_proj/stuff.py looks like:

def f(x):
    return 3 * x

and test_stuff.py looks like:

import stuff

def test_f():
    assert stuff.f(3) == 9

def test_f_2():
    assert stuff.f('hey') == 'heyheyhey'

Run all test from the top-level of the project directory like so:

~/dev/my-proj$ pytest-3