Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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.
✗ Incorrect
The variable name 'container' is used to refer to the running MySQL container instance.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using an attribute instead of a method.
Using incorrect method names like 'connection_url' without parentheses.
✗ Incorrect
The method 'get_connection_url()' returns the connection URL string for the MySQL container.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'run()' or 'launch()' which are not valid methods.
Forgetting to call the method with parentheses.
✗ Incorrect
The correct method to start the container is 'start()'. Other options are invalid and cause errors.
4fill in blank
hardFill 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'
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.
✗ Incorrect
The variable 'redis_container' is used consistently to refer to the Redis container instance inside the 'with' block.
5fill in blank
hardFill 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'
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.
✗ Incorrect
The variable 'mongo_container' is used to hold the container instance. The method 'get_connection_url()' returns the connection URL string.