Skip to navigation

Test structure, written as tests

Each example shows one procedure as prose, as detected tests, as inline tests, and as a standalone spec. For what each tab holds, see Navigation and interaction.

Create and look up users

This procedure keeps two tests and an ignored section in one file. It uses httpRequest, the spec fields specId and description, the test fields testId and description, the step fields stepId and description, ignore blocks, and the config fields beforeAll and afterAll.

prose.md
# Manage users
## Create a user
To create a user, send a `POST` request to `/api/users`:
```http
POST http://localhost:8092/api/users
Content-Type: application/json
{"name": "Ada"}
```
## Look up a user
To look up a user by name, send a `GET` request with a `name` parameter:
```http
GET http://localhost:8092/api/users?name=Ada
```
## Delete a user
To delete a user, send a `DELETE` request:
```http
DELETE http://localhost:8092/api/users/ada
```

IDs and descriptions

  • A spec’s specId and description live in a spec file. A documentation file’s spec ID is its path from where you run Doc Detective, such as docs/manage-users.md.
  • A test without a testId gets one built from its spec ID and a hash of the test, such as structure-users~1f7c37ef. The ID stays the same from run to run until the test itself changes. In the example, every inline file gives its second test the same hash.
  • Several tests in one file run in order. A test statement that starts before the previous test ends closes that test first.
  • stepId and description name a step in results. They don’t change what the step does.

Skip part of a page

A test ignore start statement and a test ignore end statement skip everything between them: step statements and detected steps alike. Here, the ignore block skips the delete request, because cleanup deletes the user after every run. A test end statement also ends an ignore block that’s still open.

FormatIgnore block
Markdown<!-- test ignore start --> and <!-- test ignore end -->
MDX{/* test ignore start */} and {/* test ignore end */}
AsciiDoc// (test ignore start) and // (test ignore end)
HTML<!-- test ignore start --> and <!-- test ignore end -->
reStructuredText.. test ignore start and .. test ignore end
DITA<data name="doc-detective" value="test ignore start"/> and <data name="doc-detective" value="test ignore end"/>

Set up and clean up the run

The example’s config runs one spec before every input and one after them all:

doc-detective.json
{
"beforeAll": ["check-api.spec.json"],
"afterAll": ["delete-user.spec.json"]
}
check-api.spec.json
{
"tests": [
{
"testId": "structure-users-check-api",
"description": "Check that the API answers before any test runs.",
"steps": [
{
"httpRequest": {
"url": "http://localhost:8092/status/200",
"statusCodes": [200]
}
}
]
}
]
}
delete-user.spec.json
{
"tests": [
{
"testId": "structure-users-delete",
"description": "Delete the user that the tests created, after every test runs.",
"steps": [
{
"httpRequest": {
"url": "http://localhost:8092/api/users/ada",
"method": "delete"
}
}
]
}
]
}

beforeAll and afterAll each take a path or a list of paths, relative to the config file. Their specs run as separate specs: first the beforeAll specs, then your inputs, then the afterAll specs. beforeAny is a deprecated name for beforeAll.

What detection finds

Each http code block becomes an httpRequest step with the block’s method, URL, headers, and body. Detection doesn’t add the response and statusCodes checks, so the inline and standalone variants hold them. The delete request sits in the ignore block, so detection skips it.

Run setup and cleanup steps around one test

This test uses the test fields before and after, with goTo and find. The setup spec signs in by setting a cookie, and the cleanup spec signs out.

