| 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-8.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 |
| 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: |
| 62 |
|
| 63 |
.. code-block:: pytest |
| 64 |
|
| 65 |
nonpython $ pytest -v |
| 66 |
=========================== test session starts ============================ |
| 67 |
platform linux -- Python 3.x.y, pytest-8.x.y, pluggy-1.x.y -- $PYTHON_PREFIX/bin/python |
| 68 |
cachedir: .pytest_cache |
| 69 |
rootdir: /home/sweet/project/nonpython |
| 70 |
collecting ... collected 2 items |
| 71 |
|
| 72 |
test_simple.yaml::hello FAILED [ 50%] |
| 73 |
test_simple.yaml::ok PASSED [100%] |
| 74 |
|
| 75 |
================================= FAILURES ================================= |
| 76 |
______________________________ usecase: hello ______________________________ |
| 77 |
usecase execution failed |
| 78 |
spec failed: 'some': 'other' |
| 79 |
no further details known at this point. |
| 80 |
========================= short test summary info ========================== |
| 81 |
FAILED test_simple.yaml::hello |
| 82 |
======================= 1 failed, 1 passed in 0.12s ======================== |
| 83 |
|
| 84 |
.. regendoc:wipe |
| 85 |
|
| 86 |
While developing your custom test collection and execution it's also |
| 87 |
interesting to just look at the collection tree: |
| 88 |
|
| 89 |
.. code-block:: pytest |
| 90 |
|
| 91 |
nonpython $ pytest --collect-only |
| 92 |
=========================== test session starts ============================ |
| 93 |
platform linux -- Python 3.x.y, pytest-8.x.y, pluggy-1.x.y |
| 94 |
rootdir: /home/sweet/project/nonpython |
| 95 |
collected 2 items |
| 96 |
|
| 97 |
<Package nonpython> |
| 98 |
<YamlFile test_simple.yaml> |
| 99 |
<YamlItem hello> |
| 100 |
<YamlItem ok> |
| 101 |
|
| 102 |
======================== 2 tests collected in 0.12s ======================== |