Skip to content

Commit a4ee7db

Browse files
committed
chore: updated readme file with steps on how to use specmatic redis with test containers
1 parent d6ca9e6 commit a4ee7db

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

README.md

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,45 @@ The stub files are expected to have this structure:
6363
}
6464
```
6565

66-
**NOTE:** The operation value must be in lowercase.
66+
**NOTE:** The operation value must be in lowercase.
67+
68+
## Running Specmatic Redis in Tests (with Testcontainers)
69+
70+
Use the containerized Specmatic Redis Mock to spin up an ephemeral Redis-like stub for your tests. The snippet below starts the image, mounts your stub data, exposes the port, and **waits until the mock is ready** (detected via a log line) before the test proceeds.
71+
72+
```python
73+
from testcontainers.core.container import DockerContainer
74+
from testcontainers.core.wait_strategies import LogMessageWaitStrategy
75+
76+
77+
SPECMATIC_REDIS_VERSION = "latest" # or a pinned version like "0.9.4"
78+
REDIS_HOST = "0.0.0.0"
79+
REDIS_PORT = 6379
80+
STUB_DATA_DIR = "/absolute/path/to/test/data"
81+
82+
container = (
83+
DockerContainer(f"specmatic/specmatic-redis:{SPECMATIC_REDIS_VERSION}")
84+
.with_command(f"virtualize --host {REDIS_HOST} --port {REDIS_PORT} --data {STUB_DATA_DIR}")
85+
.with_exposed_ports(REDIS_PORT)
86+
.with_volume_mapping(STUB_DATA_DIR, STUB_DATA_DIR)
87+
.waiting_for(LogMessageWaitStrategy(r"Specmatic Redis has started on .*:\d+").with_startup_timeout(10))
88+
)
89+
```
90+
91+
### What this does
92+
93+
* **Image**: `specmatic/specmatic-redis:{SPECMATIC_REDIS_VERSION}` – pulls and runs the Specmatic Redis Mock.
94+
* **Command**: `virtualize --host ... --port ... --data ...` – launches Specmatic Redis and points it to your **stub dataset** (files that define responses/fixtures).
95+
* **Port exposure**: `.with_exposed_ports(REDIS_PORT)` – publishes the Redis port to the host so your test client can connect.
96+
* **Volume mapping**: `.with_volume_mapping(STUB_DATA_DIR, STUB_DATA_DIR)` – mounts your local stub directory into the container at the **same** path (keeps file references simple).
97+
* **Readiness check**: `LogMessageWaitStrategy(...)` – blocks test execution until the container logs the **ready** line.
98+
99+
### Readiness log pattern
100+
101+
The regex `r"Specmatic Redis has started on .*:\d+"` matches a line like:
102+
103+
```
104+
Specmatic Redis has started on 0.0.0.0:6379
105+
```
106+
107+
Ensure `STUB_DATA_DIR` is an **absolute path** and contains your stub files.

0 commit comments

Comments
 (0)