Setting Up NUnit

To write tests using NUnit, your project needs to reference the nunit.framework.dll. In Visual Studio, you click on the project and choose Add Reference. Then navigate to the nunit.framework DLL and select it. I keep nunit.framework.dll in the directory that holds all my .NET project directories, to make it easy to find.

The usual way to write tests is to set up a test fixture, which is a class. The convention is to use a separate fixture for every test topic ”for example, we ll have a fixture for our Regex tests. Since each fixture has only one setup and one teardown , all the tests in the class want to be somewhat alike, and usually we wind up with one or a few fixtures for each important class in our system. The basic test fixture class looks like this:

 using System; 
using NUnit.Framework;
using System.Text.RegularExpressions;

namespace Notepad
[TestFixture] public class TestRegex : Assertion{

The [TestFixture] is one of those attributes I was telling you about. It tells NUnit that this class has tests in it.

NUnit tests allow us to write assertions, and it checks those assertions to see if they are true. So if we wanted to test whether 2+2 is 4, we might write

 [Test] public void TwoPlusTwo { 
AssertEquals(4, 2+2);

NUnit has a number of different assertion methods for your convenience. We ll see more of them as we move on in the book. By far, the most commonly used one is AssertEquals().

We don t have to have the test fixture inherit from Assertion, as shown previously, but I prefer that because it means I can say AssertEquals rather than Assertion.AssertEquals. Note also in this example that I m already showing the reference to System.Text.RegularExpressions. I have to do that to write any references to the Regex class. So we re all set to go.

Extreme Programming Adventures in C#
Javaв„ў EE 5 Tutorial, The (3rd Edition)
ISBN: 735619492
EAN: 2147483647
Year: 2006
Pages: 291 © 2008-2017.
If you may any questions please contact us: