File: test_doc_examples.py

package info (click to toggle)
python-xmlsec 1.3.14-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 656 kB
  • sloc: ansic: 4,118; python: 2,045; xml: 461; makefile: 4
file content (44 lines) | stat: -rw-r--r-- 1,004 bytes parent folder | download | duplicates (2)
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
"""Run tests over code examples in the documentation."""

import contextlib
import os
import runpy
import sys

import pytest

if sys.version_info >= (3, 4):
    from pathlib import Path
else:  # python2.7 compat
    from _pytest.pathlib import Path


examples_dir = Path(__file__, '../../doc/source/examples').resolve()
examples = sorted(examples_dir.glob('*.py'))


@contextlib.contextmanager
def cd(where_to):
    """
    Temporarily change the working directory.

    Restore the current working dir after exiting the context.
    """
    curr = Path.cwd()
    try:
        os.chdir(str(where_to))
        yield
    finally:
        os.chdir(str(curr))


@pytest.mark.parametrize('example', examples, ids=lambda p: p.name)
def test_doc_example(example):
    """
    Verify example scripts included in the docs are up to date.

    Execute each script in :file:`docs/source/examples`,
    not raising any errors is good enough.
    """
    with cd(example.parent):
        runpy.run_path(str(example))