Data-Driven/Decorated Tests

1.7.2 · active · verified Sat Apr 11

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.

Warnings

Install

Imports

Quickstart

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()

view raw JSON →