MbUnit v3.3 has been now released for a while and I realize that I have completely forgotten to write about the new nifty features added by our fellow contributor Aleksandr Jones. Aleks had added a couple of very powerful attributes for writing data-driven tests. He had even written some useful documentation in the Gallio wiki. Those attributes provide a seamless way to bind automatically your external data sources (CSV or XML data files) to the test parameters.
Basically you can declare your test parameters as "dynamic" variables and let MbUnit to bind automagically the parameters behind the scene. Of course it also means that the feature is only available for test projects targeting the .NET 4 framework.
Let's consider the following simple comma-separated example data file:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Name | Address | City | State | Url | Degree | Martial Art | |
---|---|---|---|---|---|---|---|
John Smith | 123 Test Street | New York | NY | www.yahoo.com | Yes | Karate | |
Patricia Doe | 72 North Avenue | Chicago | IL | www.google.com | Yes | Wing Chun | |
Allen Watts | 444 Unit Boulevard | Oakland | CA | www.apache.org | No | Jiu-Jutsu |
Let's now write a test method which loads the file and gets data from it. We could have used the classic
[CsvData]
attribute to bind explicitly the parameters. But with the new [FlatFileDataObject]
attribute, it's far easier:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Test, FlatFileDataObject("TestData.csv", FileType.CsvFile)] | |
public void Test(dynamic RowOfTestData) | |
{ | |
TestLog.WriteLine(RowOfTestData.ToStringWithNewLines()); | |
TestLog.WriteLine("RowOfTestData.Name ==> " + RowOfTestData.Name); | |
TestLog.WriteLine("RowOfTestData.Address ==> " + RowOfTestData.Address); | |
TestLog.WriteLine("RowOfTestData.City ==> " + RowOfTestData.City); | |
TestLog.WriteLine("RowOfTestData.State ==> " + RowOfTestData.State); | |
TestLog.WriteLine("RowOfTestData.Url ==> " + RowOfTestData.Url); | |
TestLog.WriteLine("RowOfTestData.Degree ==> " + RowOfTestData.Degree); | |
TestLog.WriteLine("RowOfTestData.Martial_Art ==> " + RowOfTestData.Martial_Art); | |
} |
Look at how the different properties of the test parameter were bound and printed in the test log.
