• 10 hours
  • Medium

Free online content available in this course.

course.header.alt.is_video

course.header.alt.is_certifying

Got it!

Last updated on 8/30/22

Write Integration Tests

Let’s start with an official definition. The purpose of integration testing is to check how different combinations of code units interact with each other and how the subsystems work together as a common system.

For example, when we’re testing the login and signup features of a web application, we consider them to be separate units of code. If we check that we can log in after a user has signed up for an application, we’re checking that these two features are integrated. Obviously, these two separate features have been previously checked during unit testing.

What’s the difference between unit testing and integration testing?

Integration testing and unit testing are two different levels of software testing, where:

  • unit testing checks one unit of code, such as a method or a class.

  • integration testing checks a combination of several components, for example two classes or two distinct features that will work together.

Unit testing means checking smaller functional code elements separately. Integration testing goes one step further. We’re looking for errors that occur when units of code start to interact with each other. The fact that units of code have passed tests in a previous stage is no guarantee that they will work together correctly.

We know how code can be unpredictable, no matter how well it appears to have been written. Integration testing splits the code up into blocks consisting of a number of units so that parts of the software can be checked progressively before they are joined together to become a whole system.

Create an Integration Test Using Pytest

Setting up integration tests is going to seem like second nature to you, because just like we did with our unit testing, we’re going to use the Pytest framework. This framework gives us all the necessary tools we need to carry out our testing.

Let’s go back to our little OC-commerce project and use it to create our integration test. In this test, we’re going to check that the signup and login services are working together correctly. So, once a user has signed up, they should then be able to log in to the application.

First of all, you’re going to create a folder called  integration_tests  in the test folder structure.

Next, you’re going to create an initial test module containing all components relating to authentication. We’ll call this  test_authentication.py .

As we did for unit testing, we’re going to create our integration test using a function. Then you’ll use the client to carry out all the required actions on your application. You’ll then add assertions to check and validate the scenario.

import pytest

from pytest_django.asserts import assertTemplateUsed
from django.urls import reverse, resolve
from django.test import Client
from django.contrib import auth
from django.contrib.auth.views import LoginView

@pytest.mark.django_db
def test_login_route():

    client = Client()

#Register user using the ‘signup’ view and store details in the database
    credentials = {
        'first_name': 'Test',
        'last_name': 'User',
        'username': 'TestUser',
        'email': 'testuser@testing.com',
        'password1': 'TestPassword',
        'password2': 'TestPassword'
    }
    temp_user = client.post(reverse('signup'), credentials)

    #Log user in using the `login` view
    response = client.post(reverse('login'), {'username': 'TestUser', 'password': 'TestPassword'})

    #Check that page redirects to home page
    assert response.status_code == 302
    assert response.url == reverse('home')

    #Check that user has been authenticated
    user = auth.get_user(client)
    assert user.is_authenticated

Using the redirection and user authentication checks, we can confirm that the combination of these two functions passes the integration test.

Over to You!

Go back to the source code for the Django project and practice setting up some integration tests. Don’t forget to start by identifying the different components you want to test and flesh out your test plan with some integration tests.

Your Mission:

  • Add integration test scenarios to your test plan.

  • Create a sequence of tests for the whole project:

    • Redirect to the login page for all features that require a login session.

    • Display user profile to a logged-in user.

    • Add a product to favorites.

    • Delete a product from favorites.

    • Log a user out.

Find a suggested solution on GitHub!

Let’s Recap!

  • Integration testing determines if software components that were developed independently will work together correctly when they’re connected to each other. 

  • To create an integration test, we need to:

    • write a function containing the scenario.

    • use a client to perform actions on the program.

    • add assertions to check the test scenario.

Integration tests have allowed you to check how the different components of the application interact with each other. Next, let’s see how to test the user journey using functional testing.

Example of certificate of achievement
Example of certificate of achievement