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: blogs/series-5-devops-data/5.1-database-seeding.md
+126-6Lines changed: 126 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -444,19 +444,139 @@ The seed value makes generation **deterministic**. Given the same seed, Bogus al
444
444
445
445
---
446
446
447
-
## 🧪 Playwright Seeding Placeholder
447
+
## 🧪 Playwright Seeding via the API
448
448
449
-
`Tests/AngularNetTutorial-Playwright/tests/seed.spec.ts` exists as a placeholder for E2E-driven seeding:
449
+
`Tests/AngularNetTutorial-Playwright/tests/seed.spec.ts` is a placeholder for E2E-driven seeding. The startup seeder gives every developer a consistent base of 1,000 employees — but sometimes an E2E test needs something more specific: a known number of positions, a particular department, or a clean slate before a destructive test run. That's what this file is for.
450
+
451
+
### Why E2E Tests Need Their Own Seeding
452
+
453
+
The startup seeder runs once on first launch. After that, tests that create, update, or delete records permanently change the database. If three tests run in sequence and test 2 deletes a record that test 3 expects to find, test 3 fails intermittently depending on run order. E2E-driven seeding solves this by resetting or expanding the dataset to a known state before the tests that depend on it.
454
+
455
+
Three common patterns:
456
+
457
+
***Add before** — seed extra records before a test that needs more data than startup seeding provides (e.g., pagination tests that need 200+ positions)
458
+
***Reset between** — call the seed endpoint in `beforeEach` to restore a known state before each destructive test
459
+
***Seed in global setup** — run once before the entire test suite to guarantee a baseline without relying on startup seeding
460
+
461
+
### The Playwright `request` Fixture
462
+
463
+
Playwright provides a `request` fixture — an `APIRequestContext` that makes HTTP calls without launching a browser. It is the right tool for calling the seed API in tests because:
464
+
465
+
* No browser overhead — seed calls complete in milliseconds
466
+
* Available in `beforeAll`, `beforeEach`, and in `globalSetup`
467
+
* Uses the same `baseURL` from `playwright.config.ts`
468
+
* Returns a full `APIResponse` with status, headers, and body
469
+
470
+
### Calling the Seed Endpoint
471
+
472
+
The `InsertMockPositionCommand` is exposed as `POST /api/v1/positions/seed`. Here is a complete implementation of `seed.spec.ts`:
**`params: { rowCount: 25 }`** — Playwright serializes this as a query string: `?rowCount=25`. The command handler uses this value to control how many positions Bogus generates.
500
+
501
+
**`request.post()`** — uses the `request` fixture from the `test` parameters. No browser is opened. The call completes before the test body continues.
502
+
503
+
**`await response.json()`** — the endpoint returns the count of rows inserted as a plain integer. The assertion confirms at least one record was created.
504
+
505
+
### Seeding in `beforeAll` for a Test Suite
506
+
507
+
When multiple tests in the same file need the seeded data, call the seed endpoint in `beforeAll` so it runs once before the suite rather than repeating it in every test:
For seeding that should run once before the **entire Playwright suite** — not just one file — use a `globalSetup` script. `playwright.config.ts` already has the hook point:
540
+
541
+
```typescript
542
+
// playwright.config.ts
543
+
exportdefaultdefineConfig({
544
+
globalSetup: './global-setup.ts',
545
+
// ...
456
546
});
457
547
```
458
548
459
-
When an E2E test suite needs a specific dataset before running, this is where you'd implement it — call the `InsertMockPositionCommand` endpoint via the `request` fixture, or trigger a specific seed via the API. The file exists to remind you this pattern is available.
`request.newContext()` in `globalSetup` creates a standalone API context outside of any test. `await apiContext.dispose()` releases it when done. Throwing an error from `globalSetup` fails the entire run immediately with a clear message rather than silently running tests against an unseeded database.
574
+
575
+
### When to Use Each Approach
576
+
577
+
***`seed.spec.ts` standalone test** — run manually from Swagger or CI when you need to top up data in a shared environment
578
+
***`beforeAll` in a spec file** — guarantee a data baseline for one specific test suite without affecting others
579
+
***`globalSetup`** — guarantee the baseline for the entire run; appropriate when all tests depend on the same starting state
0 commit comments