Skip to contentSkip to page list

Docs

Create and Remove a Scenario's Test Data

Write test data as fixtures, create it in setup through your real API, and remove it in reverse in teardown, so every run starts from the same world.

On this page

Where we are in the story: Anna's scenario is written, and its fixtures say what world it needs: one gold customer with one delivered order. This page is about turning those fixtures into real records before the first turn, and removing them after the last.

A fixture is the data, not the API

A fixture says what must exist: Anna, gold, with a delivered order of 49.90. It does not say how to create it. Keep the two apart. The fixtures sit with the scenario, keyed by entity type, with one object or a list of objects per type. The code that calls your API lives once, in setup, and serves every scenario.

example.jsonJSON
{  "customers": [{ "name": "Anna", "loyaltyTier": "gold" }],  "orders": [{ "customerId": { "$ref": "customers[0].id" }, "status": "delivered", "total": 49.9 }]}

A link between entities is an explicit {"$ref": "customers[0].id"} that points at an entity before it: see the $ref format. Setup replaces it with the real id it got back, never a matching name or a guessed id.

Match the API and the data models

Write each fixture with your API's own entity and field names, as the body of its create call: loyaltyTier if the API says loyaltyTier. Then a fixture reads like a request a developer already knows, and a typo is a field the API does not have.

  • Set only what the scenario needs. Each field is fixed, defaulted or generated: the scenario sets the fields its criteria depend on, a factory default covers the other fields the API needs, and setup generates what must be unique, such as an email. Keep one factory per entity type, so the data stays valid when the API adds a field. A Scenario Connector keeps its factory as the fixture types it declares.
  • Check every request body before you create anything. Refuse a field the API's schema does not list: many servers drop an unknown field without an error, and the scenario then runs against data that is not what it says.
  • Create through the real create API, not the database. The API applies the same rules, defaults and side effects as production.

Mock the scenario as closely and repeatably as you can

Build the world the conversation needs, and only that, the same way on every run. If the scenario is "a gold customer with a delivered order", create exactly that customer and order, not a shared demo account that other tests also change. The same fixtures make the same world, so two runs of the same agent can be compared, and a change in the verdict is a change in the agent.

Setup creates, teardown cleans up

Setup creates the entities in dependency order: Anna before her order. It fills in each factory's defaults, resolves every $ref, and returns the same shape with defaults and real ids filled in. That returned object is the run's state.

Create test data only in setup. Data made anywhere else, such as inside a turn, is not in the state, so teardown has no record of it.

Teardown takes that state and deletes in reverse order: the order, then Anna. Make it safe to run again:

  • Safe on partial data. Teardown can run after a failed setup, with partial state or none, so check each id exists before you delete it, and remove everything you can.
  • Safe to run twice. Treat "already gone" as success.
  • Undo a half-finished setup in setup. Record each entity as you create it. If the third create fails, delete the first two and raise the original error. A setup hook that fails, or a block that raises, returns no state, so teardown cannot know what it made.
  • Throw when the data cannot be made. A setup that catches the error and returns an empty state makes the agent face data that does not exist. A setup that fails marks the run Not scored instead, see Running and CI.

Tag the data with the run. Runs of a suite overlap, so a tag unique to the run keeps one run's data apart from another's, and lets you find leftovers if a teardown is missed.

Put the tag in a field the fixtures never set, such as an externalRef, so a fixture that names Anna does not overwrite it. Use the run id: run.scenarioRunId in a TypeScript hook, run.scenario_run_id in Python, input["run"]["scenarioRunId"] in a Scenario Connector block. Not the idempotencyKey: it is stable within an attempt but changes when the run is re-executed. In a pytest fixture, generate one.

Keep secrets out of the state. Return the fixtures with their ids, not whole API responses or credentials. The state is stored on the run, and the simulated user and the judge read it, so it is sent to your model provider.

Teardown can report what changed

Teardown can return what it removed, for example {"deleted": {"orders": 1, "customers": 1}}, and say what it could not remove. Hooks and Scenario Connector blocks show that object on the run, under Returned by teardown, so anyone can see whether something needs cleaning up by hand. pytest and vitest do not report it.

Coming: a standard report of what a run changed, for teardown to fill in. Today the returned object is free-form.

Where to create the test data

The rules above hold everywhere. Where the code runs depends on your path.

Your setupCreate the data withRuns inRead more
Scenarios in pytest or vitestA pytest fixture, or beforeEach and afterEachYour test processTest an agent fully in code
Scenarios in the platform, agent connected with connectAgent or @langwatch.connect_agentSetup and teardown hooks (langwatch SDK release after 2.5.0 / 2.0.1)Your agent's process, with its database and secretsSetup and teardown hooks
Scenarios in the platform, agent behind an APIThe setup and teardown blocks of a Scenario ConnectorLangWatch's code runner, so your API must be reachable from itScenario Connector

A Scenario Connector and setup hooks read their fixtures from the scenario's parameters: input["params"] in a block, with the defaults of the connector's fixture types filled in, and run.fixtures in a hook (langwatch SDK release after 2.5.0 / 2.0.1), as the scenario set it, so the hook applies its own defaults. Test code keeps its fixtures, and its factories, in the test files.

Next: Parameters and suites, for what varies from run to run.

Documentation