Docker-based test execution in PyTest - Build an Automation Script
Start learning this pattern below
Jump into concepts and practice - no test required
import subprocess import sys def test_run_pytest_in_docker(): # Build the Docker image build_cmd = [ "docker", "build", "-t", "pytest-test-image", "." ] build_process = subprocess.run(build_cmd, capture_output=True, text=True) assert build_process.returncode == 0, f"Docker build failed: {build_process.stderr}" # Run the Docker container to execute pytest run_cmd = [ "docker", "run", "--rm", "pytest-test-image" ] run_process = subprocess.run(run_cmd, capture_output=True, text=True) print(run_process.stdout) assert run_process.returncode == 0, f"Pytest failed inside Docker: {run_process.stdout}\n{run_process.stderr}" if __name__ == "__main__": test_run_pytest_in_docker()
This script automates running pytest tests inside a Docker container.
First, it builds a Docker image named pytest-test-image using the Dockerfile in the current directory. It checks if the build succeeded by verifying the return code.
Next, it runs a container from this image, which executes pytest tests. The output is captured and printed to the console.
Assertions check that both the Docker build and the pytest run complete successfully. If any step fails, the test fails with an error message showing the problem.
This approach ensures tests run in a clean, isolated environment defined by the Dockerfile.
Now add data-driven testing by running pytest inside Docker with three different Python versions: 3.10, 3.11, and 3.12
Practice
Solution
Step 1: Understand Docker container purpose
Docker containers provide isolated environments that are the same every time they run.Step 2: Connect to pytest benefits
Running pytest inside Docker avoids environment differences causing test failures.Final Answer:
It ensures tests run in a clean, consistent environment every time. -> Option AQuick Check:
Docker isolation = consistent test environment [OK]
- Thinking Docker speeds up tests automatically
- Believing Docker fixes test code errors
- Assuming tests run without writing code
Solution
Step 1: Identify volume sharing syntax
The-v $(pwd):/appoption shares the current directory to /app inside the container.Step 2: Check working directory and command
-w /appsets the working directory, andpytestruns tests there.Final Answer:
docker run -v $(pwd):/app -w /app python:3.12 pytest -> Option BQuick Check:
Volume + workdir + pytest = correct run command [OK]
- Using -p instead of -v for volume
- Not setting working directory with -w
- Running pytest without mounting code
docker run --rm -v $(pwd):/tests -w /tests python:3.12 pytest test_sample.py
What will happen if
test_sample.py contains one passing and one failing test?Solution
Step 1: Understand pytest behavior on failures
Pytest runs all tests and returns a non-zero exit code if any test fails.Step 2: Confirm Docker command mounts tests correctly
The volume mounts current directory to /tests, so test_sample.py is accessible and runs.Final Answer:
Pytest will run both tests and exit with a non-zero code indicating failure. -> Option DQuick Check:
Failing test causes pytest exit code != 0 [OK]
- Assuming pytest skips failing tests
- Thinking Docker volume mount fails here
- Believing pytest ignores failures
docker run -v $(pwd):/app python:3.12 pytest
But pytest does not find any tests. What is the most likely cause?
Solution
Step 1: Check working directory usage
Without-w /app, pytest runs in root, not where tests are mounted.Step 2: Understand pytest test discovery
Pytest looks for tests in current directory; wrong directory means no tests found.Final Answer:
You did not set the working directory inside the container. -> Option AQuick Check:
Working directory missing = no tests found [OK]
- Assuming pytest is missing in python image by default
- Blaming volume syntax without error
- Thinking test file names must be exact
Solution
Step 1: Ensure volume mount and working directory
Mount current directory to /app and set working directory to /app so files are accessible.Step 2: Specify report path inside container
Use--junitxml=/app/report.xmlto save report inside mounted folder, making it visible on host.Final Answer:
docker run -v $(pwd):/app -w /app python:3.12 pytest --junitxml=/app/report.xml -> Option CQuick Check:
Volume + workdir + report path inside mount = correct [OK]
- Not setting working directory with -w
- Using relative path without mount inside container
- Omitting volume mount so report is lost
