0
0
PyTesttesting~10 mins

Test containers with Docker in PyTest - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to start a Docker container for testing using pytest.

PyTest
from testcontainers.mysql import MySqlContainer

def test_mysql_container():
    with MySqlContainer("mysql:8.0") as [1]:
        assert container.is_running
Drag options to blanks, or click blank then click option'
Aservice
Bclient
Cdocker
Dcontainer
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name that does not represent the container instance.
Forgetting to assign the container to a variable.
2fill in blank
medium

Complete the code to get the connection URL from the running MySQL container.

PyTest
from testcontainers.mysql import MySqlContainer

def test_connection_url():
    with MySqlContainer("mysql:8.0") as container:
        url = container.[1]()
        assert url.startswith("mysql://")
Drag options to blanks, or click blank then click option'
Aconnection_url
Bget_connection_url
Cget_url
Dconnection_string
Attempts:
3 left
💡 Hint
Common Mistakes
Using an attribute instead of a method.
Using incorrect method names like 'connection_url' without parentheses.
3fill in blank
hard

Fix the error in the code to properly start a PostgreSQL test container.

PyTest
from testcontainers.postgres import PostgresContainer

def test_postgres():
    container = PostgresContainer("postgres:13")
    container.[1]()
    assert container.is_running
    container.stop()
Drag options to blanks, or click blank then click option'
Astart
Blaunch
Crun
Dbegin
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'run()' or 'launch()' which are not valid methods.
Forgetting to call the method with parentheses.
4fill in blank
hard

Fill both blanks to create a test that uses a Redis container and checks if it is running.

PyTest
from testcontainers.redis import RedisContainer

def test_redis_container():
    with RedisContainer() as [1]:
        assert [2].is_running
Drag options to blanks, or click blank then click option'
Aredis_container
Bredis
Ccontainer
Dclient
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names in the 'with' statement and assertion.
Using generic names like 'client' that do not represent the container.
5fill in blank
hard

Fill all three blanks to create a test that starts a MongoDB container, gets its connection URL, and asserts it contains 'mongodb://'.

PyTest
from testcontainers.mongodb import MongoDbContainer

def test_mongodb_connection():
    with MongoDbContainer("mongo:5.0") as [1]:
        url = [2].[3]()
        assert "mongodb://" in url
Drag options to blanks, or click blank then click option'
Amongo_container
Bmongo
Cget_connection_url
Dconnection_url
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names for the container instance.
Using 'connection_url' as an attribute instead of a method.
Forgetting parentheses when calling the method.