-
Notifications
You must be signed in to change notification settings - Fork 2
19. Introduction to Data Driven Testing using @ddt
DDT becomes very powerful and useful if you to run same test for multiple data set

- DDT (Data-Driven Tests) allows you to multiply one test case by running it with different test data, and make it appear as multiple test cases
- Loads the test data from various sources
Consider the following test using without DDT,
import unittest
class TestWithoutDDT(unittest.TestCase):
def test_without_ddt(self):
li=[56, 26, 56, 39, 17]
for x in li:
self.assertGreater(x, 50)
While executing the above test, the test will be failing for 26 because 26 is less than 50 and test will be stopped. And for other input you cannot know whether it is tested or passed or failed
Now consider the following test with DDT,
import unittest
from ddt import ddt, data
@ddt
class TestDDTData(unittest.TestCase):
@data(56, 26, 56, 39, 17)
def test_withddtdata(self, x):
self.assertGreater(x, 50)
While executing the above test, you can see different instance of test has been running for each input and 3 test will be failed for the inputs 26, 39 and 17. See below test result screenshot for understanding,
- Decorate your test class with @ddt decorator
- Decorate your test method with @data decorator. @data decorator can have many arguments as values to feed your test
- In case of multiple set inputs @unpack decorator is used for example, if you want to pass username and password for every test then @unpack will be helpful. See the following example,
import unittest
from ddt import ddt, data, unpack
@ddt
class TestDDTUnpack(unittest.TestCase):
@data(('uname1', 'pword1'), ('uname2', 'pword2'))
@unpack
def testddtunpack(self, uname, pword):
print("User Name: ",uname)
print("Password: ",pword)
Above we discussed @ddt, @data, and @unpack decorators. Apart from these decorators, the following are the other decorators which help us in test creation,
-
@idata- this decorator helps to generates a new data sample from a generator function you defined somewhere in the code. Following are the steps to create a test with @idata decorator:
- Create a generator function out side of the test case class
- Create test method in unit test case class and decorate with @idata. See the following example,
import unittest
from ddt import ddt, idata
def data_generator():
for x in [56, 26, 56, 39, 17]:
yield x
@ddt
class TestDDTFuncGenerator(unittest.TestCase):
@idata(data_generator())
def testddtfuncgenerator(self, x):
self.assertGreater(x, 50)
- @file_data- this decorator helps to loads the test data from a JSON or YAML file
JSON file: JSON, or JavaScript Object Notation, is a minimal, readable format for structuring data. It is used primarily to transmit data, and it is an alternative to XML
Consider the following example for JSON file,
datafile.json
[56, 26, 56, 39, 17]
import unittest
from ddt import ddt, file_data
@ddt
class TestDDTDataFile(unittest.TestCase):
@file_data('datafile.json')
def test_with_ddt_file_data(self, x):
self.assertGreater(x, 50)
YAML file: YAML is a data serialization language that matches user’s expectations about data. It designed to be human friendly and works perfectly with other programming languages. It is useful to manage data and includes Unicode printable characters
Refer same example explained for JSON. Where instead of JSON file, provide YAML file in @file_data decorator.