2011/12/23

Dynamic binding for data-driven tests in MbUnit v3

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:

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:
Look at how the different properties of the test parameter were bound and printed in the test log.
That's just awesome! Good work Aleks!

2 comments:

Funker said...

I repeated this example code but data is not loaded.

An error occurred "Test skipped because it is parameterized but no data was provided."

Where can be my mistake?

Yann Trevin said...

Probably Gallio cannot find the specified file. Check in the documentation as linked in the article. You need to make the file available either as an embedded resource, as an inline content, or as a simple file in a location relative to the current working directory.