Skip to content

Commit 8baff60

Browse files
committed
Add info about pytest_plugin into readme
1 parent 4b201d4 commit 8baff60

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

README.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,4 +177,68 @@ organizationType: non-profit
177177
employeesCount: 10
178178
```
179179

180+
## Testing with the pytest plugin
181+
182+
The SDK provides a pytest plugin that starts your app, exposes fixtures for the SDK and Aidbox client, and helps isolate tests that create resources.
183+
184+
### Activating the plugin
185+
186+
In your project’s **`conftest.py`** (e.g. `tests/conftest.py`), register the plugin:
187+
188+
```python
189+
pytest_plugins = ["aidbox_python_sdk.pytest_plugin"]
190+
```
191+
192+
Alternatively you can configure it in **`pyproject.toml`**:
193+
194+
```toml
195+
[tool.pytest.ini_options]
196+
pytest_plugins = ["aidbox_python_sdk.pytest_plugin"]
197+
```
198+
199+
### Configuring the app factory
200+
201+
The plugin needs your app factory (the callable that returns the `web.Application`). You can set it in pytest ini:
202+
203+
**`pyproject.toml`**
204+
```toml
205+
[tool.pytest.ini_options]
206+
pytest_plugins = ["aidbox_python_sdk.pytest_plugin"]
207+
aidbox_create_app = "main:create_app"
208+
```
209+
210+
Use the dotted path to your callable: either `module:name` (e.g. `main:create_app`) or `module.submodule.name` (e.g. `mypackage.main.create_app`). The default is `main:create_app`.
211+
212+
To use a different factory without changing ini, override the fixture in your `conftest.py`:
213+
214+
```python
215+
@pytest.fixture(scope="session")
216+
def create_app():
217+
from myapp.entry import create_app
218+
return create_app
219+
```
220+
221+
### Fixtures provided
222+
223+
| Fixture | Description |
224+
|------------------|-------------|
225+
| `app` | The running `web.Application` (server in a background thread on port 8081). |
226+
| `client` | HTTP client for the app + `client.server.app` for the application instance. |
227+
| `sdk` | The SDK instance: `app[ak.sdk]`. |
228+
| `aidbox_client` | `AsyncAidboxClient` for calling Aidbox (operations, `/$psql`, etc.). |
229+
| `aidbox_db` | DB proxy: `app[ak.db]`. |
230+
| `safe_db` | Isolated DB for the test; see below. |
231+
232+
### Using `safe_db` for tests that create resources
233+
234+
Use the **`safe_db`** fixture in tests that create or change data. It records the current transaction id, runs your test, then rolls back everything created in that test so the DB stays clean.
235+
236+
```python
237+
@pytest.mark.asyncio
238+
async def test_patient_subscription(aidbox_client, safe_db, sdk):
239+
patient = await aidbox_client.resource("Patient", name=[{"family": "Test"}]).save()
240+
await sdk.was_subscription_triggered("Patient")
241+
# assertions...
242+
```
243+
180244

0 commit comments

Comments
 (0)