readme-example-tester
Do not let your README/documentation examples become invalid: unit test them!
Install
pip install readme-example-tester
Usage
Mark the code blocks that you want to test.
Check out this crazy snippet:
<!-- example-id: tests/sample_pump_it_up.py -->
```python
def pump_it_up(input):
return input + 100
```
Then create a unit test for that README, such as:
from pathlib import Path
from readme_example_tester import ReadmeTestCase
class TestReadme(ReadmeTestCase):
# assuming test is in tests/ and README.md in root
README_PATH = Path(__file__).parent.parent / 'README.md'
TESTS_DIR = Path(__file__).parent
Whenever this test case runs, 3 tests are executed:
test_readme_example_targets_have_tests: ensures every snippet is covered by a test casetest_readme_code_blocks_match_example_targets: ensures every snippet matches an existing filetest_examples_are_still_in_use: ensures all files insideTESTS_DIRmatchingSAMPLE_FILE_GLOB(sample_*) are still being used in the README
These tests, plus the sample-file coverage checks, keep README examples honest.
Dogfooding
This project README is actually covered by tests/test_dogfood.py
And the inner example is in tests/sample_one.md and covered by tests/test_sample_one_readme.py
Advanced usage
Custom marker
class DemoReadme(ReadmeTestCase):
README_PATH = Path(__file__).parent / 'README.md'
TESTS_DIR = Path(__file__).parent
README_MARKER = 'demo-id'
<!-- demo-id: tests/some_sample.py -->
```python
def pump_it_up(input):
return input + 100
```
Assert snippet outputs
Use <!-- example-id-output: ... --> (or README_MARKER-output if customized) to compare/validate outputs from an executable sample script.
Expanding the previous example to be executable
#!/usr/bin/env python3
def pump_it_up(input):
return input + 100
def main(argv):
if not argv:
print('No pump')
else:
print(pump_it_up(int(argv[0])))
if __name__ == '__main__':
import sys
main(sys.argv[1:])
<!-- example-id-output: tests/sample_pump_it_up_cli.py 2 -->
```text
$ tests/sample_pump_it_up_cli.py 2
102
```
Partial snippets
Use # README+++ / # README--- in a sample file to narrow the excerpt that matches the README block when the source file has extra boilerplate.
If one sample file feeds multiple README blocks, use # README:<id>+++ / # README:<id>--- to split the source into named sections and then use <!-- example-id:<id> tests/... --> in the code snippets
Non-sample sample files
If you have a test file that matches SAMPLE_FILE_GLOB but it is not expected to be used in README, you can add # README-EXCLUDE
Development
Project setup and local checks live in CONTRIBUTING.md.
Metadata
Release files for readme-example-tester 0.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| readme_example_tester-0.0.2.tar.gz | 8.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| readme_example_tester-0.0.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 15.5 kB
Release files / readme_example_tester-0.0.2.tar.gz
| Download URL | readme_example_tester-0.0.2.tar.gz |
|---|---|
| Size | 8.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
1195c7bef26e53d3ad779b540ed5009e981d8ad6eac3f2b8b4081997b6b8405e
|
|
BLAKE2b-256 checksum How to use checksums |
b728d48f9eecc0114386d8529a2f45d520024d2016011852ea198acf8f5de9a6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.10.20
|
Release files / readme_example_tester-0.0.2-py3-none-any.whl
| Download URL | readme_example_tester-0.0.2-py3-none-any.whl |
|---|---|
| Size | 7.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
6f7a8f41e3ade82f05d2fdba364c01f346263e7a5f64a2af13a1b17dc7c73342
|
|
BLAKE2b-256 checksum How to use checksums |
f58283e227cc1939de0651957536151f17d8c17e326676c439605b41bdacd2cc
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.10.20
|