A testing framework (plugin + test fixture)
for MkDocs projects
View the documentation on Read the Docs
Description
What problem does it solve?
Traditionally, the quickest way for maintainers of
an existing MkDocs website project
(or developers of an MkDocs plugin)
to check whether an MkDocs project builds correctly,
is to run mkdocs build (possibly with the --strict option).
It leaves the following issues open:
- This is a binary proposition: it worked or it didn't.
- It doesn't perform integrity tests on the pages; if something started to go wrong, the issue might emerge only later.
- If something went already wrong, it doesn't necessarily say where, or why.
MkDocs-Test
The purpose of Mkdocs-Test is to facilitate the comparison of source pages (Markdown files) and destination pages (HTML) in an MkDocs project.
MkDocs-Test is a framework composed of two parts:
-
an MkDocs plugin (
test): it creates a__test__directory, which contains the data necessary to map the pages of the website. -
a framework for conducting the test. The
DocProjectclass groups together all the information necessary for the tests on a specific MkDocs project.
📝 Technical Note
The plugin exports thenavobject, in the form of a dictionary of Page objects.
Usage
Installation
Requirements
- Python 3.8 or higher
From pypi
pip install mkdocs-test
Locally (Github)
pip install .
Or, to install the test dependencies (for testing this package, not MkDocs projects):
pip install .[test]
Installing the plugin
⚠️ The plugin is a pre-requisite
The framework will not work without the plugin (it exports the pages map into the__test__directory).
Declare the test plugin in your config file (typically mkdocs.yml):
plugins:
- search
- ...
- test
Performing basic tests
The choice of testing tool is up to you (the examples in this package were tested with pytest).
from mkdocs_test import DocProject
project = DocProject() # declare the project
# (by default, the directory where the program resides)
project.build(strict=False, verbose=False)
# build the project; these are the default values for arguments
assert project.success # return code of the build is zero (success) ?
print(project.build_result.returncode) # return code from the build
# perform automatic integrity checks (do pages exist?)
project.self_check()
Tests on a page
Each page of the MkDocs project can be tested separately
# find the page by relative pathname:
page = project.pages['index.md']
# find the page by name, filename or relative pathname:
page = project.get_page('index')
# easy, and naïve search on the target html page
assert "hello world" in page.html
# find the words "second page", under the header that contains "subtitle"
# at level 2; arguments header and header_level are optional
# the method returns the text so found (if found)
# the search is case insensitive
assert page.find_text_text('second page', header="subtitle", header_level=2)
⚠️ Two markdown versions
page.markdowncontains the markdown after possible transformations by plugins; whereaspage.source.markdowncontains the exact markdown in the file.If you wish to have the complete source file (including the frontmatter), use
page.source.text.
Testing the HTML
You can directly access the .find() and .find_all() methods
offered by BeautifulSoup.
page = project.get_page('foo')
headers = page.find_all('h2') # get all headers of level 2
for header in headers:
print(header.string)
script = page.find('script', type="module")
assert 'import foo' in script.string
Performing advanced tests
Reading the configuration file
print(project.config.site_name)
Accessing page metadata
page = project.get_page('index')
assert page.meta.foo = 'hello' # key-value pair in the page's frontmatter
Reading the log
# search in the trace (naïve)
assert "Cleaning site" in project.trace
# get all WARNING log entries
entries = myproject.find_entries(severity='WARNING')
# get the entry from source 'test', containing 'debug file' (case-insensitive)
entry = project.find_entry('debug file', source='test')
assert entry, "Entry not found"
License
MIT License
Metadata
Release files for mkdocs-test 0.6.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| mkdocs_test-0.6.0.tar.gz | 25.3 kB | Without provenance | Details for mkdocs_test-0.6.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| mkdocs_test-0.6.0-py3-none-any.whl | Python 3 | none | any | Details for mkdocs_test-0.6.0-py3-none-any.whl |
Total release size: 53.0 kB
Release files / mkdocs_test-0.6.0.tar.gz
| Download URL | mkdocs_test-0.6.0.tar.gz |
|---|---|
| Size | 25.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ea28fd1aa737a55a3529bd9fd8277874d7365c6644785fb2fd4ac974ab77e698
|
|
BLAKE2b-256 checksum How to use checksums |
ce8141a7f37dba567acb9f3b679f2a5a78002bd1374289d3ae09ea39cd0c36da
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.12
|
Release files / mkdocs_test-0.6.0-py3-none-any.whl
| Download URL | mkdocs_test-0.6.0-py3-none-any.whl |
|---|---|
| Size | 27.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
13016f0f2409a03baf63b7b9d5ac29852fe3f1048dfacd390d26adb226366336
|
|
BLAKE2b-256 checksum How to use checksums |
ea4dd18ae77bce8646d4eb31208a8d416e814a25281e8bbab3573be8069ff27c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.12
|
Security for mkdocs-test 0.6.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.