Unit Testing Client Side with NUnit
there is an effort underway to accurately and pervasively create the main objects needed to even begin unit testing...the customer session and account objects. currently, a weak mock is being used in the [ContactManagementTest] tests, but it is far from exhaustive. the idea of serializing / deserializing has been attempted, but there is some further research needed due to the strong-named assemblies and the inherent security denial.
inheriting your [TestFixture] class from [PromptingTestCase] and calling getCredentials() in the [SetUp] method will enable the credentials input box and thus, all the validation required to make web service calls.
What Is NUnit? NUnit is an application designed to facilitate unit testing. It consists of both a command line and Window's interface, allowing it to be used both interactively and in automated test batches or integrated with the build process. The following sections discuss NUnit as it applies to C# programming.
How Does NUnit Work? NUnit utilizes attributes to designate the different aspects of a unit test class.
TestFixture The TestFixture attribute designates that a class is a test fixture. Classes thus designated contain setup, teardown, and unit tests.
SetUp The SetUp attribute is associated with a specific method inside the test fixture class. It instructs the unit test engine that this method should be called prior to invoking each unit test. A test fixture can only have one SetUp method.
TearDown The TearDown attribute is associated with a specific method inside the test fixture class. It instructs the unit test engine that this method should be called after invoking each unit test. A test fixture can only have one TearDown method.
Test The Test attribute indicates that a method in the test fixture is a unit test. The unit test engine invokes all the methods indicated with this attribute once per test fixture, invoking the set up method prior to the test method and the tear down method after the test method, if they have been defined.
The test method signature must be specific: public void xxx(), where "xxx" is a descriptive name of the test. In other words, a public method taking no parameters and returning no parameters.
Upon return from the method being tested, the unit test typically performs an assertion to ensure that the method worked correctly.
ExpectedException The ExpectedException attribute is an optional attribute that can be added to a unit test method (designated using the Test attribute). As unit testing should in part verify that the method under test throws the appropriate exceptions, this attribute causes the unit test engine to catch the exception and pass the test if the correct exception is thrown.
Methods that instead return an error status need to be tested using the Assertion class provided with NUnit.
Ignore The Ignore attribute is an optional attribute that can be added to a unit test method. This attribute instructs the unit test engine to ignore the associated method. A requires string indicating the reason for ignoring the test must be provided.
Suite The Suite attribute is being deprecated. The original intent was to specify test subsets.
An Example
[TestFixture]
public class ATestFixtureClass
{
private ClassBeingTested cbt;
[SetUp]
public void Initialize()
{
cbt=new ClassBeingTested();
}
[TearDown]
public void Terminate()
{
cbt.Dispose();
}
[Test]
public void DoATest()
{
cbt.LoadImage("fish.jpg");
}
[Test, Ignore("Test to be implemented")]
public void IgnoreThisTest()
{
}
[Test, ExpectedException(typeof(ArithmeticException))]
public void ThrowAnException()
{
throw new ArithmeticException("an exception");
}
}
for more nunit info: http://www.nunit.org/
|