Skip to content

HTML Comment Assertions ​

HTML comments are the primary way to add assertions in DocTest. They're invisible in rendered documentation, keeping your examples clean.

Syntax ​

HTML comment assertions are placed immediately after the code block:

markdown
```php
echo 'Hello';
```
<!-- doctest: Hello -->

Available Types ​

HTML CommentAssertion TypeDescription
<!-- doctest: value -->OutputExact output match
<!-- doctest-contains: value -->OutputContainsPartial output match
<!-- doctest-matches: /pattern/ -->OutputMatchesRegex pattern match
<!-- doctest-json: {...} -->OutputJsonJSON structure comparison
<!-- doctest-expect: expr -->ExpectExpression must be truthy
<!-- doctest-output -->Display OutputDisplay-only block, refreshed by --update, never asserted

Parsing Rules ​

  • The comment must start with <!-- and end with -->
  • The keyword (doctest, doctest-contains, etc.) is case-sensitive
  • Whitespace around the value is trimmed
  • Multi-line values are supported

Multi-line Values ​

For multi-line expected output, the value spans across lines within the HTML comment:

markdown
```php
echo "line 1\nline 2\nline 3";
```
<!-- doctest: line 1
line 2
line 3 -->

Wildcards ​

HTML comment assertions support wildcards:

markdown
```php
echo 'Generated ID: ' . uniqid();
```
<!-- doctest: Generated ID: {{any}} -->

Example ​

php
echo 'DocTest makes documentation testing easy';

Why HTML Comments? ​

  1. Clean rendering — Readers see only the code, not the test
  2. No PHP syntax conflicts — The assertion is outside the PHP code block
  3. Works with any renderer — GitHub, VitePress, GitBook, etc. all hide HTML comments
  4. Same power — All assertion types are available

Released under the MIT License.