You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+24-24Lines changed: 24 additions & 24 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -13,9 +13,9 @@ We also have a RedisService which connects to a Redis server and performs a few
13
13
14
14
The purpose of this project is to:
15
15
1. Demonstrate how specmatic can be used to validate the contract of the bff service
16
-
while stubbing out the order api service at the same time.
16
+
while mocking out the order api service at the same time.
17
17
18
-
2. Demonstrate how to stub out the Redis server using Specmatic Redis and Test Containers.
18
+
2. Demonstrate how to mock out the Redis server using Specmatic Redis Mock and Test Containers.
19
19
20
20
21
21
## Prerequisites
@@ -39,9 +39,9 @@ Run the tests using pytest:
39
39
pytest tests -v -s
40
40
```
41
41
42
-
## Setting stubs for Specmatic Redis
43
-
Specmatic Redis takes an argument 'data' which is expected to be a directory with stub json files.
44
-
The stub files are expected to have this structure:
42
+
## Setting test data for Specmatic Redis
43
+
Specmatic Redis takes an argument 'data' which is expected to be a directory with test data json files.
44
+
The test data files are expected to have this structure:
45
45
46
46
```
47
47
{
@@ -67,7 +67,7 @@ The stub files are expected to have this structure:
67
67
68
68
## Running Specmatic Redis in Tests (with Testcontainers)
69
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.
70
+
Use the containerized Specmatic Redis Mock to spin up an ephemeral Redis-like mock for your tests. The snippet below starts the image, mounts your test data, exposes the port, and **waits until the mock is ready** (detected via a log line) before the test proceeds.
71
71
72
72
```python
73
73
from testcontainers.core.container import DockerContainer
@@ -77,23 +77,23 @@ from testcontainers.core.wait_strategies import LogMessageWaitStrategy
77
77
SPECMATIC_REDIS_VERSION="latest"# or a pinned version like "0.9.4"
.waiting_for(LogMessageWaitStrategy(r"Specmatic Redis has started on .*:\d+").with_startup_timeout(10))
88
88
)
89
89
```
90
90
91
91
### What this does
92
92
93
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).
94
+
***Command**: `virtualize --host ... --port ... --data ...` – launches Specmatic Redis and points it to your **test dataset** (files that define responses/fixtures).
95
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).
96
+
***Volume mapping**: `.with_volume_mapping(TEST_DATA_DIR, TEST_DATA_DIR)` – mounts your local test data directory into the container at the **same** path (keeps file references simple).
97
97
***Readiness check**: `LogMessageWaitStrategy(...)` – blocks test execution until the container logs the **ready** line.
98
98
99
99
### Readiness log pattern
@@ -104,7 +104,7 @@ The regex `r"Specmatic Redis has started on .*:\d+"` matches a line like:
104
104
Specmatic Redis has started on 0.0.0.0:6379
105
105
```
106
106
107
-
Ensure `STUB_DATA_DIR` is an **absolute path** and contains your stub files.
107
+
Ensure `TEST_DATA_DIR` is an **absolute path** and contains your test data files.
108
108
109
109
110
110
## Running Contract Tests
@@ -115,7 +115,7 @@ Here’s a clean, professional README-ready write-up you can use to document tha
115
115
## 🧪 Running Contract Tests with Specmatic (FastAPI Example)
116
116
117
117
This section demonstrates how to **run contract tests** for a FastAPI application using [Specmatic](https://specmatic.io/).
118
-
Specmatic validates your app against its **OpenAPI contracts** and provides **API coverage reporting** and **stubbed interactions** for end-to-end testing.
118
+
Specmatic validates your app against its **OpenAPI contracts** and provides **API coverage reporting** and **mock interactions** for end-to-end testing.
119
119
120
120
```python
121
121
import os
@@ -126,9 +126,9 @@ from app.main import app as fastapi_app
|**Environment setup**| Defines the host and port for both the **FastAPI app** and the **Specmatic stub**. The stub uses data from `tests/contract/data` to simulate downstream dependencies. |
161
-
|**Specmatic configuration**| Points to your central `specmatic.yaml` file, which defines service contracts and stub mappings.|
|**Environment setup**| Defines the host and port for both the **FastAPI app** and the **Specmatic mock**. The mock uses data from `tests/contract/data` to simulate downstream dependencies. |
161
+
|**Specmatic configuration**| Points to your central `specmatic.yaml` file, which defines service contracts and dependencies. |
|**ASGI app registration**|`.with_asgi_app('app.main:app', APP_HOST, APP_PORT)` registers your FastAPI app for validation. Here, `app.main:app` refers to the `app` object inside the `main.py` module. |
164
-
|**Stub registration**|`.with_stub(STUB_HOST, STUB_PORT, args=[f"--data={STUB_DATA_DIR}"])` launches the Specmatic stub locally to serve mock responses from the specified stub data directory. |
164
+
|**Mock registration**|`.with_stub(MOCK_HOST, MOCK_PORT, args=[f"--data={TEST_DATA_DIR}"])` launches the Specmatic mock locally to serve mock responses from the specified test data directory. |
165
165
|**Contract testing with coverage**|`.test_with_api_coverage_for_fastapi_app(TestContract, fastapi_app)` runs Specmatic tests against your FastAPI app and reports API coverage by spinning up a lightweight coverage server (which parts of the contract were exercised). |
166
166
|**Clean-up**| Once the tests complete, the environment variable is reset to disable generative testing. |
0 commit comments