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).
1
2
deftest_foo():assert(do_foo()==...)
Run the pytest CLI to execute tests:
1
2
3
4
5
6
7
pytest # Discover and run all tests.pytest -s # Show output from the tests.pytest -vv # Show verbose differences between expected and actual.pytest test_file.py # Run tests in only the specified file.pytest test_file.py::TestClass # Run tests in only the specified class.pytest test_file.py::test_func # Run only the selected test.
To test that a function correctly raises an exception, use with pytest.raises(ExceptionClassName):
1
2
3
4
5
6
importpytestdeftest_foo_raises_bar():withpytest.raises(BarException):# If this doesn't raise a `BarException`, then the test is considered to have failed.do_foo()