Integrationstestning med pytest

Introduktion till testning i Python

Alexander Levin

Data Scientist

Vad är integrationstestning?

  • Integrationstestning – en testmetod som verifierar att en interaktion fungerar korrekt.

  • Integration – en interaktion mellan två eller fler moduler i ett system.

diagram över integrationsexempel

Introduktion till testning i Python

Integrationer i verkliga projekt

Exempel:

  • Strömkabel
  • Internetanslutning
  • Filläsningsdrivrutin
  • Databasanslutning
  • API-integration
Introduktion till testning i Python

Vad kan gå fel

Möjliga integrationsproblem:

  • Förlorad anslutning
  • Dataförlust
  • Interaktionsfördröjningar
  • Låg bandbredd
  • Versionskonflikter
  • Gränssnittsfel
  • Övrigt
Introduktion till testning i Python

Exempel på integrationstestning

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)
Introduktion till testning i Python

Sammanfattning

  • Integrationstestning – en testmetod som verifierar att en integration fungerar som förväntat.

  • Verkliga projekt innehåller många olika integrationer.

  • Integrationstestning hjälper till att förebygga många potentiella problem.

  • Exempel: kontrollera integrationen mellan Python och ett filsystem.

Introduktion till testning i Python

Nu kör vi en övning!

Introduktion till testning i Python

Preparing Video For Download...