Bird
Raised Fist0
PyTesttesting~20 mins

Docker-based test execution in PyTest - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Docker Test Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of pytest run inside Docker container
Consider a Docker container running a pytest suite with the following test file test_sample.py:
def test_addition():
    assert 2 + 3 == 5

def test_subtraction():
    assert 5 - 3 == 1

What will be the result of running pytest inside the container?
PyTest
def test_addition():
    assert 2 + 3 == 5

def test_subtraction():
    assert 5 - 3 == 1
A2 passed
B0 tests collected
C2 failed
D1 passed, 1 failed
Attempts:
2 left
💡 Hint
Check the assertions carefully for correctness.
locator
intermediate
1:30remaining
Best locator for pytest test file in Docker context
You want to run pytest inside a Docker container. Which file path locator is best to specify the test file test_api.py located in the /app/tests/ directory inside the container?
A./tests/test_api.py
B/app/tests/test_api.py
C/tests/test_api.py
Dtests/test_api.py
Attempts:
2 left
💡 Hint
Inside Docker, absolute paths are more reliable if you know the working directory.
assertion
advanced
1:30remaining
Assertion outcome in Docker pytest log
Inside a Docker container, a pytest test contains:
def test_string():
    result = 'hello'.upper()
    assert result == 'HELLO'

What will be the assertion outcome when running pytest inside Docker?
PyTest
def test_string():
    result = 'hello'.upper()
    assert result == 'HELLO'
ATest raises TypeError
BTest fails with AssertionError
CTest passes
DTest is skipped
Attempts:
2 left
💡 Hint
Check the string method and expected value carefully.
🔧 Debug
advanced
2:00remaining
Debugging pytest failure inside Docker
A pytest test inside Docker fails with:
FileNotFoundError: [Errno 2] No such file or directory: '/data/input.txt'

What is the most likely cause?
AThe file /data/input.txt is missing inside the Docker container
BDocker container has no network access
CThe test code has a syntax error
Dpytest is not installed in the container
Attempts:
2 left
💡 Hint
FileNotFoundError means the file path is invalid or file is missing.
framework
expert
3:00remaining
Choosing Docker test execution strategy for pytest
You want to run pytest tests inside Docker with these requirements:
- Tests must run in isolated containers
- Test results should be collected on host machine
- Tests should run in parallel

Which Docker strategy best fits these requirements?
AUse Docker Compose to spin up multiple containers running pytest in parallel, mounting a shared volume for test reports
BRun pytest directly on host without Docker for faster execution
CUse a single Docker container running pytest sequentially with no volume mounts
DRun pytest inside Docker but do not collect test reports
Attempts:
2 left
💡 Hint
Think about isolation, parallelism, and sharing results between container and host.

Practice

(1/5)
1. What is the main benefit of running pytest tests inside a Docker container?
easy
A. It ensures tests run in a clean, consistent environment every time.
B. It makes tests run faster than on the host machine.
C. It automatically fixes failing tests.
D. It allows tests to run without writing any test code.

Solution

  1. Step 1: Understand Docker container purpose

    Docker containers provide isolated environments that are the same every time they run.
  2. Step 2: Connect to pytest benefits

    Running pytest inside Docker avoids environment differences causing test failures.
  3. Final Answer:

    It ensures tests run in a clean, consistent environment every time. -> Option A
  4. Quick 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
A. docker run -p 8080:80 python:3.12 pytest
B. docker run -v $(pwd):/app -w /app python:3.12 pytest
C. docker build -t pytest-image . && docker run pytest-image
D. docker run --rm python:3.12 pytest /app

Solution

  1. Step 1: Identify volume sharing syntax

    The -v $(pwd):/app option shares the current directory to /app inside the container.
  2. Step 2: Check working directory and command

    -w /app sets the working directory, and pytest runs tests there.
  3. Final Answer:

    docker run -v $(pwd):/app -w /app python:3.12 pytest -> Option B
  4. Quick 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:
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
A. Pytest will run only the passing test and ignore failures.
B. Pytest will skip the failing test and exit with zero code.
C. Docker will fail to start because of volume issues.
D. Pytest will run both tests and exit with a non-zero code indicating failure.

Solution

  1. Step 1: Understand pytest behavior on failures

    Pytest runs all tests and returns a non-zero exit code if any test fails.
  2. Step 2: Confirm Docker command mounts tests correctly

    The volume mounts current directory to /tests, so test_sample.py is accessible and runs.
  3. Final Answer:

    Pytest will run both tests and exit with a non-zero code indicating failure. -> Option D
  4. Quick 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:
docker run -v $(pwd):/app python:3.12 pytest

But pytest does not find any tests. What is the most likely cause?
medium
A. You did not set the working directory inside the container.
B. The Python image does not have pytest installed.
C. Docker volume syntax is incorrect.
D. Tests must be named test_docker.py to run.

Solution

  1. Step 1: Check working directory usage

    Without -w /app, pytest runs in root, not where tests are mounted.
  2. Step 2: Understand pytest test discovery

    Pytest looks for tests in current directory; wrong directory means no tests found.
  3. Final Answer:

    You did not set the working directory inside the container. -> Option A
  4. Quick 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
A. docker run -v $(pwd):/app -w /app python:3.12 pytest --junitxml=/tmp/report.xml
B. docker run python:3.12 pytest --junitxml=/app/report.xml
C. docker run -v $(pwd):/app -w /app python:3.12 pytest --junitxml=/app/report.xml
D. docker run -v $(pwd):/app python:3.12 pytest --junitxml=report.xml

Solution

  1. Step 1: Ensure volume mount and working directory

    Mount current directory to /app and set working directory to /app so files are accessible.
  2. Step 2: Specify report path inside container

    Use --junitxml=/app/report.xml to save report inside mounted folder, making it visible on host.
  3. Final Answer:

    docker run -v $(pwd):/app -w /app python:3.12 pytest --junitxml=/app/report.xml -> Option C
  4. Quick 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