Legacy Documentation. View NUnit 3 Documentation

TestAttribute (NUnit 2.0 / 2.5 / 2.6)

The Test attribute is one way of marking a method inside a TestFixture class as a test. For backwards compatibility with previous versions of Nunit a test method may also be found if the first 4 letters are "test" regardless of case. This option is available by setting a value in the config file for the test.

Prior to NUnit 2.5, the signature for a test method was:

        public void MethodName()

Beginning with NUnit 2.5, static methods may be used as tests:

        public static void MethodName()

In addition, with 2.5, test methods may have arguments and return values, provided that NUnit is told what values to use for the arguments and how to handle the return value. For more information on these capabilities, see Parameterized Tests as well as some of the related attributes listed at the end of this page.

Parameterized test methods may also be generic, provided that NUnit is able to deduce the proper argument types from the types of the data arguments supplied.

Beginning with NUnit 2.6.2, test methods targetting .Net 4.5 may be marked as async and NUnit will wait for the method to complete before recording the result and moving on to the next test.Async test methods may return void or Task if no value is returned, or Task<T> if a value of type T is returned.

If the programmer marks a test method that does not have the correct signature it will be considered as not runnable and be indicated as such by the console or gui runner. In the Gui, such tests are marked in red.

In the examples on this page, NUnit would have no way of knowing what values to supply as arguments, so methods without parameters are used.

Example:

namespace NUnit.Tests
{
  using System;
  using NUnit.Framework;

  [TestFixture]
  public class SuccessTests
  {
    [Test] public void Add()
    { /* ... */ }

    public void TestSubtract()
    { /* backwards compatibility */ }
  }
}
Imports System
Imports Nunit.Framework

Namespace Nunit.Tests

  <TestFixture()> Public Class SuccessTests
    <Test()> Public Sub Add()
    ' ...
    End Sub
  End Class
End Namespace
#using <Nunit.Framework.dll>
using namespace System;
using namespace NUnit::Framework;

namespace NUnitTests
{
  [TestFixture]
  public __gc class SuccessTests
  {
    [Test] void Add();
  };
}

#include "cppsample.h"

namespace NUnitTests {
  // ...
}
package NUnit.Tests;

import System.*;
import NUnit.Framework.TestFixture;


/** @attribute NUnit.Framework.TestFixture() */
public class SuccessTests
{
  /** @attribute NUnit.Framework.Test() */
  public void Add()
  { /* ... */ }
}

See also...