Python 3 cheatsheet

Python 3: Testing and tools

6 common Python 3 tasks.

Run tests with pytest

Run every test found under the current folder. Needs pytest installed.

python -m pytest

Run one test

Run a single test function and show print output.

python -m pytest tests/test_app.py::test_login -s

Run unittest tests

Find and run tests using the standard library.

python -m unittest discover

Debug a script

Run a script under the pdb debugger.

python -m pdb script.py

Time a snippet

Measure how long a statement takes.

python -m timeit "sum(range(1000))"

Profile a script

Show which functions take the most time.

python -m cProfile -s cumtime script.py

Was this page helpful?