Debian Codegraph @

doc/en/example/nonpython.rst

raw
1
2 .. _`non-python tests`:
3
4 Working with non-python tests
5 ====================================================
6
7 .. _`yaml plugin`:
8
9 A basic example for specifying tests in Yaml files
10 --------------------------------------------------------------
11
12 .. _`pytest-yamlwsgi`: https://pypi.org/project/pytest-yamlwsgi/
13
14 Here is an example ``conftest.py`` (extracted from Ali Afshar's special purpose `pytest-yamlwsgi`_ plugin). This ``conftest.py`` will collect ``test*.yaml`` files and will execute the yaml-formatted content as custom tests:
15
16 .. include:: nonpython/conftest.py
17 :literal:
18
19 You can create a simple example file:
20
21 .. include:: nonpython/test_simple.yaml
22 :literal:
23
24 and if you installed :pypi:`PyYAML` or a compatible YAML-parser you can
25 now execute the test specification:
26
27 .. code-block:: pytest
28
29 nonpython $ pytest test_simple.yaml
30 =========================== test session starts ============================
31 platform linux -- Python 3.x.y, pytest-9.x.y, pluggy-1.x.y
32 rootdir: /home/sweet/project/nonpython
33 collected 2 items
34
35 test_simple.yaml F. [100%]
36
37 ================================= FAILURES =================================
38 ______________________________ usecase: hello ______________________________
39 usecase execution failed
40 spec failed: 'some': 'other'
41 no further details known at this point.
42 ========================= short test summary info ==========================
43 FAILED test_simple.yaml::hello - usecase execution failed
44 ======================= 1 failed, 1 passed in 0.12s ========================
45
46 .. regendoc:wipe
47
48 You get one dot for the passing ``sub1: sub1`` check and one failure.
49 Obviously in the above ``conftest.py`` you'll want to implement a more
50 interesting interpretation of the yaml-values. You can easily write
51 your own domain-specific testing language this way.
52
53 .. note::
54
55 ``repr_failure(excinfo)`` is called for representing test failures.
56 If you create custom collection nodes you can return an error
57 representation string of your choice. It
58 will be reported as a (red) string.
59
60 ``reportinfo()`` is used for representing the test location and is also
61 consulted when reporting in ``verbose`` mode. It should return a tuple
62 ``(path, lineno, description)``, where:
63
64 * ``path`` is the path shown in reports (usually ``self.path`` or ``self.fspath``).
65 * ``lineno`` is a zero-based line number, or ``0`` when no specific line applies.
66 * ``description`` is a short label shown for the collected item:
67
68 .. code-block:: pytest
69
70 nonpython $ pytest -v
71 =========================== test session starts ============================
72 platform linux -- Python 3.x.y, pytest-9.x.y, pluggy-1.x.y -- $PYTHON_PREFIX/bin/python
73 cachedir: .pytest_cache
74 rootdir: /home/sweet/project/nonpython
75 collecting ... collected 2 items
76
77 test_simple.yaml::hello FAILED [ 50%]
78 test_simple.yaml::ok PASSED [100%]
79
80 ================================= FAILURES =================================
81 ______________________________ usecase: hello ______________________________
82 usecase execution failed
83 spec failed: 'some': 'other'
84 no further details known at this point.
85 ========================= short test summary info ==========================
86 FAILED test_simple.yaml::hello - usecase execution failed
87 ======================= 1 failed, 1 passed in 0.12s ========================
88
89 .. regendoc:wipe
90
91 While developing your custom test collection and execution it's also
92 interesting to look at the collection tree:
93
94 .. code-block:: pytest
95
96 nonpython $ pytest --collect-only
97 =========================== test session starts ============================
98 platform linux -- Python 3.x.y, pytest-9.x.y, pluggy-1.x.y
99 rootdir: /home/sweet/project/nonpython
100 collected 2 items
101
102 <Package nonpython>
103 <YamlFile test_simple.yaml>
104 <YamlItem hello>
105 <YamlItem ok>
106
107 ======================== 2 tests collected in 0.12s ========================