API

class ddt.TestNameFormat

An enum to configure how mk_test_name() to compose a test name. Given the following example:

@data("a", "b")
def testSomething(self, value):
    ...

if using just @ddt or together with DEFAULT:

  • testSomething_1_a
  • testSomething_2_b

if using INDEX_ONLY:

  • testSomething_1
  • testSomething_2
ddt.add_test(cls, test_name, test_docstring, func, *args, **kwargs)

Add a test case to this class.

The test will be based on an existing function but will give it a new name.

ddt.data(*values)

Method decorator to add to your test methods.

Should be added to methods of instances of unittest.TestCase.

ddt.ddt(arg=None, **kwargs)

Class decorator for subclasses of unittest.TestCase.

Apply this decorator to the test case class, and then decorate test methods with @data.

For each method decorated with @data, this will effectively create as many methods as data items are passed as parameters to @data.

The names of the test methods follow the pattern original_test_name_{ordinal}_{data}. ordinal is the position of the data argument, starting with 1.

For data we use a string representation of the data value converted into a valid python identifier. If data.__name__ exists, we use that instead.

For each method decorated with @file_data('test_data.json'), the decorator will try to load the test_data.json file located relative to the python file containing the method that is decorated. It will, for each test_name key create as many methods in the list of values from the data key.

Decorating with the keyword argument testNameFormat can control the format of the generated test names. For example:

  • @ddt(testNameFormat=TestNameFormat.DEFAULT) will be index and values.
  • @ddt(testNameFormat=TestNameFormat.INDEX_ONLY) will be index only.
  • @ddt is the same as DEFAULT.
ddt.feed_data(func, new_name, test_data_docstring, *args, **kwargs)

This internal method decorator feeds the test data item to the test.

ddt.file_data(value, yaml_loader=None)

Method decorator to add to your test methods.

Should be added to methods of instances of unittest.TestCase.

value should be a path relative to the directory of the file containing the decorated unittest.TestCase. The file should contain JSON encoded data, that can either be a list or a dict.

In case of a list, each value in the list will correspond to one test case, and the value will be concatenated to the test method name.

In case of a dict, keys will be used as suffixes to the name of the test case, and values will be fed as test data.

yaml_loader can be used to customize yaml deserialization. The default is None, which results in using the yaml.safe_load method.

ddt.idata(iterable, index_len=None)

Method decorator to add to your test methods.

Should be added to methods of instances of unittest.TestCase.

Parameters:
  • iterable – iterable of the values to provide to the test function.
  • index_len – an optional integer specifying the width to zero-pad the test identifier indices to. If not provided, this will add the fewest zeros necessary to make all identifiers the same length.
ddt.mk_test_name(name, value, index=0, index_len=5, name_fmt=<TestNameFormat.DEFAULT: 0>)

Generate a new name for a test case.

It will take the original test name and append an ordinal index and a string representation of the value, and convert the result into a valid python identifier by replacing extraneous characters with _.

We avoid doing str(value) if dealing with non-trivial values. The problem is possible different names with different runs, e.g. different order of dictionary keys (see PYTHONHASHSEED) or dealing with mock objects. Trivial scalar values are passed as is.

A “trivial” value is a plain scalar, or a tuple or list consisting only of trivial values.

The test name format is controlled by enum TestNameFormat as well. See the enum documentation for further details.

ddt.process_file_data(cls, name, func, file_attr)

Process the parameter in the file_data decorator.

ddt.unpack(func)

Method decorator to add unpack feature.

class named_data.NamedDataDict(name, **kwargs)

This is a helper class for @named_data that allows ddt tests to have meaningful names.

class named_data.NamedDataList(name, *args)

This is a helper class for @named_data that allows ddt tests to have meaningful names.

named_data.named_data(*named_values)

This decorator is to allow for meaningful names to be given to tests that would otherwise use @ddt.data and @ddt.unpack.

Example of original ddt usage:

@ddt.ddt class TestExample(TemplateTest):

@ddt.data(
[0, 1], [10, 11]

) @ddt.unpack def test_values(self, value1, value2):

Example of new usage:

@ddt.ddt class TestExample(TemplateTest):

@named_data(
[‘A’, 0, 1], [‘B’, 10, 11],

) def test_values(self, value1, value2):

Note that @unpack is not used.

Parameters:| dict[Any,Any] named_values (list[Any]) – Each named_value should be a list with the name as the first argument, or a dictionary with ‘name’ as one of the keys. The name will be coerced to a string and all other values will be passed unchanged to the test.