avatarYang Zhou

Summary

This content provides an introduction to unit testing in Python, emphasizing its importance in the Test-Driven Development (TDD) process and demonstrating how to write unit tests using Python's built-in unittest module.

Abstract

The content discusses the importance of unit testing in the software development process, focusing on testing individual units such as functions, classes, or modules. It provides an overview of the unit testing methodology, which involves designing test cases with different inputs and expected results, then running the function and verifying the results.

The article then moves on to the practical demonstration of unit testing using Python's built-in unittest module. The author shows how to write a test class that inherits from unittest.TestCase and how to structure test methods using the naming convention test_xxx(). The built-in assertion methods provided by unittest.TestCase, such as assertEqual(), are also discussed.

The author then provides a tip for running unit test scripts using the command python -m unittest, which allows for the batch execution of multiple tests. The article concludes by emphasizing the value of testing in software development and the role of unit testing in writing robust code.

Bullet points

  • Unit testing is a key part of Test-Driven Development (TDD) and is used to test the correctness of individual units such as functions, classes, or modules.
  • The method for testing a function involves designing different inputs and expected results, running the function with these inputs, and verifying the results.
  • Python's built-in unittest module simplifies the process of writing unit tests by allowing developers to write test cases once and run them repeatedly.
  • When writing unit tests, a test class inheriting from unittest.TestCase should be created, and test methods should be named test_xxx().
  • The unittest.TestCase class provides built-in assertion methods, such as assertEqual(), to help verify the results of the tests.
  • Running unit test scripts can be done using the command python -m unittest, which allows for batch execution of multiple tests.
  • Testing is a crucial aspect of software development, and unit testing plays a significant role in writing robust code.

Unit Testing in Python

Photo by Nicolas Thomas on Unsplash

Introduction

Unit testing is an important part of “Test-Driven Development (TDD)”. It’s used to test the correctness of a module, a function or a class.

Let’s start from a question: how to test a function works correctly or not?

The method is simple:

  • Design some different inputs for the function, and expected results.
  • Run the function using the inputs and check out whether it will return the expected results.

For example, we wrote a function abs(), which is used to find the absolute value of a number, then we can design some inputs and outputs as test cases:

  • Enter a positive number, such as 99, 3.14, and expect the return value to be the same as the input: 99, 3.14.
  • Enter a negative number, such as -10, -6.28, and expect the return value to be opposite to the input: 10, 6.28
  • Enter 0 , expect to return 0 .

If the function passes all the test cases, it works as expected and no bugs found. If not, we should start debugging.

The problem is, every time testing the function, we need to enter the inputs of the test cases manually. Even if we just modified the function a little bit, we have to enter every case and see the results again. It is such a boring job.

Are there better methods?

Definitely! Python, as an elegant programming language, never lets developers down.

The Python’s built-in module unittest gives us the convenience to just write the test cases once and run them again and again.

How to Write an Unit Test?

Let’s take the simple function abs() mentioned above as an example to see how to write a unit test by the powerful unittest module. The abs() function is as following:

Then we write an unit test script:

Finally, we run the test script, and get the following result:

Result of Unit Test

Perfect, our abs() function passed tests! 👌

More Details of The Unit Test Class

As the above example shown, when writing unit tests, we need to write a test class that inherits from the unittest.TestCase. In this test class, methods that start with test_ are test methods. Methods that do not start with test_ are not considered test methods by Python and will not be executed during testing.

In our scenario, we need three types of tests, so there are three different test_xxx() methods. It’s a good habit to categorize your test cases to keep your mind clear especially when there are many test cases.

Since unittest.TestCase provides many built-in judgment methods, we only need to call them to check whether the outputs are as expected. The most commonly used assertion is assertEqual() . Others are:

Table form Python unittest document.

A Small but Useful Tip

There is a tip based on my experience.

As we known, the unit test script can be runned directly:

$ python unittest_abs.py

It’s because we have the following code in the unittest_abs.py:

if __name__ == '__main__':
    unittest.main()

Another way to run our unit test script without writing the above code is:

$ python -m unittest unittest_abs.py
.....
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK

This is the recommended style because it allows us to run many unit tests in batches at once, and many other tools can automatically run these unit tests.

Conclusion

Testing is a big topic and a significant step for software development. The unit test is one of the testing methods by which an individual unit of the program, such as a function, class or module, is put under various test cases to determine whether they work as expected. It helps us write robust code.

Thanks for reading. Please follow me and get more Python tutorials here:

Programming
Python
Testing
Test Driven Development
Python3
Recommended from ReadMedium