TEST @setup
Test files only
This annotation applies only to test files run by the SQL test runner (npgsqlrest --test). It has no meaning in endpoint SQL files or routine comments.
Run one or more named steps (from the TestRunner.Steps registry) before this test file executes.
Syntax
Placed in the file's header — the leading -- line comments before the first SQL statement or HTTP block:
sql
-- @setup StepName [StepName ...]- Names may be whitespace- or comma-separated:
-- @setup CreateDb SeedDataand-- @setup CreateDb, SeedDataare equivalent. - The annotation is repeatable; all listed steps run in the order written.
- Every name must exist in the
TestRunner.Stepsregistry — an unknown name is a loud error, not a silent skip. - A step with
"Enabled": falseis the one sanctioned skip: it is ignored wherever referenced (logged at debug level) — the default configuration ships disabled example steps to flip on instead of typing.
Example
Config:
json
{
"TestRunner": {
"Steps": {
"CreateIsolatedDb": {
"Sql": "create database app_iso_{rnd5_1} template app_template_{rnd5}",
"ConnectionName": "Admin"
},
"DropIsolatedDb": {
"Sql": "drop database if exists app_iso_{rnd5_1} with (force)",
"ConnectionName": "Admin"
}
}
}
}Test file:
sql
-- @setup CreateIsolatedDb
-- @teardown DropIsolatedDb
-- @connection Isolated
/*
POST /api/create-user
Content-Type: application/json
{"name": "Ada"}
*/
select status = 200, 'user created in the isolated clone' from _response;The step runs once, immediately before this file (after the run-level Setup). Combined with -- @teardown and -- @connection, this gives a single test file its own private database.
Header semantics
The header ends at the first SQL statement or HTTP block. An included file (\i/\ir) that contains only comments (an annotation profile) continues the header — its annotations count as if written in-place — so a shared profile can carry @setup/@teardown/@connection/@tag for many test files.
Watch the prose
Everything after the step names on the line is treated as more step names. Write explanatory text on its own comment line, not after the names.
Related
- TEST @teardown — the mirror, always runs after the file
- TEST @connection — run the file on another connection
- Test Runner configuration — the
Stepsregistry - Testing Guide