NUnit Frequently Asked Questions


  • How do I implement a test case for a thrown exception?
  • How do I organize my test cases?
  • How do I run setup code once for all my TestCases?
  • I want to debug when a test fails

    How do I implement a test case for a thrown exception?

    Catch the exception and if it isn't thrown call the Fail method. Fail signals the failure of a test case. Here is an example:
    public void TestArgumentOutOfRangeException() {
        ArrayList l = new ArrayList(10)
        try {
             Object o= l[l.Count];
             Fail("Should raise an ArgumentOutOfRangeException");
        } catch (ArgumentOutOfRangeException) {
        }
    }
    or use the ExceptionTestCase as follows.
    1) make your TestCase class a subclass of ExceptionTestCase.
    2) write the test ignoring exceptions
    public void TestArgumentOutOfRangeException() {
        ArrayList l= new ArrayList(10);
        l[l.Count];
    }
    3) create the TestCase:
        Test t= new ExceptionTestCase("TestArgumentOutOfRangeException", typeof(ArgumentOutOfRangeException))

    Looking at this again, the first way is simpler. Sigh...

    How do I organize my Test Cases?

    Here is one way:
    1. create a test namespace for each of your application namespaces. For example, for a namespace MyApp.Util define MyApp.UtilTest. Put all the fixtures for the Util namespace into this namespace.
    2. in MyApp.UtilTest define a class which creates a suite with all the tests in this namespace. To do so define a class AllTests which includes a single static Suite property. Here is an example:
       public static ITest Suite {
         get {
           TestSuite suite= new TestSuite();
           suite.AddTest(Fixture1.Suite);
           suite.AddTest(Fixture2.Suite);
           return suite;
         }
       }
    3. define similar AllTests classes that create higher level suites containing the suites from other test packages.
    When the fixtures are in a separate test assembly the test cases don't have access to the methods and fields with internal visibility. A variation of the above convention is to put all fixtures into the application assembly itself. This gives the fixtures access to all the assembly visible methods and fields. To separate the fixture classes from the production classes put them into a separate directory that you then add to the project. This makes it easy to ship the production classes independent of the fixtures.

      How do I run setup code once for all my TestCases?

      Wrap the top level suite in a subclass of TestSetup. Here is a sample AllTests.Suite method:
      protected class WrappedTestSetup: TestSetup {
        public WrappedTestSetup(ITest test) : base(test) {} 
        protected override void SetUp() {
          OneTimeSetUp();
         }
        };

      public static ITest Suite {
        get {
          TestSuite suite= new TestSuite();
          ...add your tests and suites here...
          TestSetup wrapper= new WrappedTestSetup(suite);
          return wrapper;
        }
      }

      I want to debug when a test fails

      Start the test runner under the debugger and configure the debugger so that it catches the NUnit.Framework.AssertionFailedError. How you do this depends on the used IDE. Most debuggers support to stop the program when a specific exception is fired. Notice, that this will only break into the debugger when an "anticipated" assertion failed error occurs.

      Where are the test runner properties documented?

      See the NUnit preferences documentation.