File: README.md

package info (click to toggle)
mkdocs-test 0.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 424 kB
  • sloc: python: 938; sh: 34; makefile: 5
file content (220 lines) | stat: -rw-r--r-- 6,228 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<div align="center">

![MkDocs-Test](logo.png)

#  A testing framework (plugin + test fixture)<br>for MkDocs projects


[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) 
![Language](https://img.shields.io/github/languages/top/fralau/mkdocs-test)
![Github](https://img.shields.io/github/v/tag/fralau/mkdocs-test?label=github%20tag)
![PyPI](https://img.shields.io/pypi/v/mkdocs-test)
![Downloads](https://img.shields.io/pypi/dm/mkdocs-test)
![Python Version](https://img.shields.io/badge/python-3.8%2B-blue.svg)

[View the documentation](https://mkdocs-test-plugin.readthedocs.io/en/latest/) on Read the Docs
  
</div>

<!-- To update, run the following command:
markdown-toc -i README.md 
-->

<!-- toc -->

- [A testing framework (plugin + test fixture)for MkDocs projects](#a-testing-framework-plugin--test-fixturefor-mkdocs-projects)
  - [Description](#description)
    - [What problem does it solve?](#what-problem-does-it-solve)
    - [MkDocs-Test](#mkdocs-test)
  - [Usage](#usage)
    - [Installation](#installation)
      - [Requirements](#requirements)
      - [From pypi](#from-pypi)
      - [Locally (Github)](#locally-github)
    - [Installing the plugin](#installing-the-plugin)
    - [Performing basic tests](#performing-basic-tests)
    - [Tests on a page](#tests-on-a-page)
    - [Testing the HTML](#testing-the-html)
  - [Performing advanced tests](#performing-advanced-tests)
    - [Reading the configuration file](#reading-the-configuration-file)
    - [Accessing page metadata](#accessing-page-metadata)
    - [Reading the log](#reading-the-log)
  - [License](#license)

<!-- tocstop -->

## Description

### What problem does it solve?

Traditionally, the quickest way for maintainers of 
an existing [MkDocs](https://www.mkdocs.org/) website project
(or developers of an [MkDocs plugin](https://www.mkdocs.org/dev-guide/plugins/)) 
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 `DocProject`
  class groups together all the information necessary for the tests on a
  specific MkDocs project. 


> 📝 **Technical Note** <br> The plugin exports the `nav` object,
> in the form of a dictionary of Page objects.

## Usage

### Installation 

#### Requirements

- Python 3.8 or higher 

#### From pypi

```sh
pip install mkdocs-test
```

#### Locally (Github)

```sh
pip install .
```

Or, to install the test dependencies (for testing _this_ package,
not MkDocs projects):

```sh
pip install .[test]
```

### Installing the plugin

> ⚠️ **The plugin is a pre-requisite** <br> 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`):

```yaml
plugins:
  - search
  - ...
  - test
```

### Performing basic tests

The choice of testing tool is up to you (the examples in this package 
were tested with
[pytest](https://docs.pytest.org/en/stable/)).

```python
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

```python
# 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** <br>  `page.markdown`
> contains the markdown after possible transformations
> by plugins; whereas `page.source.markdown` contains 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](https://www.crummy.com/software/BeautifulSoup/bs4/doc/#find-all).  

```python
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

```python
print(project.config.site_name)


```

### Accessing page metadata

```python
page = project.get_page('index')
assert page.meta.foo = 'hello' # key-value pair in the page's frontmatter
```

### Reading the log

```python
# 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