Unit Testing in Python for Beginners

Pratik Pandab
Analytics Vidhya
Published in
4 min readMay 13, 2020
Photo by David Travis on Unsplash

One of the constant source of argument between a Data Science team & the Software Engineering team in most organizations revolves around proper testing of the code before taking it towards production as for some reason, most Data Scientists tend to overlook testing while deriving insights from the data.

And make no mistake, the problem is real. Untested code could be loaded with bugs, bad encoding, bias, etc. Which in turn leads to a bunch of consequences like high variability in results, crash in the production environment and so on. Thus, it is advisable for everyone to incorporate proper testing to ease the workflow.

Photo by Markus Spiske on Unsplash

There is a long list of types of testing technique being used in software development life-cycle each for a specific phase. However, I would start from the most fundamental type i.e. Unit Testing.

Unit tests are tests designed for independent sections of the code providing some functionality (known as units) for example, a function in the code.

Testing Tools in Python

Pytest: A fully-featured, easy-to-use testing tool with a no boilerplate code required policy, great readability and the list go on. Obviously, the best choice to get started with.

Installation: pip install -U pytest

Usage Example:

Consider a function that returns the list of factors for a given number in the file named as factors.py:

factors.py

Now we’ll write some tests in a file named test_factors.py in the same directory as the unit code file named factors.py to find a flaw:

test_factors.py

The assert statement just checks the condition & throws an AssertionError if it is False. I have used set () function because the set is an unordered data structure so order of elements in the factors list won’t make the tests to fail.

Note: pytest has this default configuration to name the test file starting with a “test_” and also, all the testing functions in this file should be named starting with a “test_” as seen in the above image.

And we’re DONE!

To run the tests type pytest on the command line while being in the directory containing the unit code file and the testing script.

Results

Here you would notice a line: test_factors.py .F

Every dot after test_factors.py signifies a successful test & the F represents a failed test with the details provided below. In our case, the second test failed because the code was designed to output factors of positive integers only and our second test input was a negative integer(see test_factors.py).

After updating the code(factors.py) to handle negative numbers as well:

Updated factors.py

On running the tests with pytest command: BINGO!

Tests passed

Unittest: An out-of-the-box ready to use testing library in python, has a similarity with the testing frameworks in other languages like JUnit. Some of the key features available like testing automation, independent tests could be performed, and so on.

Installation: As it is a standard library in python, so, no need to explicitly install it.

Usage Example:

Updated test_factors with unittest

For testing with unittest library, this standard boilerplate code should be used. Here, the tests are defined as methods of the testing module with assertEqual() being analogous to the assert() function used earlier with pytest.

unittest Results

To run all the tests at once use the following command format:

python -m unittest -v test_script

Here -v tag is used for verbosity i.e. To provide details of the tests ran.

To run individual tests use the following command:

python -m unittest -v test_script.test_class.test_method

So, That’s all for this post. I would be covering some other testing topics in my upcoming posts. So Stay Tuned.

--

--

Pratik Pandab
Analytics Vidhya

Data Scientist | Writing on Machine Learning, Statistics and some more