Integration testing with pytest

Introduction to Testing in Python

Alexander Levin

Data Scientist

What is integration testing?

  • Integration testing - software testing method, that allows to verify that an interaction behaves normally.

  • Integration - an interaction between 2 or more modules inside of a system.

diagram of integration example

Introduction to Testing in Python

Integrations in real-life projects

Examples:

  • Power cable
  • Internet connection
  • File reading driver
  • Database connection
  • Application Programming Interface (API) integration
Introduction to Testing in Python

What can go wrong

Potential integration problems:

  • Lost connection
  • Loss of data
  • Interaction delays
  • Low bandwidth
  • Version conflicts
  • Interface mismatch
  • Others
Introduction to Testing in Python

Example of integration testing

import pytest, os

@pytest.fixture
def setup_file():
    # Create temporary file
    file = "test_file.txt"
    with open(file, "w") as f1:
        f1.write("Test data 1")
    yield file
    os.remove(file)

def test_fs(setup_file):
    file = setup_file
    # Check that the file was created successfully
    assert os.path.exists(file)
Introduction to Testing in Python

Summary

  • Integration testing - software testing method; allows to verify that an integration works as expected.

  • Real-life projects include a lot of different integrations in them.

  • Integration testing helps to prevent lots of potential problems.

  • Example: checking integration between Python and a file system.

Introduction to Testing in Python

Let's practice!

Introduction to Testing in Python

Preparing Video For Download...