This last Sunday NUnit 2.4.7 was released. As usual, great bug fixes were implemented by the team for this release, but I think the key thing to note about this release is that this is the first release of NUnit to include an externally developed Add-In [Charlie Poole, altdotnet mailing list – 20080331].
The RowTest Add-In
NUnit now includes the RowTest extension, written by Andreas Schlapsi, in it’s extension assemblies. This extension allows you to write test methods that take arguments and to provide multiple sets of argument values using the RowAttribute. To use RowTest, your test must reference the nunit.framework.extensions assembly. [NUnit 2.4.7 Release Notes]
This type of functionality is not something new to the community by any means. MBUnit developed this technology AGES ago- and honestly I think this one feature is the sole reason MANY Developers use MBUnit.
Ok, So what is the RowTest?
The RowTest Attribute allows you to specify different sets of data as declarative statements at the top of your unit test.
Example: Lets say we had to write 3 tests for 3 different sets of data. Here’s a matrix of what we might need to test:
Specification | Parameter 1 | Parameter 2 | Expected Result |
Test Should Fail When Values are Null | null | null | InvalidOperationException |
Test Should Pass with two values of 5 | 5 | 5 | 7.07…<remainder omitted> |
Test Should Pass With two ZEROS | 0 | 0 | 0 |
In a previous release of NUnit we would have to write three unit tests for this because we can only assert one value at a time. Over time this causes duplication of code and you’ll find that you have tons of test that nearly do the same thing, only with different values.
Enter The RowTestAttribute
With the RowTestAttribute (located in the NUnit.Framework.Extensions.dll) you can test rows of data with only one unit test. NUnit will run the unit test each time for each specified row of data.
Example (actual code files are available at the bottom of this post):
Which Tests this code:
Breaking It Down…
The Unit Tests in the first screen shot show two unit tests.
- The first one utilizes the RowTestAttribute
- The second one is how we normally test using NUnit.
Also note how we’re using the new(er) Constraint Model for Unit testing. The unit test in the first set allow me to test via a "per row" basis. Utilizing the "RowTestAttribute" I can assign a set of data to the row and run that rows data via the values passed in. I can also expect exceptions, provide descriptions and messages – just like you would normally do in a classic NUnit test.
The second test is a basic example of what a normal Unit Test would look like. If we DID NOT use the RowTestAttribute we would have to write four unit tests to obtain the same functionality.
Setting It Up
The only thing you’ll need to do to utilize the RowTestAttribute is to reference the nunit.framework.extensions.dll as well as the nunit.framework.dll. See below:
My Only Complaint
The only real complaint that I that I have right now is that ReSharper does pick up the "RowTest" attribute for unit tests, therefore it does not run the unit test. See the screen shot below:
This isn’t a NUnit thing, its a ReSharper thing. If anyone knows how to get ReSharper to "see" RowTest as a valid Test, please let me know. Because the only way I can get this to work is to run it through the NUnit GUI or command line. (No I don’t have TestDriven.NET installed either – but I’m assuming that TDD.NET would see this as a "valid" test).
Download Files (you will need the NUnit 2.4.7 Binaries to get this to run)
FooTheory.NUnit247.Example.zip
Note: This is a VS2008 project. If you’re running VS2005, just copy the .cs files as they are still valid 2005 format files and put them into a new VS2005 project and you’ll be good to go. 🙂
Coach Factory Online says
Business coaching is an informal, open affair. You will meet with the coach and he will assess your business’s needs, and then tailor his services to your precise requirements.
Coach Factory Outlet says
http://buycoachfactoryoutletsz.com