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
+86Lines changed: 86 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -105,3 +105,89 @@ Specmatic Redis has started on 0.0.0.0:6379
105
105
```
106
106
107
107
Ensure `STUB_DATA_DIR` is an **absolute path** and contains your stub files.
108
+
109
+
110
+
## Running Contract Tests
111
+
Here’s a clean, professional README-ready write-up you can use to document that block of code:
112
+
113
+
---
114
+
115
+
## 🧪 Running Contract Tests with Specmatic (FastAPI Example)
116
+
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.
|**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. |
|**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. |
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
+
|**Clean-up**| Once the tests complete, the environment variable is reset to disable generative testing. |
167
+
168
+
---
169
+
170
+
### 🧩 Typical Workflow
171
+
172
+
1. Ensure your `specmatic.yaml` file points to the correct contract specs.
173
+
2. Place your stubbed response files under:
174
+
175
+
```
176
+
tests/contract/data/
177
+
```
178
+
3. Specmatic will:
179
+
180
+
* Start the stub server.
181
+
* Boot your FastAPI app.
182
+
* Execute contract-based test cases.
183
+
* Report mismatches and coverage results.
184
+
---
185
+
186
+
### 🧠 Notes
187
+
*`app.main:app` follows the same syntax as `uvicorn main:app` — it references the `app` object defined inside `main.py`.
188
+
*`SPECMATIC_GENERATIVE_TESTS=true` enables generation of resilience tests.
189
+
* Keep `STUB_DATA_DIR` paths **absolute** to avoid issues with volume mounts or relative references.
190
+
* API coverage output helps ensure all contract endpoints are implemented and validated.
0 commit comments