Data-Driven Tests

This example is based on Examples/Source/Data-Driven Tests/DataTests.js from examples.zip.

Often, it can be useful to run a single test with different sets of expectations. To accomplish this, xUnit.js provides the concept of Fact.Data. In the console runner, this is exposed through the Data(values) attribute.

Test:

Function.RegisterNamespace("Test"); [Fixture] Test.DataTests=function(){ [Fact, Data(1,2,3,4)] function VerifiesDataIsInRange(data){ var expectedLow = 1; var expectedHigh = 3; var actual=data; Assert.InRange(actual,expectedLow,expectedHigh); } }

In the test above, we are supplying four separate values, the numbers 1 to 4, as the data we are expecting in our test. This will cause the fact to be run four times, once for each value. This value is supplied as an argument for the parameter "data", which is then collected as our actual, for the purpose of comparing to our low and high expectations.

Looking at the expectations, then, we can tell that this test will pass three times, for the values 1 to 3, and fail once, for the value 4, which falls outside of our intended range of values.

Any javascript value can be supplied as a piece of data, from primitives such as Booleans, Numbers and Strings, to complex Objects, Arrays, and even Functions.