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.
Jump into concepts and practice - no test required
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.
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"]
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Docker daemon starts and builds the image from Dockerfile | Docker image with Python 3.12 and pytest installed is created | — | PASS |
| 2 | Docker container starts from the built image | Container running with working directory /app containing test_script.py | — | PASS |
| 3 | pytest runs test_addition inside the container | pytest executes test and outputs result | assert 2 + 3 == 5 is True | PASS |
| 4 | pytest finishes and container stops | Test passed, container exits cleanly | Test report shows 1 passed, 0 failed | PASS |
-v $(pwd):/app option shares the current directory to /app inside the container.-w /app sets the working directory, and pytest runs tests there.docker run --rm -v $(pwd):/tests -w /tests python:3.12 pytest test_sample.py
test_sample.py contains one passing and one failing test?docker run -v $(pwd):/app python:3.12 pytest
-w /app, pytest runs in root, not where tests are mounted.--junitxml=/app/report.xml to save report inside mounted folder, making it visible on host.