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:
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:
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.
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.
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.
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.
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).