Pytest is a very popular and simple testing framework for running unit tests, integration tests and e2e tests in Python projects.
Usage
The convention is to make a separate tests
directory in your project root directory, then make test files with the name test_*.py
. Inside these files, you just define functions and directly make assertions with the built-in Python assert
. That’s it (for simple tests).
Run the pytest
CLI to execute tests:
Exceptions Tests
To test that a function correctly raises an exception, use with pytest.raises(ExceptionClassName)
: