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
--------------------------------------------------------------
.. _`pytest-yamlwsgi`: http://bitbucket.org/aafshar/pytest-yamlwsgi/src/tip/pytest_yamlwsgi.py
2010-10-27 21:14:12 +08:00
.. _`PyYAML`: http://pypi.python.org/pypi/PyYAML/
2010-10-27 20:52:28 +08:00
Here is an example ``conftest.py`` (extracted from Ali Afshnars special purpose `pytest-yamlwsgi`_ plugin). This ``conftest.py`` will collect ``test*.yml`` files and will execute the yaml-formatted content as custom tests:
.. include:: nonpython/conftest.py
:literal:
You can create a simple example file:
.. include:: nonpython/test_simple.yml
:literal:
2010-10-27 21:14:12 +08:00
and if you installed `PyYAML`_ or a compatible YAML-parser you can
now execute the test specification::
2010-10-27 20:52:28 +08:00
2010-11-01 02:51:16 +08:00
nonpython $ py.test test_simple.yml
2010-10-27 20:52:28 +08:00
=========================== test session starts ============================
2014-01-29 20:47:11 +08:00
platform linux2 -- Python 2.7.3 -- py-1.4.20 -- pytest-2.5.2
2012-10-18 18:24:50 +08:00
collected 2 items
2010-11-07 17:19:58 +08:00
2014-01-29 20:47:11 +08:00
test_simple.yml F.
2012-10-18 18:24:50 +08:00
================================= FAILURES =================================
______________________________ usecase: hello ______________________________
usecase execution failed
spec failed: 'some': 'other'
no further details known at this point.
2013-10-04 01:09:18 +08:00
==================== 1 failed, 1 passed in 0.03 seconds ====================
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
consulted when reporting in ``verbose`` mode::
2010-10-27 20:52:28 +08:00
nonpython $ py.test -v
=========================== test session starts ============================
2014-01-29 20:47:11 +08:00
platform linux2 -- Python 2.7.3 -- py-1.4.20 -- pytest-2.5.2 -- /home/hpk/p/pytest/.tox/regen/bin/python
2012-10-18 18:24:50 +08:00
collecting ... collected 2 items
test_simple.yml:1: usecase: hello FAILED
2014-01-29 20:47:11 +08:00
test_simple.yml:1: usecase: ok PASSED
2010-11-07 17:19:58 +08:00
2012-10-18 18:24:50 +08:00
================================= FAILURES =================================
______________________________ usecase: hello ______________________________
usecase execution failed
spec failed: 'some': 'other'
no further details known at this point.
2013-10-04 01:09:18 +08:00
==================== 1 failed, 1 passed in 0.03 seconds ====================
2010-10-27 20:52:28 +08:00
While developing your custom test collection and execution it's also
interesting to just look at the collection tree::
2013-08-01 23:32:19 +08:00
nonpython $ py.test --collect-only
2011-03-09 17:58:36 +08:00
=========================== test session starts ============================
2014-01-29 20:47:11 +08:00
platform linux2 -- Python 2.7.3 -- py-1.4.20 -- pytest-2.5.2
2012-10-18 18:24:50 +08:00
collected 2 items
<YamlFile 'test_simple.yml'>
<YamlItem 'hello'>
2014-01-29 20:47:11 +08:00
<YamlItem 'ok'>
2011-03-09 17:58:36 +08:00
2013-12-17 15:30:35 +08:00
============================= in 0.02 seconds =============================