Getting Started with unittest in Python

Ranvir Chhina
Tauk Blog
Published in
3 min readApr 22, 2022

To ensure that all code meets the quality standards and functional requirements, writing unit tests to verify code behavior in isolation is necessary. Python provides a unit testing framework in its standard library called unittest. With this framework, the developer not only has control over the definition of the test case but also is able to control the corresponding test setup and cleanup actions.

In this short guide, we are going to write a Python Unit test using unittest and run it using the command line.

Test Fixture

Each unit test can be created as method for a class which extends unittest.TestCase. The method’s name should be prefixed with test_ to ensure the the Test Runner recognizes it as a test case. The subclasses of unittest.TestCase have access to the following methods:

  • setUp(): This method allows you to reduce code repetition across your test methods by allowing you to add the common setup action. This method is run before each test case.
  • tearDown(): Similar to the setUp() method, this method allows the user to add cleanup actions for all the test cases and is run after each test case execution. One important thing to note is that this method will only be called if the setUp() doesn’t raise any exceptions.

In this code snippet, we have created an example of an AppiumTestCase class which extends unittest.TestCase and has its own setUp() and tearDown() methods. In setUp() method, we create the Appium driver. In tearDown() method, we access the same instance of Appium driver and quit the driver.

Asserts

In each test case, we add some kind of assert which determines if the test passed or failed. If an exception is raised within the test case, the test is considered a failure. There are quite a few asserts available from the unittest framework but I have selected a few and written about them here.

  • assert(a, b): Tests whether a and b are equal. If the comparison does not hold true, the assert fails the test case.
  • assertIsNotNone(x): Verifies whether the expression x is equal to None or not. It passes the test case if the expression is not None.
  • assertRaises(exc, fun): This assert verifies that the given exception exc is raised when the callable fun is executed. In addition to a normal approach, this assertion is also supported by the python with() statement.

Writing the Test Case

By using the above API, we can create a test case as follows:

In the code snippet above:

  • unittest is imported to make its API available inside our test module.
  • unittest.TestCase is extended by AppiumTestCase so that it is recognized by unittest as a Test Class.
  • We define the Test Fixture by adding the driver creation in the setUp() method and the driver cleanup in the tearDown() method.
  • We write test cases that call various asserts to ensure the tested method has the correct behavior.
  • We define the entry point to the test case by calling unittest.main() which allows us to run test cases using the command line.

Running the Test Case

The Test can be run using the command line commands available using the unittest framework. To run the AppiumTestCase class, we will need to save it in a module, say, appium_test_case.py. Then, we can run it using the following command.

Integrate with Tauk

Tauk with its intuitive and usable interface allows developers to test their application before releasing to the app store. You can integrate the above suite with Tauk by installing the python package and adding the following lines.

Installing the Python Package

You can install the python package using pip.

Modifying Test Suite

You can integrate your existing test suite with Tauk by

  • importing the @Tauk.observe() decorator, and
  • registering your driver with @Tauk.register_driver() .

Conclusion

This short guide introduces the reader to the unittest framework. It shows how to write test cases and run them using the command line. In the next blog post, we will cover how to run test cases in concurrence to make test execution an efficient process.

Lastly, if you have any questions on this blog post, you can reach out to us by joining our Discord server:

--

--