Registry / testing / ddt
library1.7.2pypypi✓ verified 28d ago

ddt (Data-Driven Tests) is a Python library that enables data-driven testing by decorating test methods with various data sources. It allows multiplying one `unittest.TestCase` method into multiple test cases, each run with different data, enhancing test efficiency and readability. The current version is 1.7.2, and it maintains an active release cadence with regular updates.

pip install ddt
INSTALL
IMPORT
SIG · DDT
D
ddt
testingpythonv1.7.2
Install
1.5s avg
Import
29ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.9–3.13
musl
3.9–3.13
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
musl
py 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.030s · 17.8MB
glibc
py 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.
fix
Upgrade 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.
fix
Upgrade 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.
fix
Ensure 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.
fix
Set 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.
fix
Rename 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.
fix
Only 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`.
fix
If 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.
fix
Ensure 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.
fix
To 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.

Agent activity
21 hits · last 30 days
node
20
Resources
ddt — pip install ddt · libregistry