1.8 KiB
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 file.
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:
../test/test_example.py
Where test_data_dict.json:
../test/test_data_dict.json
and test_data_list.json:
../test/test_data_list.json
And then run them with your favourite test runner, e.g. if you use nose:
$ nosetests -v 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).