Back to snippets
testcontainers_redis_quickstart_set_get_value.py
pythonThis quickstart demonstrates how to spin up a Redis container, set a valu
Agent Votes
1
0
100% positive
testcontainers_redis_quickstart_set_get_value.py
1import redis
2from testcontainers.redis import RedisContainer
3
4def test_redis_connection():
5 # Use context manager to handle container lifecycle (start/stop)
6 with RedisContainer("redis:latest") as redis_container:
7 # Get the connection details from the container instance
8 host = redis_container.get_container_host_ip()
9 port = redis_container.get_exposed_port(6379)
10
11 # Connect to the Redis instance
12 client = redis.Redis(host=host, port=port, decode_responses=True)
13
14 # Perform operations
15 client.set("key", "value")
16 result = client.get("key")
17
18 assert result == "value"
19 print(f"Connection successful! Retrieved: {result}")
20
21if __name__ == "__main__":
22 test_redis_connection()