Skip to content

Commit a464108

Browse files
committed
test: Check the examples in README.md
This extracts the examples into temporary files and runs them through pytest. Let's make sure that these don't break again in the future!
1 parent 3c62a3b commit a464108

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

tests/test_readme_examples.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# SPDX-License-Identifier: LGPL-3.0-or-later
2+
3+
"""Test that code examples in README.md actually work"""
4+
5+
# pylint does not understand pytest fixtures..
6+
# pylint: disable=redefined-outer-name
7+
8+
__author__ = "Martin Pitt"
9+
__copyright__ = """
10+
(c) 2026 Martin Pitt <martin@piware.de>
11+
"""
12+
13+
import re
14+
import subprocess
15+
import sys
16+
from pathlib import Path
17+
18+
import pytest
19+
20+
21+
@pytest.fixture(scope="module")
22+
def readme_blocks():
23+
"""Extract Python code blocks from README.md"""
24+
readme_path = Path(__file__).parent.parent / "README.md"
25+
return re.findall(r"```python\n(.*?)\n```", readme_path.read_text(), re.DOTALL)
26+
27+
28+
def test_examples_exist(readme_blocks):
29+
"""Verify that we found some Python code blocks"""
30+
assert len(readme_blocks) > 0
31+
32+
33+
def test_readme_examples(readme_blocks, tmp_path):
34+
"""Test all README examples by running them through pytest"""
35+
for i, code_block in enumerate(readme_blocks):
36+
test_file = tmp_path / f"test_readme_example_{i}.py"
37+
test_file.write_text(code_block)
38+
39+
subprocess.run(
40+
[sys.executable, "-m", "pytest", str(test_file), "-v"],
41+
check=True,
42+
)

0 commit comments

Comments
 (0)