0
0
PyTesttesting~20 mins

Test containers with Docker in PyTest - Practice Problems & Coding Challenges

Choose your learning style9 modes available
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 test using Docker container fixture
What is the output of this pytest test when using the Docker container fixture that starts a Redis container and sets a key?
PyTest
import pytest
import redis

@pytest.fixture(scope='module')
def redis_container(docker_ip, docker_services):
    port = docker_services.port_for('redis', 6379)
    client = redis.Redis(host=docker_ip, port=port)
    docker_services.wait_until_responsive(
        timeout=30.0, pause=0.1,
        check=lambda: client.ping())
    return client

def test_redis_set_get(redis_container):
    redis_container.set('testkey', 'value')
    result = redis_container.get('testkey')
    assert result == b'value'
    print(result)
Ab'value' printed and test passes
Bb'value' printed but test fails due to assertion error
CTest raises a ConnectionError because Redis is not reachable
DTest raises a SyntaxError due to fixture definition
Attempts:
2 left
💡 Hint
The fixture waits until Redis is responsive before returning the client.
assertion
intermediate
1:30remaining
Correct assertion for checking HTTP response in Dockerized test
You run a test against a web service inside a Docker container. Which assertion correctly verifies the HTTP status code is 200?
PyTest
response = client.get('http://localhost:8080/api/data')
Aassert response.status == 200
Bassert response.status_code == 200
Cassert response.code == 200
Dassert response.status_code is 200
Attempts:
2 left
💡 Hint
Check the attribute name for status code in the response object.
🔧 Debug
advanced
2:00remaining
Debugging Docker container test failure due to port conflict
A pytest test using a Docker container fails with an error indicating the port 5432 is already in use. What is the most likely cause?
AThe Docker container image is missing the PostgreSQL service
BThe test code has a syntax error in the port mapping
CAnother process on the host machine is using port 5432, causing conflict
DThe pytest fixture is not starting the container
Attempts:
2 left
💡 Hint
Port conflicts happen when two services try to use the same port on the host.
framework
advanced
1:30remaining
Choosing the best pytest plugin for Docker container management
Which pytest plugin is designed specifically to manage Docker containers lifecycle during tests?
Apytest-docker
Bpytest-mock
Cpytest-cov
Dpytest-xdist
Attempts:
2 left
💡 Hint
Look for a plugin name that includes 'docker'.
🧠 Conceptual
expert
2:00remaining
Why use test containers in integration testing?
What is the main advantage of using test containers with Docker in integration testing?
AThey automatically generate test data for database tests
BThey speed up unit tests by mocking dependencies
CThey eliminate the need for writing assertions in tests
DThey provide isolated, reproducible environments matching production services
Attempts:
2 left
💡 Hint
Think about environment consistency and isolation.