|
1 | | -_Testcontainers for Python_ plays well with Python's testing frameworks like pytest. |
| 1 | +_Testcontainers for Python_ integrates seamlessly with Python testing frameworks like [pytest](https://docs.pytest.org/en/stable/). |
2 | 2 |
|
3 | | -The ideal use case is for integration or end to end tests. It helps you to spin |
4 | | -up and manage the dependencies life cycle via Docker. |
| 3 | +It's ideal for integration and end-to-end tests, allowing you to easily manage dependencies using Docker. |
5 | 4 |
|
6 | 5 | ## 1. System requirements |
7 | 6 |
|
8 | | -Please read the [system requirements](system_requirements/index.md) page before you start. |
| 7 | +Before you begin, review the [system requirements](system_requirements/index.md). |
9 | 8 |
|
10 | 9 | ## 2. Install _Testcontainers for Python_ |
11 | 10 |
|
12 | | -You can install testcontainers-python using pip: |
| 11 | +Install testcontainers-python with pip: |
13 | 12 |
|
14 | 13 | ```bash |
15 | 14 | pip install testcontainers |
@@ -37,33 +36,25 @@ def test_with_redis(): |
37 | 36 | assert value == "Hello, Redis!" |
38 | 37 | ``` |
39 | 38 |
|
40 | | -The `RedisContainer` class provides a convenient way to start a Redis container for testing. |
| 39 | +The `RedisContainer` class makes it easy to start a Redis container for testing: |
41 | 40 |
|
42 | | -- The container is automatically started when entering the context manager (`with` statement) |
43 | | -- The container is automatically stopped and removed when exiting the context manager |
44 | | -- `get_container_host_ip()` returns the host IP where the container is running |
45 | | -- `get_exposed_port()` returns the mapped port on the host |
| 41 | +- The container starts automatically when entering the context manager (`with` statement). |
| 42 | +- It stops and removes itself when exiting the context. |
| 43 | +- `get_container_host_ip()` returns the host IP. |
| 44 | +- `get_exposed_port()` returns the mapped host port. |
46 | 45 |
|
47 | | -When you use `get_exposed_port()`, you have to imagine yourself using `docker run -p |
48 | | -<port>`. When you do so, `dockerd` maps the selected `<port>` from inside the |
49 | | -container to a random one available on your host. |
| 46 | +When using `get_exposed_port()`, think of it as running `docker run -p <port>`. `dockerd` maps the container's internal port to a random available port on your host. |
50 | 47 |
|
51 | | -In the previous example, we expose the default Redis port (6379) for `tcp` traffic to the outside. This |
52 | | -allows Redis to be reachable from your code that runs outside the container, but |
53 | | -it also makes parallelization possible because if you run your tests in parallel, each test will get its own Redis container exposed on a different random port. |
| 48 | +In the example above, the default Redis port (6379) is exposed for TCP traffic. This setup allows your code to connect to Redis outside the container and supports parallel test execution. Each test gets its own Redis container on a unique, random port. |
54 | 49 |
|
55 | | -The container is automatically cleaned up when the test finishes, thanks to the context manager (`with` statement). This ensures that no containers are left running after your tests complete. |
| 50 | +The context manager (`with` statement) ensures containers are cleaned up after tests, so no containers are left running. |
56 | 51 |
|
57 | 52 | !!!tip |
| 53 | + See [the garbage collector](features/garbage_collector.md) for another way to clean up resources. |
58 | 54 |
|
59 | | - Look at [the garbage collector](features/garbage_collector.md) to know another way to |
60 | | - clean up resources. |
| 55 | +## 4. Connect your code to the container |
61 | 56 |
|
62 | | -## 4. Make your code to talk with the container |
63 | | - |
64 | | -This is just an example, but usually Python applications that rely on Redis are |
65 | | -using the [redis-py](https://github.com/redis/redis-py) client. This code gets |
66 | | -the endpoint from the container we just started, and it configures the client. |
| 57 | +Typically, Python applications use the [redis-py](https://github.com/redis/redis-py) client. The following code retrieves the endpoint from the container and configures the client. |
67 | 58 |
|
68 | 59 | ```python |
69 | 60 | def test_redis_operations(): |
@@ -97,4 +88,4 @@ pytest test_redis.py |
97 | 88 |
|
98 | 89 | ## 6. Want to go deeper with Redis? |
99 | 90 |
|
100 | | -You can find a more elaborated Redis example in our examples section. Please check it out [here](./modules/redis.md). |
| 91 | +You can find a more elaborated Redis example in our [examples section](./modules/redis.md). |
0 commit comments