RowTests with NUnit 2.4

I had some time last week and therefore I played around with the new extensibility features of NUnit 2.4. I wanted to know, how difficult it is to implement the RowTest feature of MbUnit as NUnit Addin. Two hours later the RowTest Extension for NUnit was ready. I released the extension under the MIT license.

The following code snippet shows you how to use the extension:

[TestFixture]
public class RowTestSample
{
   [RowTest]
   [Row( 1000, 10, 100.0000)]
   [Row(-1000, 10, -100.0000)]
   [Row( 1000, 7, 142.85715)]
   [Row( 1000, 0.00001, 100000000)]
   [Row(4195835, 3145729, 1.3338196)]
   public void DivisionTest(double num, double den, double res)
   {
     Assert.AreEqual(res, num / den, 0.00001);
   }
}

The current release does not support all features of MbUnit RowTests, e.g. Exceptions cannot be tested.

Technorati Tags , , ,

This entry was posted in .NET Development, TDD and tagged , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

26 Comments

  1. Posted September 12, 2007 at 2:32 am | Permalink

    Andreas,

    Nice job on the extension! This proved to be a very useful sample for me. My team is deprecating the use of MbUnit in favor of NUnit and I have an existing UT code base (on the order of 2000 UT cases) that use the RowTest functionality extensively.

    One optimization I have made to the RowTestNameBuilder.cs. I added a null check on _arguments[i] (in case null is passed in as a parameter to a [Row] attribute).

    One odd thing I’ve observed involves UT fixture class fields being accessed from RowTests. I commonly perform one-time only initialization in a method decorated with a [TestFixtureSetup] attribute. I noticed that when RowTests are executed the class fields are always uninitialized even though I se the TestFixtureSetUp method execute prior to the RowTest.

    I theorize that this is due to the fact that the TestSuite does not receive an instance of the test fixture class in the RowTestAddIn.BuildFrom() method but I am not sure how to get around that (perhaps this is a limitation in the NUnit extensibility model due to the fact that only the MethodInfo is available on ITestCaseBuilder extensions).

    I can post my code modifications (no matter how slight) if anyone is interested.

  2. a.schlapsi
    Posted September 13, 2007 at 7:58 am | Permalink

    Thank you for your feedback. If you permit that I integrate your modifications into the extension I’ll be interested.

    I could not reproduce the issue with UT fixture class fields. Which version of NUnit are you using?

  3. Posted September 25, 2007 at 4:07 am | Permalink

    I’m using 2.4.3…I can put together a sample that would demonstrate what I was seeing. I’ll get that together for you tomorrow.

  4. Posted October 6, 2007 at 3:56 am | Permalink

    Here’s the modification I made to RowTestNameBuilder.cs:

    ///
    /// Method to create a comma separated list of [RowTest] arguments.
    ///
    private void CreateArgumentList()
    {
    StringBuilder argumentListBuilder = new StringBuilder();
    argumentListBuilder.Append(”(”);

    for (int i = 0; i 0)
    argumentListBuilder.Append(”, “);

    if (_arguments[i] == null)
    {
    argumentListBuilder.Append(”NULL”);
    }
    else
    {
    argumentListBuilder.Append(_arguments[i].ToString());
    }

    argumentListBuilder.Append(”)”);
    _argumentList = argumentListBuilder.ToString();
    }

    argumentListBuilder = null;
    }

    I’ll be on a trip for about a week. Once I’m back I’ll get the sample with the odd behavior I mentioned in the previous post.

  5. Kenneth
    Posted October 22, 2007 at 3:08 pm | Permalink

    Hi

    I’ve placed the files in the ‘addins’ directory, but RowTest are NOT shown when I chose, the Addins… from the Tools menu. Only the RepeatedTestDecorator is there???

    Kenneth

  6. a.schlapsi
    Posted October 23, 2007 at 8:18 pm | Permalink

    Which version of NUnit are you using?

  7. Kenneth
    Posted October 24, 2007 at 8:36 am | Permalink

    I’m using the latest and newest v2.4.4

    Kenneth

  8. a.schlapsi
    Posted October 27, 2007 at 4:52 pm | Permalink

    I released a new version which was built using NUnit 2.4.4. Please try the new version.

  9. Kenneth
    Posted October 28, 2007 at 10:16 pm | Permalink

    Works now. Thanks

    Kenneth

  10. Wayne
    Posted December 3, 2007 at 9:21 am | Permalink

    I took your add-in recompiled it for Nunit 2.4.5. All unit tests pass, but it does not work. In your DivisionTest() sample, the test is called only once - and the parameters are all zero…

    Any ideas?

  11. a.schlapsi
    Posted December 5, 2007 at 1:03 am | Permalink

    I could not reproduce this behavior. I have just uploaded a version built for NUnit 2.4.5. Do you still have problems if you use this build?

  12. Wayne
    Posted December 12, 2007 at 4:17 pm | Permalink

    Turns out this appears to be a problem if using TestDriven.Net’s release.

  13. Andy
    Posted January 17, 2008 at 9:20 am | Permalink

    How can I write a row test using a datetime?

    I tried the following, but it was unsuccessful:

    System.ArgumentException : Object of type ‘System.String’ cannot be converted to type ‘System.DateTime’.

  14. Posted January 18, 2008 at 6:22 am | Permalink

    We are not able to create categories while using Rowtest Extension.Can you suggest some method to do that please?? We are using verison 2.4 of nunit.

    GAURAV
    HARSHA

  15. Posted January 29, 2008 at 11:33 pm | Permalink

    Nice bits, thanks.

    Has anyone tried this using NUnitASP?

    I’m using the WebFormTestCase : CompatibilityAdapter so my test classes inherit from WebFormTestCase

    Something odd happens with the SetUp/MasterSetUp

    When my test runs, I can debug and see that MasterSetUp gets called, but the test fails with error that happen when the setup doesn’t run.
    If I add a call to MasterSetUp() as the first line of the rowtest, it works as expected but there are lots of extra calls to setup :S

    Of note is that if I debug and I have two Rows to test, I can skip over the call to MasterSetUp in the second row

  16. Richard
    Posted February 5, 2008 at 3:48 pm | Permalink

    Great stuff. Really makes my life easier, thanks for creating it. I will look into the source code to learn how to make this kind of stuff myself (NUnit documentation doesn’t shine in clarity).

  17. Richard
    Posted February 5, 2008 at 4:09 pm | Permalink

    Andreas, I’m missing a small bit of functionality: the Category-attribute is not honored. I use this to filter out tests that have database-access (you do not always want to mock your db…) and are long-running.
    It does not help to add a [Test, nycategory] attribute or a [Category] attribute to my test.

    Any plans for supporting this in the [RowTest] attribute?

  18. a.schlapsi
    Posted February 5, 2008 at 8:54 pm | Permalink

    Hi Richard,

    support for the Category attribute is planned for the next release.

  19. Satish
    Posted March 11, 2008 at 6:40 pm | Permalink

    Does this work with nunit version 2.4.6? Im trying to use it and it does not show up in the gui runner addons also it TestDriven.NET doesn’t run the RowTests as well.

  20. Andreas
    Posted March 12, 2008 at 12:30 am | Permalink

    It should work with NUnit 2.4.6. Did you compile the extension yourself or did you use the pre-built binary?

    Note, that TestDriven.NET has supported NUnit addins since version 2.11 only.

  21. Neil Burnett
    Posted March 12, 2008 at 12:20 pm | Permalink

    Can Resharper support this RowTest extension?

  22. Yichuan
    Posted March 20, 2008 at 11:27 am | Permalink

    RowTest does not load. I have tried with NUnit 2.4.5 and 2.4.6. Neither work. Any advice?

  23. Mike Nacey
    Posted March 26, 2008 at 3:30 pm | Permalink

    Confirmed that this does not work in NUnit 2.4.6 with compiled binary. If I compile it myself and add the attribute:

    Type=ExtensionType.Client

    it will at least show up in the GUI, but it will not recognize or run any tests that start with [RowTest]. Without the above attribute set, NUnit fails to cast the extension as IAddin for some reason. If a add the additional attribute of [Test] for the test, then NUnit just ignores it since it has input parameters I guess.

  24. Mike Nacey
    Posted March 26, 2008 at 3:41 pm | Permalink

    Also tested with Test Driven.NET 2.12. No inputs are given values. I.E.

    [RowTest]
    [Row(1,2)]
    public void test (int one, int two)
    {
    Assert.AreEqual (1, one);
    }

    one will always be 0

  25. andreas
    Posted March 27, 2008 at 8:49 am | Permalink

    Which .NET framework version do you use?

  26. andreas
    Posted April 4, 2008 at 12:48 am | Permalink

    Have you already tried the version of the RowTest extension which is included in NUnit 2.4.7?

3 Trackbacks

  1. [...] (historically NUnit has only supported parameterless test methods). However Andreas Schlapsi has recently written an implementation of MbUnit’s RowTest using NUnit 2.4’s Addin extensibility [...]

  2. [...] (historically NUnit has only supported parameterless test methods). However Andreas Schlapsi has recently written an implementation of MbUnit’s RowTest using NUnit 2.4’s Addin extensibility [...]

  3. [...] (historically NUnit has only supported parameterless test methods). However Andreas Schlapsi has recently written an implementation of MbUnit’s RowTest using NUnit 2.4’s Addin extensibility [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*