Skip to content

Commit 3dfd64a

Browse files
committed
chore: updated readme file with steps on how to run contract tests.
1 parent a4ee7db commit 3dfd64a

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

README.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,89 @@ Specmatic Redis has started on 0.0.0.0:6379
105105
```
106106

107107
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.
119+
120+
```python
121+
import os
122+
from specmatic.core.specmatic import Specmatic
123+
from definitions import ROOT_DIR
124+
from app.main import app as fastapi_app
125+
126+
APP_HOST = "127.0.0.1"
127+
APP_PORT = 8000
128+
129+
STUB_HOST = "127.0.0.1"
130+
STUB_PORT = 8080
131+
STUB_DATA_DIR = ROOT_DIR + "tests/contract/data"
132+
133+
SPECMATIC_CONFIG_FILE_PATH = ROOT_DIR + '/specmatic.yaml'
134+
135+
os.environ["SPECMATIC_GENERATIVE_TESTS"] = "true"
136+
137+
138+
class TestContract:
139+
pass
140+
141+
142+
(
143+
Specmatic()
144+
.with_specmatic_config_file_path(SPECMATIC_CONFIG_FILE_PATH)
145+
.with_stub(STUB_HOST, STUB_PORT, args=[f"--data={STUB_DATA_DIR}"])
146+
.with_asgi_app('app.main:app', APP_HOST, APP_PORT)
147+
.test_with_api_coverage_for_fastapi_app(TestContract, fastapi_app)
148+
.run()
149+
)
150+
151+
os.environ["SPECMATIC_GENERATIVE_TESTS"] = "false"
152+
```
153+
154+
---
155+
156+
### ⚙️ What This Does
157+
158+
| Step | Description |
159+
| ---------------------------------- |----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
160+
| **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. |
162+
| **Generative tests mode** | Enables Specmatic’s *generative testing* mode (`SPECMATIC_GENERATIVE_TESTS=true`) to generate resilience tests. |
163+
| **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.
191+
---
192+
193+

0 commit comments

Comments
 (0)