File: example.rst

package info (click to toggle)
python-ddt 1.7.2-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 256 kB
  • sloc: python: 785; makefile: 147; sh: 11
file content (82 lines) | stat: -rw-r--r-- 2,435 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
Example usage
=============

DDT consists of a class decorator ``ddt`` (for your ``TestCase`` subclass)
and two method decorators (for your tests that want to be multiplied):

* ``data``: contains as many arguments as values you want to feed to the test.
* ``file_data``: will load test data from a JSON or YAML file.

.. note::

   Only files ending with ".yml" and ".yaml" are loaded as YAML files. All
   other files are loaded as JSON files.

Normally each value within ``data`` will be passed as a single argument to
your test method. If these values are e.g. tuples, you will have to unpack them
inside your test. Alternatively, you can use an additional decorator,
``unpack``, that will automatically unpack tuples and lists into multiple
arguments, and dictionaries into multiple keyword arguments. See examples
below.

This allows you to write your tests as:

.. literalinclude:: ../test/test_example.py
   :language: python

Where ``test_data_dict_dict.json``:

.. literalinclude:: ../test/data/test_data_dict_dict.json
   :language: javascript

and ``test_data_dict_dict.yaml``:

.. literalinclude:: ../test/data/test_data_dict_dict.yaml
   :language: yaml

and ``test_data_dict.json``:

.. literalinclude:: ../test/data/test_data_dict.json
   :language: javascript

and ``test_data_dict.yaml``:

.. literalinclude:: ../test/data/test_data_dict.yaml
   :language: yaml

and ``test_data_list.json``:

.. literalinclude:: ../test/data/test_data_list.json
   :language: javascript

and ``test_data_list.yaml``:

.. literalinclude:: ../test/data/test_data_list.yaml
   :language: yaml

And then run them with your favourite test runner, e.g. if you use pytest::

    $ pytest test/test_example.py

..
   program-output:: pytest ../test/test_example.py

The number of test cases actually run and reported separately has been
multiplied.


DDT will try to give the new test cases meaningful names by converting the
data values to valid python identifiers.


.. note::

   Python 2.7.3 introduced *hash randomization* which is by default
   enabled on Python 3.3 and later. DDT's default mechanism to
   generate meaningful test names will **not** use the test data value
   as part of the name for complex types if hash randomization is
   enabled.

   You can disable hash randomization by setting the
   ``PYTHONHASHSEED`` environment variable to a fixed value before
   running tests (``export PYTHONHASHSEED=1`` for example).