types of python testing

Types of Testing in Python

Python is a powerful language that allows you to create complex applications and websites. It also provides a robust system for testing your code. In this article, we will learn about the different types of testing in python that you can use to easily identify and fix bugs in your code.


Types of Testing in Python

There are 4 types of testing available in Python – Unit Testing, Feature Testing, Integration Testing & Performance Testing. Let us look at them one by one. In most cases, we use assert statement for checking values. It checks a variable against a value and returns True if they match. It throws an exception if it does not match.


1. Unit Testing

In this case, we basically test only a logical unit of our code. It is used to test if the internal flow of methods & data is correct, and that edge cases are handled properly. This is the most granular form of testing in Python.

Here is an example of the same.

def func():
    return 10

def test_func():
    assert func() == 10

First we define the function func(). Next we, call the function func() from test_func() designed for testing. We also check if the value returned is 10. This shows that the function call is working properly and returns values as expected. You can modify it as per your requirement, but it is the easiest way to test a function or logic or algorithm.


2. Feature Testing

In this case, we test the actual functionality of features. You can create a collection of unit tests for this purpose, or a single feature test also. Here is an example.

class NewEndpoint:
    def on_get(req, resp):
        resp.body = "Hello World"

def test_new_endpoint():
    result = simulate_get("/newendpoint")
    assert result.body = "Hello World"

In this case, we create a class NewEndPoint with a get function handler. We design a test_new_endpoint() function that sends a get request to this class, retrieves the response and checks if it is actually “Hello World”.


3. Integration Testing

Integration tests are used to test applications end to end. Even if new code is added to your application, the existing integration tests should work properly. Here is an example.

class MySystem:
    external_system = ExternalSystemConnector()
    def handle_message(message):
        try:
            external_system.send_message(message)
            return True
        catch Exception as err:
            return False
            
def test_MySystem():
    system = MySystem()
    assert system.handle_message(good_message)
    assert not system.handle_message(bad_message)

In this case, we create an entire instance of a class MySystem and call its function handle_message which connects with another system to execute its code. Only if everything goes well, it will return True, else it will return False.


4. Performance Testing

In this case, we are only checking the performance of a piece of code. Before we run performance tests, we should have ideally performed Unit & Feature Testing to ensure that it is working properly. Performance tests are basically calling the same function repeatedly over a given period of time to ensure that it does not crash the application. For this purpose, you can use Python libraries like timeit, as shown below.

import timeit

def func(i):
    return i * 2

def test_performance():
    assert 1 > timeit.timeit("[func(x) for x in range(20)]", number=5,
                              setup="from __main__ import func")

In the above case, we have defined a function func() that returns double the input value. We use timeit function to repeatedly call the func() function with various input values and test their output.

In this article, we have learnt several ways to perform testing in Python.

Also read:

How to Generate SSH Keys for Git Authorization
How to Download File in Django
How to Check If File Exists in Python
How to Allow MySQL User from Multiple Hosts
How to Check Python Package Dependencies

Leave a Reply

Your email address will not be published. Required fields are marked *