Sunday, August 12, 2007

NUnit > Basics

• NUnit is a unit testing framework for all .net languages
• Current production release is 2.4, it is open source and written in C#
• All test cases are built directly into the code of the project
• Nunit tests are grouped into a number of classes, called test fixtures
• A test fixture class is denoted by the [TestFixture] attribute
• Any class that contains tests must be marked with this attribute, so that Nunit can locate the tests
• All Nunit attributes reside in the Nunit.Framework namespace
• Nunit is built into Visual Studio, but the provided GUI program is also easy to use

[Test] public void Add()
{
double result = fvalue1 + fvalue2;
Assert.AreEqual(6, result, “Expected Result”);
}

• The Assert class contains many of the methods used to construct test cases, like: AreEqual(),AreSame(), IsTrue(), Fail()

• AreSame and AreEqual are different. AreSame checks if same objects are referenced by both objects being compared

[SetUp] and [TearDown] allow to initialize private members and cleanup code

• [ExpectedException] and [Ignore]: If a method is expected to throw exception, such a method is declared with the [ExpectedException], example as under:

[Test]
[ ExpectedException( typeof (InvalidOperationException) ) ]
public void ExceptAnException()
{
throw new InvalidCastException();
}

• Ignore attribute is to cause Nunit to skip a certain test case
• Example as under:

[Test]
[Ignore(“ ignored test ”)]pulic void IgnoredTest()
{
throw new Excpetion(); }}

No comments: