DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 966,904 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Cover image for Python Testing: Creating unit test in python
Jordan Williams
Jordan Williams

Posted on

Python Testing: Creating unit test in python

As developers or programmers, call yourself anyone. As long as you write code there is often one thing that gets overlooked. Testing. Testing is that part of development where you’re suppose to rigorously test your applications to ensure that they meet the required functionalities.

Let’s start of with why we even do testing in the first place:

  1. The need for less manual testing.Because the test cases are pre-written, there is no real reason to conduct manual testing anymore. Most times, especially with large applications you have to run through a lot of stages in the software to test if a certain feature works. This could be avoided with testing. Imagine having to use certain inputs every time your application runs. With testing this would be inputted automatically over each run.

  2. Allows for better structure of your code. It is of a best practice to create your test cases before development. This forces you to think of the system as a whole before you start developing.

  3. Allows for faster testing in long run. When coding, the implementation of a function can change however, the output remains the same. Instead of manually testing this every time a function’s implementation changes, you can simply implement specific test cases that can run multiple test cases at a time.

Monitor

Getting Started

We will be focusing on a specific type of testing and that is unit testing. This specific type of test is for testing small units of code such as a function or a class.

To create our unit test we will be using the python standard library unittest. This way of creating test cases uses OOP(Object Oriented Programming) by creating a class that will house all our test cases. This will allow us to run all the test cases in the class at once.

Note: You can use any IDE or text editor that your familiar with (default python IDE can be used). For this, I will be demonstrating using Visual Studio Code with a python extension.

We will first import our unit test library:

import unittest
Enter fullscreen mode Exit fullscreen mode

Now to make our class declaration:

class TestingClass(unittest.TestCase):
Enter fullscreen mode Exit fullscreen mode

Note that (unittest.TestCase) is inheritance in python. Essentially giving functionality to the class.

Inside the class add our first function:

def test_first(self):
    test_var = 9 + 1
    self.assertEqual(11,test_var)
Enter fullscreen mode Exit fullscreen mode

self.assertEqual is a method given by the previously inherited class (unittest.TestCase). This method tests if the 2 variables are equally of the same value.

Adding our test runner. This is what makes our unit test run:

unittest.main()
Enter fullscreen mode Exit fullscreen mode

This is what the completed code should look like:

Alt Text

The above code demonstrates testing if 9 + 1 is equal to 11. If you know any basic math you should know that 9+1 = 10. Hence this test case would fail.

Output:

Alt Text

As you should have already guessed. Failure!

The fix is simple. Modify the code to:

test_var = 9 + 2
Enter fullscreen mode Exit fullscreen mode

Alt Text

Output:

Alt Text

Testing Outside Functions

For testing outside of functions, the previous example might not be realistic. Let’s replace the test_var values to now come from a function. We will add a functional declaration to the top of our file.

def add(a,b):
    return a + b
Enter fullscreen mode Exit fullscreen mode

This function adds 2 numbers together. Replace 9 + 2 with the function call add(9,2) and then run your code.

Alt Text

We have only implemented one test case so far. Each function/method (both are the same thing in this case) in the TestingClass represents a single test case.

Lets add another test case called test_multiple_num_addition.

def test_multiple_num_addition(self):
    test_var = add(10,20,30,40) 
    self.assertEqual(100,test_var)
Enter fullscreen mode Exit fullscreen mode

Note: All the names of the test cases created should be preceded with the word β€œtest” or else it will not be recognized by the test runner.

Your code should look like the following below:

Alt Text

After running:

Alt Text

This will obviously fail because the function parameters only accept 2 arguments. But what if we really wanted to add more numbers?

This can be solved by doing a change to the add function:

def add(*values):
    return sum(values)
Enter fullscreen mode Exit fullscreen mode

Notice the asterisk (*) left of the values argument. This allows you to input multiple arguments as well as allowing the values to be stored as a tuple.

Your code before execution:

Alt Text

Your code while being executed:

Alt Text

And Success!

Putting an asterisk before your variable in the function parameters is called non keyword arguments. Want more information checkout the article below.

Summary

Giving testing a try might give your code additional robustness. It also has the benefit of changing the way you approach development. Having a testing mindset can ensure that there are less errors in production and less repetitive manual testing during development.

If you liked this article and want to support. Buy me a coffee. The link is provided below.

Buy Me A Coffee

Top comments (1)

Collapse
 
amrelhusseiny profile image
Amr ElHusseiny

Hello Jordan ,
thank you for the great post , I am a newbie on the pythonic way , can you please help me understand the right way to test if a script has a different output each time , so my simple script converts json into a nice CLI table , but in this case the output is different each time , how should I test that :)
, thank you in advance .

πŸ‘‹ New to DEV?

Head over to our Welcome Thread and tell us a bit about yourself!