2015-04-20 20:10:04 +01:00
|
|
|
Test Example
|
|
|
|
============
|
|
|
|
|
|
|
|
What follows is a commented example of some tests in a single
|
2015-04-20 20:21:52 +01:00
|
|
|
file demonstrating many of the :doc:`format` features. See
|
2015-06-01 16:12:05 +01:00
|
|
|
`Loader`_, below, for the Python needed to integrate with a testing
|
2015-04-20 20:21:52 +01:00
|
|
|
harness.
|
2015-04-20 20:10:04 +01:00
|
|
|
|
|
|
|
.. literalinclude:: example.yaml
|
|
|
|
:language: yaml
|
|
|
|
|
2015-07-14 10:14:05 +01:00
|
|
|
.. _test_loaders:
|
|
|
|
|
2015-04-20 20:10:04 +01:00
|
|
|
Loader
|
|
|
|
------
|
|
|
|
|
2016-04-06 14:20:30 +01:00
|
|
|
To run the tests they must be generated in some fashion and then
|
|
|
|
run. This is accomplished by a test loader. Initially gabbi only
|
|
|
|
supported those test harnesses that supported the ``load_tests``
|
|
|
|
protocol in UnitTest. It now possible to also build and run tests
|
|
|
|
with pytest_ with some limitations described below.
|
2015-06-04 16:45:02 -07:00
|
|
|
|
2016-04-06 14:20:30 +01:00
|
|
|
UnitTest Style Loader
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
To run the tests with a ``load_tests`` style loader a test file containing
|
|
|
|
a ``load_tests`` method is required. That will look a bit like:
|
2015-04-20 20:10:04 +01:00
|
|
|
|
|
|
|
.. literalinclude:: example.py
|
|
|
|
:language: python
|
2015-06-04 16:45:02 -07:00
|
|
|
|
2015-07-07 09:39:02 +01:00
|
|
|
For details on the arguments available when building tests see
|
2015-10-01 15:18:05 +02:00
|
|
|
:meth:`~gabbi.driver.build_tests`.
|
2015-07-07 09:39:02 +01:00
|
|
|
|
2015-06-05 11:08:29 +01:00
|
|
|
Once the test loader has been created, it needs to be run. There are *many*
|
|
|
|
options. Which is appropriate depends very much on your environment. Here are
|
|
|
|
some examples using ``unittest`` or ``testtools`` that require minimal
|
|
|
|
knowledge to get started.
|
2015-06-04 16:45:02 -07:00
|
|
|
|
2015-06-05 11:08:29 +01:00
|
|
|
By file::
|
2015-06-04 16:45:02 -07:00
|
|
|
|
2015-06-05 11:08:29 +01:00
|
|
|
python -m testtools.run -v test/test_loader.py
|
2015-06-04 16:45:02 -07:00
|
|
|
|
2015-06-05 11:08:29 +01:00
|
|
|
By module::
|
|
|
|
|
|
|
|
python -m testttols.run -v test.test_loader
|
|
|
|
|
|
|
|
python -m unittest -v test.test_loader
|
|
|
|
|
|
|
|
Using test discovery to locate all tests in a directory tree::
|
|
|
|
|
|
|
|
python -m testtools.run discover
|
|
|
|
|
|
|
|
python -m unittest discover test
|
|
|
|
|
|
|
|
See the `source distribution`_ and `the tutorial repo`_ for more
|
2015-07-07 09:39:02 +01:00
|
|
|
advanced options, including using ``testrepository`` and
|
|
|
|
``subunit``.
|
2015-06-05 11:08:29 +01:00
|
|
|
|
2016-04-06 14:20:30 +01:00
|
|
|
pytest
|
|
|
|
~~~~~~
|
|
|
|
|
|
|
|
Since pytest does not support the ``load_tests`` system, a different
|
|
|
|
way of generating tests is required. A test file must be created
|
|
|
|
that calls :meth:`~gabbi.driver.py_test_generator` and yields the
|
|
|
|
generated tests. That will look a bit like this:
|
|
|
|
|
|
|
|
.. literalinclude:: pytest-example.py
|
|
|
|
:language: python
|
|
|
|
|
|
|
|
This can then be run with the usual pytest commands. For example::
|
|
|
|
|
|
|
|
py.test -svx pytest-example.py
|
|
|
|
|
|
|
|
.. warning:: When using the unittest runners, it is possible to select
|
|
|
|
just one test from a YAML file and cause it and all its
|
|
|
|
prior tests to run. This **does not** work when using
|
|
|
|
pytest. It will traverse the prior tests and then exit
|
|
|
|
after the first one has run. Fixing this is desirable.
|
|
|
|
|
2015-06-05 11:08:29 +01:00
|
|
|
.. _source distribution: https://github.com/cdent/gabbi
|
|
|
|
.. _the tutorial repo: https://github.com/cdent/gabbi-demo
|
2016-04-06 14:20:30 +01:00
|
|
|
.. _pytest: http://pytest.org/
|