Introduction to Python unit testing

Python has built-in support for unit tests, with the unittest module of the standard library. This module contains all the features needed to easily write unit tests. However, running tests directly with this module is cumbersome. Some other tools allow us to simplify this step. In this chapter, the nosetests tool will be used. It is available in PyPI, and can be installed in the following way:

(venv-rx) $ pip3 install nose

Okay, now let's see how to write and execute a very simple test, before testing some ReactiveX code. First, the TestCase class must be imported:

from unittest import TestCase

A test case is a group of tests. The usual way to use them is to write one test case per class or feature. A new test case is defined by declaring a new class that will inherit from the TestCase class, as can be seen in the following example:

class MyTestCase(TestCase):

def test_example(self):
self.assertEqual(3, 1 + 1)

In this example, the MyTestCase test case is defined. It contains only one test: test_example. All tests must have their names prefixed with test_. Each test_ method in a test case is the implementation of a test. A test case can have many checks in it. In this example, the test_example checks that 3 is equal to 1 + 1 (which is wrong).

Checking for a test result is done with assertions. In this example the assertEqual assertion is used. This assertion checks that the two parameters are equal. If they are equal, then the assertion passes; otherwise, the test fails. If all assertions of a test pass, then the test passes.

This example is available in the GitHub repository (https://github.com/PacktPublishing/Hands-On-Reactive-Programming-with-Python) of this book, in the test_base.py script.

Running this test is done as follows:

(venv-rx) $ nosetests test_base.py 

It gives the following result:

F
======================================================================
FAIL: test_example (test_base.MyTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/xxx/Hands-On-Reactive-Programming-with-Python/ch10/test_base.py", line 7, in test_example
self.assertEqual(3, 1 + 1)
AssertionError: 3 != 2

----------------------------------------------------------------------
Ran 1 test in 0.001s

FAILED (failures=1)

The first F letter being printed means that a test has failed. When several tests are executed, then each passed test is printed with a dot. Then the reason for the failure is printed, with the line that caused the failure. In this case, the error is that 3 is different from 2. With the error being fixed, the test passes.

Using assertions allows us to create tests that are self-validating. Many assertions are available, covering all needs. The main ones are detailed here:

  • assertEqual(a, b)/assertNotEqual(a, b): Checks that two objects are equal or different, by using the equality operator
  • assertTrue(a)/assertFalse(a): Checks that the provided object is true or false
  • assertIs(a, b)/assertIsNot(a, b): Checks that object a is of type b, or not of type b
  • assertIsNone(a)/assertIsNotNone(a): Checks that the value of a is none, or not
  • assertIn(a, b)/assertNotIn(a, b): Checks that a is in b (with the Python In operator), or not in b
  • assertIsInstance(a, b)/assertNotIsInstance(a, b): Checks that a is an instance of b, or not an instance of b
  • assertRaises(exc, fun, *args, **kwds): Checks that the execution of fun with args and kwds as parameters raises the exc exception
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.14.98