Unittest

unittest supports you in test automation with shared setup and tear-down code as well as aggregation and independence of tests.

It provides the following test concepts:

Test Case

tests a single scenario.

Test Fixture

is a consistent test environment.

Test Suite

is a collection of several test cases.

Test Runner

runs through a Test Suite and displays the results.

Example

Suppose you have implemented the following add method in the test_arithmetic.py module:

1def add(x, y):
2    """
3    >>> add(7,6)
4    13
5    """
6    return x + y

… then you can test this method with a Unittest.

  1. To do this, you must first import your module and the unittest module:

    1import unittest
    2class TestArithmetic(unittest.TestCase):
    
  2. Then you can write a test method that illustrates your addition method:

    6class TestArithmetic(unittest.TestCase):
    7    def test_addition(self):
    8        self.assertEqual(arithmetic.add(7, 6), 13)
    9
    
  3. In order to import the unittests into other modules, you should add the following lines:

    23if __name__ == "__main__":
    24    unittest.main()
    
  4. Finally, all tests in test_arithmetic.py can be executed:

    $ python test_arithmetic.py
    ....
    ----------------------------------------------------------------------
    Ran 4 tests in 0.000s
    
    OK
    
    C:> python test_arithmetic.py
    ....
    ----------------------------------------------------------------------
    Ran 4 tests in 0.000s
    
    OK
    

    … or a little more detailed:

    $ python test_arithmetic.py -v
    test_addition (__main__.TestArithmetic) ... ok
    test_division (__main__.TestArithmetic) ... ok
    test_multiplication (__main__.TestArithmetic) ... ok
    test_subtraction (__main__.TestArithmetic) ... ok
    
    ----------------------------------------------------------------------
    Ran 4 tests in 0.000s
    
    OK
    
    C:> python test_arithmetic.py -v
    test_addition (__main__.TestArithmetic) ... ok
    test_division (__main__.TestArithmetic) ... ok
    test_multiplication (__main__.TestArithmetic) ... ok
    test_subtraction (__main__.TestArithmetic) ... ok
    
    ----------------------------------------------------------------------
    Ran 4 tests in 0.000s
    
    OK
    

Example: Testing an SQLite database

  1. To test whether the database library.db was created with create_db.py, we import ../save-data/sqlite/create_db.py and os in addition to sqlite3 and unittest:

    1import os
    2import sqlite3
    3import unittest
    4
    5import create_db
    
  2. Then we first define a test class TestCreateDB:

    8class TestCreateDB(unittest.TestCase):
    
  3. In it we then define the test method test_db_exists, in which we use assert to assume that the file exists in os.path:

     9    def test_db_exists(self):
    10        assert os.path.exists("library.db")
    
  4. Now we also check whether the books table was created. For this we try to create the table again and expect with assertRaises that sqlite is terminated with an OperationalError:

    12    def test_table_exists(self):
    13        with self.assertRaises(sqlite3.OperationalError):
    14            create_db.cursor.execute("CREATE TABLE books(title text)")
    
  5. We do not want to carry out further tests on a database in the file system but in an SQLite database in the working memory:

    17class TestCommands(unittest.TestCase):
    18    def setUp(self):
    19        self.conn = sqlite3.connect(":memory:")
    20        cursor = self.conn.cursor()
    

See also

You can find more examples for testing your SQLite database functions in the SQLite test suite test_sqlite3.