Unit Testing is not difficult

Josh Holmes posts another article about his experiences with unit testing -- this time concentrating on the rather painless process of developing your unit tests with nUnit.

One thing I wanted to point out from his article. nUnit is based on jUnit. The inital port to nUnit had the same basic functionality as jUnit. Later, attributes were added to nUnit to make the process smoother and more "in tune" with what the .NET Framework has to offer. However, there's still some legacy code in nUnit that behaves like jUnit. Since Java doesn't have attributes, test cases were required to be public void methods that started with "Test". Even today, nUnit still exhibits this behavior and will treat any public void method with zero arguments that starts with "Test" as a test case. So while Josh's example showed a method called "TestGetIntegerFromConfigFile" decorated with the "Test" attribute, the code will run the same way from the nUnit GUI even without the attribute.

Here's a quick example showing this behavior:

using System;
using NUnit.Framework;
namespace NUnitCheck { public class Foo { public string Method1() { return "hello"; } } [TestFixture()] public class MyTest { public void TestMethod1() { Foo f = new Foo();
Assert.AreEqual("hello", f.Method1()); } } }

Interestingly, one of my favorite add-ins, TestDriven.NET, does not support this and uses attributes exclusively to find the test cases.

No Comments