Skip to content

Attributes Overview ​

Attributes control how DocTest handles each code block. They can be written in two ways:

  1. Info string — after php in the code fence (traditional)
  2. HTML comment — in a <!-- doctest-attr: ... --> comment before the block (preserves editor syntax highlighting)

Syntax ​

Attributes appear after the language identifier in the code fence:

markdown
```php ignore
// This block is skipped
```

```php throws(RuntimeException)
throw new RuntimeException('Error');
```

```php group="database" setup
$pdo = new PDO('sqlite::memory:');
```

Available Attributes ​

AttributeDescriptionExample
ignoreSkip the block entirelyphp ignore
no_runSyntax check only, don't executephp no_run
throwsExpect an exceptionphp throws(RuntimeException)
parse_errorExpect a parse errorphp parse_error
groupGroup blocks sharing statephp group="name"
setupSetup code for a groupphp setup group="name"
teardownTeardown code for a groupphp teardown group="name"
bootstrapLoad bootstrap profilesphp bootstrap="laravel"

Combining Attributes ​

Some attributes can be combined:

markdown
```php setup group="database"
// Both setup and group
```

```php teardown group="database"
// Both teardown and group
```

Alternative: HTML Comment Syntax ​

All attributes above can also be specified via HTML comments before the code block. This preserves editor syntax highlighting. See the HTML Comment Syntax page for details.

markdown
<!-- doctest-attr: group="database" setup -->
```php
$pdo = new PDO('sqlite::memory:');
```

Processing Order ​

When a block has attributes, DocTest processes them in this order:

  1. ignore — Skip immediately, no further processing
  2. no_run — Syntax check only (php -l)
  3. parse_error — Execute and expect non-zero exit code
  4. throws — Execute with try/catch wrapper
  5. group/setup/teardown — Group execution with shared state

Released under the MIT License.