|
HTML clipboard
using NUnit.Framework;
at the top. Then we declare our class to be descendant of TestCase which is
an abstract class.
Then we override the base constructor with a single string parameter:
public MyTest(String name) :
base(name) {}
And we write the every test in different method:
public void SimpleAcceptedTest()
{
Assert(true);
}
public void SimpleRejectedTest()
{
Assert(false);
}
Choose meaningful names for the methods because this will be the way you will
be notified which test failed.
We check with the Assert method. If its parameter is true then it passes, if it
is false it fails. So the first will be accepted and the second will fail.
I have put pure 'true' and 'false' just to make the things more clear. You will
never need to do that because this is meaningless. For example some of your
tests may look like
MyClass myObj = new MyClass('pronto');
AssertEquals(myObj.ToString(), 'pronto');
Here I used another method for checking - AssertEquals. It checks if the two
parameters passed to it are equal (obviously :).
After we have written our tests we have to make them able to run. Here is the
code to do that:
public static ITest Suite
{
get
{
TestSuite suite = new TestSuite();
suite.AddTest(new MyTest("SimpleAcceptedTest"));
suite.AddTest(new MyTest("SimpleRejectedTest"));
return suite;
}
}
We create an object from the TestSuite class. And then we add the tests to it
as shown. Warning: The method name passed as parameter to the constructor is not
checked at compile time. If you make a mistake NUnit test runner will tell you
that it can not find the test needed. Now you are ready to run the test. Compile
your project. Find NUnitGUI.exe in your c:\program files\NUnit\bin folder. You
may add it to the Tools menu of Visual Studio.net to access it easier.
After you have started the GUI choose Browse and locate the .dll you just
compiled. If everything is OK then 'Run' button will become enabled. Click it
and a progress meter will start to progress. These are very simple tests so the
result will be immediate. The SimpleRejectedTest will fail.
So after you have written your application and your tests it is a matter of few
clicks to check if you have broken something or everything is working correct.
The code
using System;
using NUnit.Framework;
namespace TestNUnit
{
public class MyTest: TestCase
{
public
MyTest(String name) : base(name) {}
public void
SimpleAcceptedTest()
{
Assert(true);
AssertEquals(true, true);
}
public void
SimpleRejectedTest()
{
Assert(false);
AssertEquals(false, true);
}
public static
ITest Suite
{
get
{
TestSuite suite = new TestSuite();
suite.AddTest(new MyTest("SimpleAceptedTest"));
suite.AddTest(new MyTest("SimpleRejectedTest"));
return suite;
}
}
}
}
|