Remove duplicate code using Data Driven Tests (DDT)

Registered by Dinesh Bhor

Many times, there are a number of data sets that we have to run the same tests on. And, to create a different test for each data set values is time-consuming and inefficient.

Data Driven Testing [1] overcomes this issue. Data-driven testing (DDT) is taking a test, parametrizing it and then running that test with varying data. This allows you to run the same test case with many varying inputs, therefore increasing coverage from a single test, reduces code duplication and can ease up error tracing as well.

DDT is a third party library needs to be installed separately and invoke the module when writing the tests. Several openstack projects [2] are using DDT for test optimization.

DDT generates the test names automatically which can be problematic to trace out for which test data failure occurred. To overcome this limitation we can pass descriptive name using annotate function as shown below:

@ddt.data(annotated('missing_name', {"foo": "test", "availability_zone": "nova1"}),
                      annotated('name_greater_than_255_characters', {"name": "x" * 256, "availability_zone": "nova1"}))
    def test_create_invalid_aggregate_data(self, value):
  pass

it generates following test names:

 test_create_invalid_aggregate_data_2_name_greater_than_255_characters
 test_create_invalid_aggregate_data_1_missing_name

This will help to find out failure against particular test data.

Same is implemented in 'openstack/zaqar' [3].

[1] http://ddt.readthedocs.io/en/latest/index.html
[2] http://codesearch.openstack.org/?q=ddt%3E%3D1.0.1&i=nope&files=&repos=
[3] https://github.com/openstack/zaqar/blob/master/zaqar/tests/functional/wsgi/v1/test_queues.py#L87

Blueprint information

Status:
Complete
Approver:
Matt Riedemann
Priority:
Undefined
Drafter:
Dinesh Bhor
Direction:
Needs approval
Assignee:
Dinesh Bhor
Definition:
Superseded
Series goal:
Proposed for ocata
Implementation:
Slow progress
Milestone target:
None
Started by
Dinesh Bhor
Completed by
Dinesh Bhor

Related branches

Sprints

Whiteboard

(?)

Work Items

This blueprint contains Public information 
Everyone can see this information.

Subscribers

No subscribers.