A quick look at the unittest module
Python comes with the built-in unittest
module, which is a framework to write automated tests based on JUnit, a unit testing framework for Java. You create tests by subclassing from unittest.TestCase
and defining methods that begin with test
. Here's an example of a typical minimal test case using unittest
:
import unittest from fibo import fibonacci class Test(unittest.TestCase): def test_fibo(self): result = fibonacci(4) self.assertEqual(result, 3) if __name__ == '__main__': unittest.main()
The focus of this example is on showcasing the test itself, not the code being tested, so we will be using a simple fibonacci
function. The Fibonacci sequence is an infinite sequence of positive integers where the next number in the sequence is found by summing up the two previous numbers. Here are the first 11 numbers:
1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
Our fibonacci
function receives an index
of the...