Automatic tests in Python

The statistics examples – which are on GitHub – show three ways to add automatic tests in Python, each of them with a specific scope:

1. Unit Tests (white box)

White box is a method of testing software that tests internal structures or workings of a function/module/application, as opposed to its functionality (i.e. black-box testing, see point #2). Unit testing means to test individual pieces of the source code and is generally associated to test cases written by the developers themselves (who know the internal workings of the piece to be tested), often written before the source code.
In Python there is a unit testing framework available that allows to quickly set unit tests:

  • create a separate file that has a suffix  “_unittest” attached, for example: stats_unittest.py
  • In this file import unittest (and the module you want to test)
  • define a class inheriting from unittest.TestCase that will contain as methods all the unit tests.

The framework is inspired to JUnit so it provides several assert statements to compare actual and expected results, and to produce a final report. Continue reading “Automatic tests in Python”

Advertisements