Introduction
Example Code
- Unittest is a built-in Python unit testing framework
- Other popular unit testing frameworks for Python include
pytestandnose - Object oriented programming is a way of encapsulating data and the functions that operate on that data together
- Run a set of Unittest unit tests using
python -m unittestfollowed by the script filename containing the tests that begins withtest_
Creating a New Test
- Unit tests check that functions behave as expected
- Re-running tests ensures code changes don’t break behaviour
- Use edge cases and different paths through your code to create effective tests
Handling Errors
- Code should validate their inputs and raise clear exceptions when needed
-
unittestcan test for exceptions withassertRaises - Testing errors is part of good testing practice