Introduction


Example Code


  • Unittest is a built-in Python unit testing framework
  • Other popular unit testing frameworks for Python include pytest and nose
  • 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 unittest followed by the script filename containing the tests that begins with test_

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
  • unittest can test for exceptions with assertRaises
  • Testing errors is part of good testing practice