A tool for testing markdown documentation
Low maintenance activity. Last release 16 months ago. Weaker on recent releases and release cadence.
pip install mktestdocsREADME as published on PyPI with version 0.2.5.
mktestdocs
Run pytest against markdown files/docstrings.
Installation
pip install mktestdocs
Usage
Let's say that you're using mkdocs for your documentation. Then you're writing down markdown to explain how your Python packages work. It'd be a shame if a codeblock had an error in it, so it'd be great if you could run your unit tests against them.
This package allows you to do just that. Here's an example:
import pathlib
import pytest
from mktestdocs import check_md_file
# Note the use of `str`, makes for pretty output
@pytest.mark.parametrize('fpath', pathlib.Path("docs").glob("**/*.md"), ids=str)
def test_files_good(fpath):
check_md_file(fpath=fpath)
This will take any codeblock that starts with ```python and run it, checking for any errors that might happen. This means that if your docs contain asserts, that you get some unit-tests for free!
Multiple Code Blocks
Let's suppose that you have the following markdown file:
This is a code block
```python
from operator import add
a = 1
b = 2
```
This code-block should run fine.
```python
assert add(1, 2) == 3
```
Then in this case the second code-block depends on the first code-block. The standard settings of check_md_file assume that each code-block needs to run independently. If you'd like to test markdown files with these sequential code-blocks be sure to set memory=True.
import pathlib
from mktestdocs import check_md_file
fpath = pathlib.Path("docs") / "multiple-code-blocks.md"
try:
# Assume that cell-blocks are independent.
check_md_file(fpath=fpath)
except NameError:
# But they weren't
pass
# Assumes that cell-blocks depend on each other.
check_md_file(fpath=fpath, memory=True)
Markdown in Docstrings
You might also have docstrings written in markdown. Those can be easily checked as well.
# I'm assuming that we've got a library called dinosaur
from dinosaur import roar, super_roar
import pytest
from mktestdocs import check_docstring
# Note the use of `__name__`, makes for pretty output
@pytest.mark.parametrize('func', [roar, super_roar], ids=lambda d: d.__name__)
def test_docstring(func):
check_docstring(obj=func)