Legacy Documentation. View NUnit 3 Documentation

NUnit Addins

Preliminary documentation, subject to change.

NUnit originally identified tests in the time-honored way used in many xUnit test frameworks. Test classes inherited from the framework's TestCase class. Individual test case methods were identified by having names starting with "test..."

With NUnit 2.0, we introduced the use of attributes to identify both fixtures and test cases. Use of attributes in this way was a natural outcome of their presence in .NET and gave us a way of identifying tests that was completely independent of both inheritance and naming conventions.

However, by moving away from an inheritance-based mechanism we no longer had an easy way for others to extend NUnit's internal behavior. NUnit Addins are intended to fill that gap, providing an mechanism to introduce new or changed behavior without modifying NUnit itself.

Extension Points, Extensions and Addins

NUnit provides a number of Extension Points, places where it is possible to extend its behavior. Because NUnit works with various hosts and uses separate AppDomains to run tests, Extension Points are categorized into three types: Core, Client and Gui. Each of these types is supported by a different Extension Host.

An NUnit Addin provides enhanced functionality through one or more extensions, which it installs at identified Extension Points. Each Addin is characterized by the types of extensions it supplies, so that an Extension Host knows whether to invoke it.

Note: In the current release, only Core extensions are actually supported. An Addin may characterize itself as a Client or Gui extension and will be listed as such in the Addins Dialog, but no other action is taken.

Addin Identification, Loading and Installation

NUnit examines all assemblies in the bin/addins directory, looking for public classes with the NUnitAddinAttribute and implementing the IAddin interface. It loads all those it finds as Addins.

NUnitAddinAttribute supports three optional named parameters: Type, Name and Description. Name and Description are strings representing the name of the extension and a description of what it does. If no name is provided, the name of the class is used. Type may be any one or a combination of the ExtensionType enum members:

	[Flags]
	public enum ExtensionType
	{
		Core=1,
		Client=2,
		Gui=4
	}
The values may be or'ed together, allowing for future Addins that require extensions at multiple levels of the application. If not provided, Type defaults to ExtensionType.Core.

The IAddin interface, which must be implemented by each Addin, is defined as follows:

	public interface IAddin
	{
		bool Install( IExtensionHost host );
	}

The Install method is called by each host for which the addin has specified an ExtensionType. The addin should check that the necessary extension points are available and install itself, returning true for success or false for failure to install. The method will be called once for each extension host and - for Core extensions - each time a new test domain is loaded.

The Install method uses the IExtensionHost interface to locate extension points. It is defined as follows:

	public interface IExtensionHost
	{
	 	IExtensionPoint[] ExtensionPoints { get; }
		IFrameworkRegistry FrameworkRegistry{ get; }
		IExtensionPoint GetExtensionPoint( string name );
	}

The ExtensionPoints property returns an array of all extension points for those extensions that need the information. The FrameworkRegistry is provided for advanced extensions that emulate external test frameworks. See the source code for details.

Most addins will only need to use the GetExtensionPoint method to get the interface to a particular extension point. The IExtensionPoint interface is defined as follows:

	public interface IExtensionPoint
	{
		string Name { get; }
		IExtensionHost Host { get; }
		void Install( object extension );
		void Remove( object extension );
	}

Once again, most addins will only need to use one method - the Install method in this case. This method passes an extension object to the Extension Point where it is installed. Generally, extensions do not need to remove themselves once installed, but the method is provided in any case.

Extension Point Details

Depending on the particular extension point, the object passed will need to implement one or more interfaces. The following ExtensionPoints are implemented in this release of NUnit:

For examples of implementing each type of extension, see the Extensibility samples provided with NUnit. More complete examples are available in the code of NUnit itself, since NUnit uses the same mechanism internally.

SuiteBuilders

Addins use the host to access this extension point by name:

	IExtensionPoint suiteBuilders = host.GetExtensionPoint( "SuiteBuilders" );

The extension object passed to Install must implement the ISuiteBuilder interface:

	public interface ISuiteBuilder
	{
		bool CanBuildFrom( Type type );
		Test BuildFrom( Type type );
	}

The BuildFrom method should return a test fixture completely populated with its contained test cases.

TestCaseBuilders

Addins use the host to access this extension point by name:

	IExtensionPoint testCaseBuilders = host.GetExtensionPoint( "TestCaseBuilders" );

The extension object passed to Install must implement the ITestCaseBuilder interface:

	public interface ITestCaseBuilder
	{
		bool CanBuildFrom( MethodInfo method );
		Test BuildFrom( MethodInfo method );
	}

Note that this extension point will be called for methods in any type of fixture. If the addin is intended to only work on methods within a particular type of fixture, the CanBuildFrom method must check the fixture type.

TestDecorators

Addins use the host to access this extension point by name:

	IExtensionPoint testDecorators = host.GetExtensionPoint( "TestDecorators" );

The extension object passed to Install must implement the ITestDecorator interface:

	public interface ITestDecorator
	{
		Test Decorate( Test test, MemberInfo member );
	}

The Decorator may do several things, depending on what it needs to accomplish:

  1. Return test unmodified
  2. Modify properties of the test object and return it
  3. Replace test with another object, either discarding the original or aggregating it in the new test.

EventListeners

Addins use the host to access this extension point by name:

	IExtensionPoint listeners = host.GetExtensionPoint( "EventListeners" );

The extension object passed to Install must implement the EventListener interface:

	public interface EventListener
	{
		void RunStarted( string name, int testCount );
		void RunFinished( TestResult result );
		void RunFinished( Exception exception );
		void TestStarted(TestName testName);
		void TestFinished(TestCaseResult result);
		void SuiteStarted(TestName testName);
		void SuiteFinished(TestSuiteResult result);
		void UnhandledException( Exception exception );
		void TestOutput(TestOutput testOutput);
	}

You must provide all the methods, but the body may be empty for any that you have no need of.

Tips for Writing Extensions

An Extenders Guide will be published in the future. At the moment, writing an extension is a bit of an adventure. Extension authors are advised to join the nunit-developer list and post questions and comments there.

For the moment, the following tips may be of assistance.

  • The nunit.core.interfaces assembly is intended to be stable in the future while the nunit.core assembly will change from release to release. Right now, both assemblies are still in flux, but extensions that depend solely on the interfaces assembly will have a much better chance of surviving NUnit version changes. Unfortunately, this is rather difficult to do without duplicating a great deal of NUnit code. Most of the add-in samples provided with NUnit are currently version dependent.
  • If you place the definition of a custom attribute in the same assembly as your add-in, then user tests are dependent on the add-in assembly. If the add-in is version-dependent, then the user tests will also be version-dependent. We suggest placing any types referenced by user tests in a separate assembly, particularly if your extension relies on nunit.core.
  • If using Visual Studio, set Copy Local to false for any references to nunit.core or nunit.core.interfaces. This is especially important if you are also building NUnit itself.
  • There is currently no mechanism to allow decorators to apply in a particular order. NUnit applies decorators in the order in which they are returned through reflection, which may vary among different runtimes.
  • Avoid trying to "stretch" the existing extension points to do more than they were intended to do. Rather, let us know what further extension points you would like to see!