On this page
The short answerWhat byexample finds and runsRun a page with two languagesControl execution and matchingKeep setup visible to readersInvestigate a broken API exampleCompare tools and triage resultsWhere DocsAlive fitsPasteable checklistThe sources we checkedFAQThe short answer
Use byexample to run prompt-based examples and their expected output across the languages you explicitly select. Inspect the number of examples run, keep setup understandable to readers, and use relaxed matching only where variation is expected. A passing file does not establish coverage for a language omitted from -l.
In engine 0.6.3, DocsAlive ignores byexample options in comments and skips multi-line HTML comments. A python fence containing >>> prompts can produce an invalid-syntax false positive; a shell fence runs as a script, including prompt and output lines. Use pycon and console fences for the respective sessions. The engine runs $ lines in console fences without comparing their output; it compares printed output in Python pycon and python-repl sessions.
What byexample finds and runs
byexample is a literate programming engine: examples and expected output in documentation become regression tests. Its home page lists Python, Ruby, Shell, GDB, JavaScript, C/C++, Java, PowerShell, iasm, Go and Rust, with pages for Elixir and PHP.
In Markdown, it searches fenced blocks and HTML comments; text outside a block or comment is ignored. In .py files it searches docstrings. It searches comments in .rb, .sh, .cpp, .h, .js and .php files, and searches anywhere in other files.
The prompt determines the example’s language, not the fence label. For example, >>> identifies Python and $ identifies Shell. An example ends at the next prompt, a blank line, text with lower indentation, or the end of the block or file. These boundaries matter when a displayed result contains blank lines or when nearby prose resembles output.
byexample compares each selected example’s output with the expected output and reports differences. Its per-file summary includes pass, fail and skip counts and a status. Read those counts alongside the status: a file can pass even when an entire language was not selected.
Run a page with two languages
The package names acme-slug and acme_slug in this guide are illustrative, not published packages. Install byexample in an environment that can run the examples you intend to test:
python -m pip install byexample
This illustrative Markdown page uses Python prompts in pycon fences and shell prompts in a console fence:
# Usage
Make a slug:
```pycon
>>> from acme_slug import slugify, truncate_slug
>>> slugify("Hello, World")
'hello-world'
```
Shorten one:
```pycon
>>> truncate_slug("docs-that-run-everywhere", 13)
'docs-that-run'
```
Check the files from a shell:
```console
$ echo "acme-slug docs"
acme-slug docs
$ ls acme_slug/*.py
acme_slug/__init__.py
```
Select both languages when you run the page:
byexample -l python,shell docs/usage.md
The checked run executed five examples and passed:
File docs/usage.md, 5/5 test ran in 0.01 seconds
[PASS] Pass: 5 Fail: 0 Skip: 0
Selecting only Python produced a pass too, but ran three examples rather than five:
File docs/usage.md, 3/3 test ran in 0.01 seconds
[PASS] Pass: 3 Fail: 0 Skip: 0
The two shell examples were left out silently. Maintain the language list with the page, especially when a contributor adds a new kind of session. A green status is useful only when the selected languages and collected examples match the intended coverage.
The fence label distinction also matters when sharing the page with a scan. byexample accepted the same examples under python and shell labels in the checked runs because it identifies prompts. Engine 0.6.3 instead treats a python fence as a script and a shell fence as a shell script. The prompts and displayed shell lines can therefore cause failures unrelated to the intended session. pycon and console make the intended form legible to both tools.
Control execution and matching
The usage options let you adjust the run without changing the page. --ff or --fail-fast stops at the first failure and skips the rest. --timeout sets the per-example timeout, which defaults to two seconds. -j selects parallel jobs; the default is one, and cpu uses all CPUs. --dry parses without running, --skip excludes specified files, and -d chooses a diff format: none, unified, ndiff, context or tool. The default diff format is none.
Per-example options offer more local control. In the following illustrative page, +pass runs an example without checking output, +norm-ws treats a run of whitespace as one, +skip does not run its example, and +timeout=5 permits the slow command more time. The capture tag <...> matches any text:
# Options
```pycon
>>> import time
>>> print("took", round(time.time() % 1, 3), "seconds") # byexample: +pass
took 0.5 seconds
>>> print("a b") # byexample: +norm-ws
a b
>>> print("build id", 4821, "ok")
build id <...> ok
>>> import acme_missing_client # byexample: +skip
```
```console
$ sleep 3; echo done # byexample: +timeout=5
done
```
The checked run reported five passed and one skipped:
File docs/options.md, 6/6 test ran in 3.01 seconds
[PASS] Pass: 5 Fail: 0 Skip: 1
Without the per-example timeout, the three-second command timed out under the default. Choose a timeout that reflects the operation under test, and investigate changes in duration rather than raising it without explanation.
Use +pass and <...> sparingly. They are useful for unstable data, but they can also conceal a meaningful output change. A named capture tag, <name>, captures output that can be pasted into a later example with +paste. That is useful when a later step needs an earlier generated value; it also creates a dependency that should be understandable when the page is maintained.
The engine does not interpret these byexample comments or capture tags. In the checked options page, it attempted the import marked +skip and classified the missing package as a missing install step. On a copy without that line, the pycon block received an output-difference warning because the byexample matching options had no meaning to the engine. Treat those findings as differences between the tools’ rules and review them against the page’s intent.
Keep setup visible to readers
A Python session in one file shares an interpreter. This can make a setup example in an HTML comment available to a later visible example. The next illustrative page demonstrates the consequence:
# Hidden setup
<!--
>>> from acme_slug import slugify
-->
The import above is hidden from readers. The visible example:
```pycon
>>> slugify("Docs That Run")
'docs-that-run'
```
byexample passed both examples in the checked run. A reader viewing the rendered page, however, would not see the import. Engine 0.6.3 skips the multi-line comment entirely, so the visible example was classified as depending on context.
That finding is a useful review prompt. If the reader needs the import to follow the page, show it or clearly link to the prerequisite. If the hidden setup is intentional test scaffolding, record that fact and accept that the visible block alone does not demonstrate the complete reader path. See README testing for the same issue in a repository’s entry page.
Investigate a broken API example
The following change is broken on purpose for the existing page. It changes the default separator and renames a function:
--- a/acme_slug/__init__.py
+++ b/acme_slug/__init__.py
@@ -1,7 +1,7 @@
"""Tiny slug helpers used to demonstrate xdoctest."""
-def slugify(text, sep="-"):
+def slugify(text, sep="_"):
"""Turn a title into a URL slug.
Example:
@@ -19,10 +19,10 @@
return sep.join(keep.split())
-def truncate_slug(slug, limit):
+def shorten_slug(slug, limit):
"""Cut a slug at a word boundary.
- >>> truncate_slug("docs-that-run-everywhere", 13)
+ >>> shorten_slug("docs-that-run-everywhere", 13)
'docs-that-run'
"""
if len(slug) <= limit:
The full byexample run reported three failures and two passes. The shortened output shows the failed import and the changed result:
File "docs/usage.md", line 6
Failed example:
from acme_slug import slugify, truncate_slug
Expected nothing
Got:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
from acme_slug import slugify, truncate_slug
ImportError: cannot import name 'truncate_slug' from 'acme_slug' (.../acme_slug/__init__.py)
File "docs/usage.md", line 7
Failed example:
slugify("Hello, World")
Expected:
'hello-world'
Got:
'hello_world'
...
File docs/usage.md, 5/5 test ran in 0.01 seconds
[FAIL] Pass: 2 Fail: 3 Skip: 0
Start with the earliest failing example, then rerun after fixing it. Later failures may be independent changes or consequences of missing state. The engine classified the import block as API drift and the following block as context-dependent; the console block passed.
For CI, fail-fast can make the first problem easier to locate:
byexample -l python,shell --ff docs/*.md
On the broken page, the checked fail-fast run reported one failure and four skips. On the intact illustrative pages, the command exited successfully. A fail-fast result is a fast signal, not a full inventory of remaining failures; run again after repair or use a full run when you need the complete list.
Compare tools and triage results
| byexample feature | What byexample does | What the engine does in 0.6.3 |
|---|---|---|
| Language selection | Runs examples in languages selected with -l |
Plans supported fences by their labels |
Prompt in a python fence |
Recognizes a Python session from the prompt | Runs the fence as a script and may flag invalid syntax |
console session |
Runs selected shell examples and checks expected output | Runs $ commands; displayed output is not compared |
| Comment options | Applies options such as +skip and +pass |
Ignores byexample options |
| Multi-line HTML comment | Can find examples inside it | Skips the comment entirely |
| Python state | Shares an interpreter within a file | Shares a Python namespace within a page |
| Capture tags | Matches or captures output according to byexample rules | Does not apply byexample capture rules |
| Finding | Likely cause | What to do |
|---|---|---|
| File passes with fewer examples than expected | A language was omitted from -l |
Select every language used and inspect counts |
| Output change is missed | +pass or <...> masks it |
Narrow the relaxation or add a meaningful check |
| A slow example fails | It exceeds the two-second default | Measure it and set an appropriate timeout |
| Scan cannot find a name | Setup is hidden in a multi-line comment | Make the setup visible or document the dependency |
| Prompt lines fail as syntax | An interactive session is labeled as a script | Use pycon or console as appropriate |
| A skipped import is attempted by the scan | The scan ignores byexample options | Review the finding with that limitation in mind |
Where DocsAlive fits
Keep byexample as the regression test for the prompt-based examples and matching rules you maintain. Use DocsAlive for a clean-sandbox scan of supported Markdown examples and classification of findings. The two reports answer different questions, particularly when a page uses hidden setup or byexample-specific matching.
Engine 0.6.3 reads Markdown file types in its scan scope, not source-file docstrings or comments. It skips multi-line HTML comments. It also has a known limitation for unknown fence labels: a block can be reported as passing without being run or parsed. Inspect coverage and skip reasons before treating a green report as evidence of execution.
For other approaches, see CLI docs testing, Python docs testing, Prysk and Cram docs testing and xdoctest for docstrings. The findings reference explains how scan categories help separate likely documentation bugs from cases needing context.
Pasteable checklist
[ ] List every language represented on each page.
[ ] Include every intended language in the byexample -l selection.
[ ] Inspect run and skip counts, not just the final status.
[ ] Use pycon for Python sessions and console for shell sessions.
[ ] Keep reader-required setup visible.
[ ] Review uses of +skip, +pass, +norm-ws and +timeout=5.
[ ] Use capture tags only where their flexibility is intentional.
[ ] Investigate timeout failures and set realistic limits.
[ ] Run a full check after a fail-fast failure is fixed.
[ ] Review scan findings against the tools' different rules.
The sources we checked
These sources were read on November 16, 2026. Engine behavior was checked against engine 0.6.3 on the same day.
| Source | What it covers |
|---|---|
| byexample home | Project overview and languages |
| Usage | Running examples and command options |
| Where to write examples | Supported locations and example boundaries |
| Skip and pass | Per-example execution and output options |
| Capture and paste | Capture tags and reuse |
| Normalize whitespace | Whitespace matching |
| Timeout | Per-example timeout behavior |
FAQ
Does byexample use the Markdown fence label to choose a language?
No. It identifies the language from the prompt. Select the languages to run with -l, and inspect the number of examples run because omitted languages can leave a file passing.
Where can byexample find examples?
It searches Markdown fenced blocks and HTML comments, Python docstrings, and comments in several source file types. For other file types it searches anywhere in the file.
Why did a slow command fail?
The default timeout is two seconds per example. Investigate the duration, then use an appropriate run-wide timeout or a per-example option such as +timeout=5 when the longer duration is expected.
Can a passing example hide an output change?
Yes. +pass runs without checking output, while the capture tag <...> can match varying text. Use those features deliberately and keep important behavior under a meaningful check.
How does DocsAlive handle byexample pages?
Engine 0.6.3 ignores byexample comment options and skips multi-line HTML comments. Prompted Python in a python fence can cause an invalid-syntax false positive, and a shell fence runs prompt and output lines as script text. Use pycon and console fences: console runs dollar-prompt commands without comparing output, while pycon printed output is compared.
Should I use fail-fast in CI?
It can provide a focused first failure, but remaining examples are skipped after that failure. Rerun after fixing it or use a full run when you need an inventory of all failures.




