0
0
PyTesttesting~10 mins

Docker-based test execution in PyTest - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test runs a simple pytest test inside a Docker container. It verifies that the test code executes correctly in the isolated Docker environment and the test passes.

Test Code - pytest
PyTest
import pytest

def test_addition():
    assert 2 + 3 == 5

# Dockerfile content:
# FROM python:3.12-slim
# WORKDIR /app
# COPY test_script.py /app/
# RUN pip install pytest
# CMD ["pytest", "test_script.py"]
Execution Trace - 4 Steps
StepActionSystem StateAssertionResult
1Docker daemon starts and builds the image from DockerfileDocker image with Python 3.12 and pytest installed is created-PASS
2Docker container starts from the built imageContainer running with working directory /app containing test_script.py-PASS
3pytest runs test_addition inside the containerpytest executes test and outputs resultassert 2 + 3 == 5 is TruePASS
4pytest finishes and container stopsTest passed, container exits cleanlyTest report shows 1 passed, 0 failedPASS
Failure Scenario
Failing Condition: Test assertion fails inside the Docker container
Execution Trace Quiz - 3 Questions
Test your understanding
What ensures the test runs in an isolated environment?
ARunning pytest directly on the host machine
BRunning pytest inside a Docker container
CUsing assert statements in the test
DInstalling pytest globally on the host
Key Result
Running tests inside Docker containers ensures a consistent environment, avoiding differences between developer machines and CI servers.