Data driven testing with MXUnit dataproviders

Data driven testing allows you to execute tests with a wide variety of input data. This can make creating and executing certain kinds of tests  efficient and very powerful. Essentially, you provide a reference to a collection of data and MXUnit will iterate over that data and execute the test for each item in the collection.

Here it is in action:

Basic Array Dataprovider Example

To use data to drive tests, tell MXUnit about it using the mxunit:dataprovider annotation. This accepts a string name of some data that exists in the variables scope of the test. The mxunit:dataprovider annotation accepts the following types of data: array, query, list, and a CSV or Excel file. What's unique about these types of tests is that they accept parameters. The parameters represent the data you are using in your tests and may vary depending upon the type of the dataprovider. Note that this is invalid in ColdFusion: mxunit:dataprovider="#my_variable#" - this value must be a string.
Consider the following examples:

Query Dataprovider Example

In this example we specify the name of a query that's in the variables scope of the test. We use this query name for the mxunit:dataprovider value. This test will be run once for each row in the query, and the value of "arguments.q" in this case will be the current row of the query.

Basic List Dataprovider Example

Iterating over a list is essentially the same as an array. The data here is provided as a list of values. As in all mxunit:dataprovider types, pass in the name of the data to the dataprovider.

Basic Excel/CSV Dataprovider

For the basic default excel behavior where you have an Excel file with named row headers, pass the name of the file reference to the mxunit:dataprovider. Then, treat the data as a query object, using the row headings as column names.

Basic Iterator Dataprovider

Sometimes it's useful to just iterate a number of times over some computation. You can do this with MXUnit simply by specify a positive integer to the mxunit:dataprovider.

Script Example

To use dataproviders in CFScript, use @mxunit:dataprovider NameOfDataProvider or @dataprovider NameOfDataProvider.

@mxunit:dataprovider works with CF 9.0.1 and above. If you're on 9.0, stick with @dataprovider

To Do: Advanced Dataproviders (2D Arrays and Excel and CSVs with or without header rows).

Labels

dataproviders dataproviders Delete
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.
  1. Aug 16, 2011

    Vikas Patel says:

    Can we have structure array as dataprovider?

    Can we have structure array as dataprovider?