Docker lets you run tests in a clean, controlled space. This helps avoid problems from different computers or setups.
Docker-based test execution in PyTest
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
PyTest
docker run [OPTIONS] IMAGE [COMMAND]
docker run starts a container from an image.
You can add options like -v to share folders or -it for interactive mode.
Examples
PyTest
docker run -v $(pwd):/app -w /app python:3.12 pytest test_sample.pyPyTest
docker run --rm -v $(pwd):/tests -w /tests python:3.12 pytestSample Program
This simple test checks if 1 + 1 equals 2. The Docker command runs pytest inside a Python 3.12 container, using your current folder.
PyTest
# test_sample.py def test_addition(): assert 1 + 1 == 2 # Run command: # docker run --rm -v $(pwd):/app -w /app python:3.12 pytest test_sample.py
Important Notes
Always share your test folder with the container using -v so tests can access your files.
Use --rm to clean up containers after tests finish.
Make sure the Docker image has all needed tools for your tests.
Summary
Docker helps run tests in a clean, repeatable environment.
You run tests inside containers using docker run with volume sharing.
This avoids setup problems and makes tests portable.
Practice
1. What is the main benefit of running pytest tests inside a Docker container?
easy
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]
Hint: Docker isolates environment to avoid setup issues [OK]
Common Mistakes:
- Thinking Docker speeds up tests automatically
- Believing Docker fixes test code errors
- Assuming tests run without writing code
2. Which of the following is the correct way to run pytest inside a Docker container with your current directory shared?
easy
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]
Hint: Use -v for volume and -w for working directory [OK]
Common Mistakes:
- Using -p instead of -v for volume
- Not setting working directory with -w
- Running pytest without mounting code
3. Given this Docker command:
What will happen if
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?medium
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]
Hint: Pytest exit code shows if tests passed or failed [OK]
Common Mistakes:
- Assuming pytest skips failing tests
- Thinking Docker volume mount fails here
- Believing pytest ignores failures
4. You run this command:
But pytest does not find any tests. What is the most likely cause?
docker run -v $(pwd):/app python:3.12 pytest
But pytest does not find any tests. What is the most likely cause?
medium
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]
Hint: Always set -w to your mounted test folder [OK]
Common Mistakes:
- Assuming pytest is missing in python image by default
- Blaming volume syntax without error
- Thinking test file names must be exact
5. You want to run pytest inside Docker and save the test report to your host machine. Which command correctly achieves this?
hard
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]
Hint: Set -w and use full path for report inside mounted folder [OK]
Common Mistakes:
- Not setting working directory with -w
- Using relative path without mount inside container
- Omitting volume mount so report is lost
