Install & Compatibility
Where this runs
tested against v1.7.2 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.030s · 17.8MB
glibcpy 3.10–3.95 runs
installs and imports cleanly · install 1.5s · import 0.028s · 18MB
16MB installed
● package 16MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
ddt
✓ from ddt import ddt
✗ import ddt
The 'ddt' class decorator must be imported directly from the module, not the module itself.
data
✓ from ddt import data
unpack
✓ from ddt import unpack
file_data
✓ from ddt import file_data
named_data
✓ from ddt import named_data
This quickstart demonstrates basic usage of `ddt` with `unittest`. It shows how to use the `@ddt` class decorator and the `@data` and `@unpack` method decorators to run the same test logic with different inputs. The commented-out section shows usage of `@named_data` for more descriptive test names.
import unittest
from ddt import ddt, data, unpack
@ddt
class MyTests(unittest.TestCase):
@data(1, 2, 3)
def test_single_value(self, value):
self.assertGreater(value, 0)
@data((1, 2), (3, 4))
@unpack
def test_multiple_values(self, a, b):
self.assertLess(a, b)
# Example with named_data (requires ddt >= 1.5.0)
# from ddt import named_data
# @named_data(
# {'name': 'test_case_one', 'x': 5, 'y': 10},
# {'name': 'test_case_two', 'x': 10, 'y': 5}
# )
# def test_with_named_data(self, x, y):
# self.assertGreater(x, y)
if __name__ == '__main__':
unittest.main()
Debug
Known issues
breakingddt dropped support for Python 2.7 in version 1.7.0. Projects using ddt with Python 2.7 must remain on an older ddt version or migrate to Python 3.fixUpgrade to Python 3 or pin ddt to `<1.7.0`.
affects: >=1.7.0
breakingddt dropped support for Python 3.5 in version 1.5.0.fixUpgrade to Python 3.6+ or pin ddt to `<1.5.0`.
affects: >=1.5.0
breakingThe `nose` dependency was completely removed in version 1.4.1. This means `ddt` no longer has specific integrations or requirements for `nose`, favoring standard `unittest` or `pytest` runners.fixEnsure your test runner (e.g., `unittest.main()` or `pytest`) is configured correctly, and remove any `nose`-specific configurations related to `ddt`.
affects: >=1.4.1
gotchaWhen using `@data` or `@file_data` with complex data types (like dictionaries or custom objects), the generated test names can be unpredictable if Python hash randomization is enabled (default in Python 3.3+). This can affect test reporting and re-running failed tests.fixSet the `PYTHONHASHSEED` environment variable to a fixed value (e.g., `export PYTHONHASHSEED=1`) before running tests, or use the `@named_data` decorator (introduced in 1.5.0) for explicit test naming.
affects: all (Python 3.3+)
gotchaNaming your test file `ddt.py` will cause an `ImportError` because Python will try to import your local file instead of the `ddt` library.fixRename your test file to something other than `ddt.py`, for example, `test_my_feature.py`.
affects: all
gotchaThe `@unpack` decorator is used to unpack iterable (tuples, lists) or dictionary arguments into multiple positional or keyword arguments for the test method, respectively. Misunderstanding its function can lead to `TypeError` or unexpected argument passing.fixOnly use `@unpack` when your `@data` values are tuples/lists that correspond to multiple test method arguments, or dictionaries for keyword arguments. If your test method expects a single argument, do not use `@unpack`.
affects: all
Errors
Common errors & fixes
ImportError: cannot import name 'ddt'
This error typically occurs when a local Python file is named `ddt.py`, shadowing the installed `ddt` library, or when attempting to import the `ddt` decorator incorrectly using `import ddt` instead of `from ddt import ddt`.
fixIf you have a file named `ddt.py` in your project or Python path, rename it to something else (e.g., `my_tests.py`). Ensure your import statement is `from ddt import ddt, data, unpack` (or only the decorators you need), and decorate your test class with `@ddt`.
TypeError: 'NoneType' object is not iterable
This error happens when the data source provided to `ddt` decorators like `@data` or `@file_data` resolves to `None` instead of an iterable (e.g., a list, tuple, or dictionary). This can occur if a function fetching the test data returns `None` unexpectedly.
fixEnsure that the data provided to `ddt` decorators is always an iterable. Validate that any function or file providing data returns a list, tuple, or dictionary (even an empty one) and not `None`.
AttributeError: type object 'YourTestCaseClass' has no attribute 'your_test_method_name'
When running individual data-driven tests from the command line, `unittest` expects the full, `ddt`-generated test method name (e.g., `test_method_1_data_value`), not the original base method name defined in your test class. `ddt` dynamically creates these unique test methods during test discovery.
fixTo run a specific data-driven test, use the full, `ddt`-generated test name, which typically includes an index and a string representation of the data. For example: `python -m unittest your_module.YourTestCaseClass.test_method_1_data_value`. Alternatively, run the entire test class to allow `ddt` to discover and execute all generated tests.
Upgrade
Version history
1.7.2latest on PyPI · released Feb 26, 2024
Audit
Dependencies
No dependency data recorded yet.