2010-10-27 20:52:28 +08:00
2010-10-27 23:36:27 +08:00
.. _`non-python tests`:
2010-10-27 20:52:28 +08:00
Working with non-python tests
====================================================
2010-11-06 06:37:25 +08:00
.. _`yaml plugin`:
2011-09-06 17:43:42 +08:00
A basic example for specifying tests in Yaml files
2010-10-27 20:52:28 +08:00
--------------------------------------------------------------
2022-10-05 22:24:55 +08:00
.. _`pytest-yamlwsgi`: https://pypi.org/project/pytest-yamlwsgi/
2010-10-27 20:52:28 +08:00
2020-08-08 06:22:10 +08:00
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:
2010-10-27 20:52:28 +08:00
.. include :: nonpython/conftest.py
:literal:
You can create a simple example file:
2019-07-03 14:49:06 +08:00
.. include :: nonpython/test_simple.yaml
2010-10-27 20:52:28 +08:00
:literal:
2021-10-22 20:47:57 +08:00
and if you installed :pypi: `PyYAML` or a compatible YAML-parser you can
2018-11-24 13:41:22 +08:00
now execute the test specification:
.. code-block :: pytest
2010-10-27 20:52:28 +08:00
2019-07-03 14:49:06 +08:00
nonpython $ pytest test_simple.yaml
2019-01-06 03:19:40 +08:00
=========================== test session starts ============================
2024-01-02 16:58:20 +08:00
platform linux -- Python 3.x.y, pytest-8.x.y, pluggy-1.x.y
2021-10-04 14:56:26 +08:00
rootdir: /home/sweet/project/nonpython
2017-05-13 19:25:52 +08:00
collected 2 items
2018-05-18 16:19:46 +08:00
2019-07-05 08:01:16 +08:00
test_simple.yaml F. [100%]
2018-05-18 16:19:46 +08:00
2019-01-06 03:19:40 +08:00
================================= FAILURES =================================
______________________________ usecase: hello ______________________________
2017-05-13 19:25:52 +08:00
usecase execution failed
spec failed: 'some': 'other'
no further details known at this point.
2020-03-11 22:23:25 +08:00
========================= short test summary info ==========================
FAILED test_simple.yaml::hello
2019-08-30 23:43:47 +08:00
======================= 1 failed, 1 passed in 0.12s ========================
2010-10-27 20:52:28 +08:00
2015-09-22 20:02:11 +08:00
.. regendoc:wipe
2010-10-27 20:52:28 +08:00
You get one dot for the passing `` sub1: sub1 `` check and one failure.
Obviously in the above `` conftest.py `` you'll want to implement a more
2010-11-06 06:37:25 +08:00
interesting interpretation of the yaml-values. You can easily write
your own domain specific testing language this way.
.. note ::
`` repr_failure(excinfo) `` is called for representing test failures.
2010-11-06 18:38:53 +08:00
If you create custom collection nodes you can return an error
2010-11-06 06:37:25 +08:00
representation string of your choice. It
will be reported as a (red) string.
2011-12-05 18:10:48 +08:00
`` reportinfo() `` is used for representing the test location and is also
2018-11-24 13:41:22 +08:00
consulted when reporting in `` verbose `` mode:
.. code-block :: pytest
2010-10-27 20:52:28 +08:00
2016-06-21 22:16:57 +08:00
nonpython $ pytest -v
2019-01-06 03:19:40 +08:00
=========================== test session starts ============================
2024-01-02 16:58:20 +08:00
platform linux -- Python 3.x.y, pytest-8.x.y, pluggy-1.x.y -- $PYTHON_PREFIX/bin/python
2021-10-04 14:56:26 +08:00
cachedir: .pytest_cache
rootdir: /home/sweet/project/nonpython
2017-05-13 19:25:52 +08:00
collecting ... collected 2 items
2018-05-18 16:19:46 +08:00
2019-07-05 08:01:16 +08:00
test_simple.yaml::hello FAILED [ 50%]
test_simple.yaml::ok PASSED [100%]
2018-05-18 16:19:46 +08:00
2019-01-06 03:19:40 +08:00
================================= FAILURES =================================
______________________________ usecase: hello ______________________________
2017-05-13 19:25:52 +08:00
usecase execution failed
spec failed: 'some': 'other'
no further details known at this point.
2020-03-11 22:23:25 +08:00
========================= short test summary info ==========================
FAILED test_simple.yaml::hello
2019-08-30 23:43:47 +08:00
======================= 1 failed, 1 passed in 0.12s ========================
2010-10-27 20:52:28 +08:00
2015-09-22 20:02:11 +08:00
.. regendoc:wipe
2010-10-27 20:52:28 +08:00
While developing your custom test collection and execution it's also
2018-11-24 13:41:22 +08:00
interesting to just look at the collection tree:
.. code-block :: pytest
2010-10-27 20:52:28 +08:00
2016-06-21 22:16:57 +08:00
nonpython $ pytest --collect-only
2019-01-06 03:19:40 +08:00
=========================== test session starts ============================
2024-01-02 16:58:20 +08:00
platform linux -- Python 3.x.y, pytest-8.x.y, pluggy-1.x.y
2021-10-04 14:56:26 +08:00
rootdir: /home/sweet/project/nonpython
2017-05-13 19:25:52 +08:00
collected 2 items
2020-07-09 05:51:01 +08:00
<Package nonpython>
2019-07-03 14:49:06 +08:00
<YamlFile test_simple.yaml>
2019-01-05 23:21:49 +08:00
<YamlItem hello>
<YamlItem ok>
2018-05-18 16:19:46 +08:00
2020-12-13 05:21:28 +08:00
======================== 2 tests collected in 0.12s ========================