prose.md
# Check the session cookie
1. Start at [the cookies page](http://localhost:8092/cookies).
2. Check that the page lists `structure-session=signed-in`.

before and after each name one spec file, relative to the file that names them: a spec, or a document with an inline test statement. Doc Detective uses the steps of the first test in that file. It runs them in the same context as the test, so the test shares their browser and cookies. The before steps run first. The after steps run last, even when a step of the test fails.

The setup and cleanup specs:

sign-in.spec.json
{
"tests": [
{
"testId": "structure-before-after-sign-in",
"description": "Sign in by setting a session cookie.",
"steps": [
{
"goTo": "http://localhost:8092/cookies/set/structure-session/signed-in"
}
]
}
]
}
sign-out.spec.json
{
"tests": [
{
"testId": "structure-before-after-sign-out",
"description": "Sign out by replacing the session cookie.",
"steps": [
{
"goTo": "http://localhost:8092/cookies/set/structure-session/signed-out"
},
{
"goTo": "http://localhost:8092/cookies"
},
{
"find": "structure-session=signed-out"
}
]
}
]
}

What detection finds

Detection adds a checkLink step for the link, after the sign-in steps. Code spans aren’t detected, so the detected variants carry goTo and find statements, and they still open the page with detection off.

Show when a procedure last passed

This procedure uses goTo and find, the test field badge, and a meta statement with a verified-by entry.

prose.md
# Open the elements demo
1. Open [the elements demo](http://localhost:8092/index.html).
2. Check that the page shows **Common HTML Elements**.

After a passing run, Doc Detective fills in the meta statement and writes a badge above the test statement, in the same comment style:

inline.md, after a passing run
<!-- meta sha256-97f2… {"verified-by":[{"tool":"doc-detective","check":"structure-verified","version":"5.7.0","passed":"2026-11-26"}],"last-verified":"2026-11-26"} -->
![Last verified 2026-11-26](https://img.shields.io/badge/Last_verified-2026--11--26-brightgreen)
<!-- test {"testId":"structure-verified","badge":true,"detectSteps":false} -->

AsciiDoc gets an image: macro, HTML an <img> tag, and DITA an <image> element. The check names the test’s testId. A spec file has no line above a test, so the standalone spec can’t hold a badge. A meta statement on a page can still name its testId.

What Doc Detective writes depends on the config field verification.write:

ValueWrites
"existing" (default)Fills the verified-by entries and badges the pages already have.
trueAlso adds the entries. A passing inline test gets a meta statement and, when its test statement has none, a testId. A passing Markdown or MDX page gets a verified-by entry in its front matter.
falseNothing.

The examples on this page set verification.write to false, so a run doesn’t change the files. To see a write, run a copy:

npx doc-detective --input inline.md --verification true

The example leaves out reStructuredText. Doc Detective doesn’t write into .rst files, so a badge and a meta statement have no effect there. For the full workflow, see Show a “Last verified on” date.

Where a statement came from

Each test and step in the results has a location: the line and the character offsets of its statement. Doc Detective fills it in. For the detected Markdown file, the test points at its test statement, and the goTo step at the link it came from:

detected.md, in the results
{
"testId": "structure-verified",
"location": { "line": 4, "startIndex": 112, "endIndex": 189 },
"steps": [
{
"goTo": "http://localhost:8092/index.html",
"location": { "line": 6, "startIndex": 199, "endIndex": 252 }
}
]
}

Include a shared check in a page

Each page includes a shared file that holds its own test, then runs a test of its own. It uses httpRequest, goTo, and find, and the config fields resolveIncludes and fileTypes with includeStatements.

prose.md
# Open the elements demo
--8<-- "_check-status.md"
1. Open [the elements demo](http://localhost:8092/index.html).
2. Check that the page shows **Text Inputs**.

Each format includes its own shared file. The MDX page includes the Markdown file:

_check-status.md
<!-- test {"testId":"structure-includes-status","detectSteps":false} -->
Before you start, check that the API answers:
```http
GET http://localhost:8092/status/200
```
<!-- step {"httpRequest":{"url":"http://localhost:8092/status/200","statusCodes":[200]}} -->
<!-- test end -->
FormatInclude
Markdown--8<-- "_check-status.md" (MkDocs snippets)
MDX<Markdown src="./_check-status.md" />
AsciiDocinclude::_check-status.adoc[]
HTML<!--#include file="_check-status.html" -->, through includeStatements
reStructuredText.. include:: _check-status.rst

With resolveIncludes on, the default, the shared test runs inside each page, before the page’s own test, so the standalone spec holds two tests. Its location gains a file field with the shared file’s absolute path. The shared file doesn’t run on its own. With resolveIncludes off, each page runs only its own test, and the shared file runs as its own spec.

HTML has no include syntax of its own, so the example’s config adds Apache-style server-side includes to HTML:

doc-detective.json
{
"resolveIncludes": true,
"fileTypes": [
"markdown",
"asciidoc",
"rst",
"dita",
{
"extends": "html",
"extensions": "html",
"includeStatements": ["<!--#include\\s+file=\"([^\"]+)\"\\s*-->"]
}
]
}

Each includeStatements entry is a regular expression whose first group is the path to include. extensions and includeStatements each take a string or a list. A fileTypes list replaces the default list, so it names the other formats too.

The example leaves out DITA. DITA reuses content through conrefs and keyrefs, which a later example covers. For every include syntax, see Test pages that include other files.

Include a shared check with mdBook or an MDX import

The same procedure, with the shared file included the way mdBook and MDX component frameworks include it. It uses httpRequest, goTo, find, and the config field resolveIncludes.

prose.md
# Open the elements demo
{{#include _check-status.md}}
1. Open [the elements demo](http://localhost:8092/index.html).
2. Check that the page shows **Text Inputs**.
FormatInclude
Markdown{{#include _check-status.md}} (mdBook)
MDXimport CheckStatus from './_check-status.md';, then <CheckStatus /> (Docusaurus, Mintlify)

Doc Detective knows both forms, so the example’s config only turns resolveIncludes on. An MDX import counts as an include only where the page renders the component. An import with no matching component tag includes nothing.

Test the topics in a DITA map

This DITA topic uses a key that its map defines. It uses goTo, find, and the config field processDitaMaps.

prose.dita
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE task PUBLIC "-//OASIS//DTD DITA Task//EN" "task.dtd">
<task id="find-selection-elements">
<title>Find the selection elements</title>
<taskbody>
<steps>
<step><cmd>Open <xref keyref="elements-demo">the elements demo</xref>.</cmd></step>
<step><cmd>Check that the page shows <b>Selection Elements</b>.</cmd></step>
</steps>
</taskbody>
</task>

The map defines the elements-demo key and references the topic:

structure.ditamap
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE map PUBLIC "-//OASIS//DTD DITA Map//EN" "map.dtd">
<map>
<title>Elements demo guide</title>
<keydef keys="elements-demo" href="http://localhost:8092/index.html" scope="external" format="html"/>
<topicref href="inline.dita"/>
</map>

When an input is a .ditamap file, Doc Detective runs DITA-OT on it (dita -i <map> -f dita) and tests the resolved topics. The keyref becomes a link with the key’s URL, as a reader sees it. This needs the dita command on your PATH:

npx doc-detective --input structure.ditamap

Without the dita command, Doc Detective logs an error and skips the map. When you pass the topic or a folder, Doc Detective reads the topic as written, with its keyref unresolved. The inline steps name their URL, so they run either way.

processDitaMaps is true by default, and the example’s config sets it explicitly. To read a map as a plain DITA file instead, without DITA-OT, turn it off:

.doc-detective.json
{
"processDitaMaps": false
}

The example leaves out detected tests. A detected goTo comes from a link’s href, and an unresolved keyref has none, so detection depends on whether DITA-OT ran. The example leaves out the other formats, which have no maps.

detectSteps permutations

Each example checks two links. Its config sets detectSteps, and its test statement sets detectSteps or leaves it out. Detection runs only when both allow it. A test statement can turn detection off, but it can’t turn detection on when the config has it off. When detection runs, each link becomes a checkLink step beside the inline step, so the test checks each link twice. These examples have prose and inline variants only.

true in config, unset in the test

Varies: detectSteps · Value: config true, test unset · Effect: the test inherits detection, so each link gets a detected step and an inline step.

prose.md
# Check the demo pages
1. Check that [the elements demo](http://localhost:8092/index.html) loads.
2. Check that [the type echo page](http://localhost:8092/type-echo.html) loads.

true in config, true in the test

Varies: detectSteps · Value: config true, test true · Effect: detection runs, so each link gets a detected step and an inline step.

prose.md
# Check the demo pages
1. Check that [the elements demo](http://localhost:8092/index.html) loads.
2. Check that [the type echo page](http://localhost:8092/type-echo.html) loads.

true in config, false in the test

Varies: detectSteps · Value: config true, test false · Effect: the test turns detection off, so only the inline steps run.

prose.md
# Check the demo pages
1. Check that [the elements demo](http://localhost:8092/index.html) loads.
2. Check that [the type echo page](http://localhost:8092/type-echo.html) loads.

false in config, unset in the test

Varies: detectSteps · Value: config false, test unset · Effect: the test inherits detection off, so only the inline steps run.

prose.md
# Check the demo pages
1. Check that [the elements demo](http://localhost:8092/index.html) loads.
2. Check that [the type echo page](http://localhost:8092/type-echo.html) loads.

false in config, true in the test

Varies: detectSteps · Value: config false, test true · Effect: detection stays off, because a test can’t turn on what the config turned off.

prose.md
# Check the demo pages
1. Check that [the elements demo](http://localhost:8092/index.html) loads.
2. Check that [the type echo page](http://localhost:8092/type-echo.html) loads.

false in config, false in the test

Varies: detectSteps · Value: config false, test false · Effect: detection stays off, so only the inline steps run.

prose.md
# Check the demo pages
1. Check that [the elements demo](http://localhost:8092/index.html) loads.
2. Check that [the type echo page](http://localhost:8092/type-echo.html) loads.

testId permutations

testId: "structure-testid-set"

Varies: test.testId · Value: "structure-testid-set" · Effect: results and verified-by entries name the test by this ID.

prose.md
# Check the demo pages
1. Check that [the elements demo](http://localhost:8092/index.html) loads.
2. Check that [the type echo page](http://localhost:8092/type-echo.html) loads.

testId omitted

Varies: test.testId · Value: omitted · Effect: the ID joins the spec ID and a hash of the test, such as <path>~92b45990.

prose.md
# Check the demo pages
1. Check that [the elements demo](http://localhost:8092/index.html) loads.
2. Check that [the type echo page](http://localhost:8092/type-echo.html) loads.

Tests per file permutations

One test

Varies: tests per file · Value: one · Effect: all three checks run in one test, so a failure stops the checks after it.

prose.md
# Check the demo pages
1. Check that [the elements demo](http://localhost:8092/index.html) loads.
2. Check that [the type echo page](http://localhost:8092/type-echo.html) loads.
3. Check that [the status endpoint](http://localhost:8092/status/200) answers.

Several tests

Varies: tests per file · Value: two · Effect: each section runs as its own test, so a failure in one doesn’t stop the other.

prose.md
# Check the demo pages
## Pages
1. Check that [the elements demo](http://localhost:8092/index.html) loads.
2. Check that [the type echo page](http://localhost:8092/type-echo.html) loads.
## API
1. Check that [the status endpoint](http://localhost:8092/status/200) answers.

test end permutations

test end explicit

Varies: test end statement · Value: present · Effect: each test ends where its test end statement sits.

prose.md
# Check the demo pages
## Pages
1. Check that [the elements demo](http://localhost:8092/index.html) loads.
## API
1. Check that [the status endpoint](http://localhost:8092/status/200) answers.

test end implied

Varies: test end statement · Value: omitted · Effect: the next test statement ends the previous test, and the end of the file ends the last one.

prose.md
# Check the demo pages
## Pages
1. Check that [the elements demo](http://localhost:8092/index.html) loads.
## API
1. Check that [the status endpoint](http://localhost:8092/status/200) answers.

Ignore block permutations

Around a step statement

Varies: ignore block placement · Value: around one step statement · Effect: the inline variant skips that step, but detection still finds the link before the block.

prose.md
# Check the demo pages
1. Check that [the elements demo](http://localhost:8092/index.html) loads.
2. Check that [the type echo page](http://localhost:8092/type-echo.html) loads.

Around a list item

Varies: ignore block placement · Value: around the second list item · Effect: both variants skip the item’s link and step. The items around it still run.

prose.md
# Check the demo pages
1. Check that [the elements demo](http://localhost:8092/index.html) loads.
2. Check that [the type echo page](http://localhost:8092/type-echo.html) loads.
3. Check that [the status endpoint](http://localhost:8092/status/200) answers.

Around a whole test

Varies: ignore block placement · Value: around the second test · Effect: the second test has no steps, so Doc Detective drops it.

prose.md
# Check the demo pages
## Pages
1. Check that [the elements demo](http://localhost:8092/index.html) loads.
## API
1. Check that [the status endpoint](http://localhost:8092/status/200) answers.

A test end statement also ends an open ignore block, so in this example the test ignore end statement after it has no effect.

before and after permutations

These examples are standalone specs. A before or after field on an inline test statement works the same way. For both fields together, in every format, see Run setup and cleanup steps around one test.

before only

Varies: test.before · Value: "sign-in.spec.json" · Effect: the sign-in steps run first, in the test’s own browser, so the test sees the cookie they set.

standalone.spec.json
{
"$schema": "https://raw.githubusercontent.com/doc-detective/doc-detective/main/src/common/dist/schemas/spec_v3.schema.json",
"tests": [
{
"testId": "structure-test-before",
"before": "sign-in.spec.json",
"steps": [
{
"goTo": "http://localhost:8092/cookies"
},
{
"find": "structure-session=signed-in"
}
]
}
]
}

after only

Varies: test.after · Value: "sign-out.spec.json" · Effect: the sign-out steps run last, in the test’s own browser, even when a step of the test fails.

standalone.spec.json
{
"$schema": "https://raw.githubusercontent.com/doc-detective/doc-detective/main/src/common/dist/schemas/spec_v3.schema.json",
"tests": [
{
"testId": "structure-test-after",
"after": "sign-out.spec.json",
"steps": [
{
"goTo": "http://localhost:8092/cookies/set/structure-session/signed-in"
},
{
"goTo": "http://localhost:8092/cookies"
},
{
"find": "structure-session=signed-in"
}
]
}
]
}

beforeAll and afterAll permutations

Each example’s config lists the server setup spec and one spec of its own. The shared config of these examples sets each field to a single path, the string form. For both lists together, see Create and look up users.

beforeAll list

Varies: beforeAll · Value: ["../../../test-setup/start-server.spec.json", "check-api.spec.json"] · Effect: both specs run, in order, before any input.

prose.md
# Check the demo pages
1. Check that [the elements demo](http://localhost:8092/index.html) loads.
2. Check that [the type echo page](http://localhost:8092/type-echo.html) loads.

afterAll list

Varies: afterAll · Value: ["check-api.spec.json", "../../../test-setup/stop-server.spec.json"] · Effect: both specs run, in order, after every input.

prose.md
# Check the demo pages
1. Check that [the elements demo](http://localhost:8092/index.html) loads.
2. Check that [the type echo page](http://localhost:8092/type-echo.html) loads.

resolveIncludes permutations

With resolveIncludes on, see Include a shared check in a page and Include a shared check with mdBook or an MDX import. Each page then runs two tests: the shared file’s test and its own.

Include syntaxresolveIncludes: trueresolveIncludes: false
MkDocs --8<--Shared test runs inside the pageDirective stays text
mdBook {{#include}}Shared test runs inside the pageDirective stays text
Fern <Markdown src>Shared test runs inside the pageComponent isn’t read
MDX import with a componentShared test runs inside the pageComponent isn’t read
AsciiDoc include::Shared test runs inside the pageDirective stays text
reStructuredText .. include::Shared test runs inside the pageDirective isn’t read
includeStatements matchShared test runs inside the pageComment isn’t read

resolveIncludes: false, built-in and custom syntax

Varies: resolveIncludes · Value: false · Effect: each page runs only its own test, and the shared file runs only when it’s an input itself.

prose.md
# Open the elements demo
--8<-- "_check-status.md"
1. Open [the elements demo](http://localhost:8092/index.html).
2. Check that the page shows **Text Inputs**.

resolveIncludes: false, mdBook and MDX import

Varies: resolveIncludes · Value: false · Effect: each page runs only its own test, and the shared file runs only when it’s an input itself.

prose.md
# Open the elements demo
{{#include _check-status.md}}
1. Open [the elements demo](http://localhost:8092/index.html).
2. Check that the page shows **Text Inputs**.

badge permutations

A badge appears only after a passing run that writes back. These examples use the default verification.write, "existing".

badge: true

Varies: test.badge · Value: true · Effect: a passing run writes a “Last verified” badge on the line above the test statement.

prose.md
# Check the demo pages
1. Check that [the elements demo](http://localhost:8092/index.html) loads.
2. Check that [the type echo page](http://localhost:8092/type-echo.html) loads.

In reStructuredText, badge has no effect.

badge: false

Varies: test.badge · Value: false · Effect: a passing run writes no badge, the same as leaving badge out.

prose.md
# Check the demo pages
1. Check that [the elements demo](http://localhost:8092/index.html) loads.
2. Check that [the type echo page](http://localhost:8092/type-echo.html) loads.

verification.write permutations

Each example sets verification.write in its config and has a test statement with badge: true. The examples leave out reStructuredText, because Doc Detective doesn’t write into .rst files. To try a value without changing your files, run a copy.

verification.write: "existing"

Varies: verification.write · Value: "existing" · Effect: a passing run fills the meta statement and the badge the page already declares.

prose.md
# Check the demo pages
1. Check that [the elements demo](http://localhost:8092/index.html) loads.
2. Check that [the type echo page](http://localhost:8092/type-echo.html) loads.

verification.write: true

Varies: verification.write · Value: true · Effect: a passing run adds a meta statement and writes the generated testId into the test statement.

prose.md
# Check the demo pages
1. Check that [the elements demo](http://localhost:8092/index.html) loads.
2. Check that [the type echo page](http://localhost:8092/type-echo.html) loads.

In Markdown and MDX, a passing run also adds a verified-by entry to the front matter.

verification.write: false

Varies: verification.write · Value: false · Effect: a run writes nothing, so the meta statement and the badge stay unchanged.

prose.md
# Check the demo pages
1. Check that [the elements demo](http://localhost:8092/index.html) loads.
2. Check that [the type echo page](http://localhost:8092/type-echo.html) loads.