In this section we will look at some ways of implementing custom NUnit attributes, which can be used to alter test execution.
A powerful part of NUnit is that it is very extendable. One of the ways it can be extended is through custom attributes. An example is attributes that implement the IWrapTestMethod interface. This interface has a method for wrapping a TestCommand
, which implements a method for executing. Normally these test commands do something, call execute on their inner command and then maybe do something again after the inner command is completed.
In the following three classes an IWrapTestMethod
interface is implemented and used in a test:
public class MyAttribute : NUnitAttribute, IWrapTestMethod
{
public TestCommand Wrap(TestCommand command)
{
return new MyCommand(command);
}
}
public class MyCommand : TestCommand
{
private TestCommand innerCommand;
public MyCommand(TestCommand command) : base(command.Test)
{
innerCommand = command;
}
public override TestResult Execute(ITestExecutionContext context)
{
Debug.Log("Before");
var result = innerCommand.Execute(context);
Debug.Log("After");
return result;
}
}
public class MyTests
{
[Test]
[MyAttribute]
public void Test1()
{
Debug.Log("The test");
}
}
When running MyTests.Test1
the following output is printed:
Test1 (0,017s)
-–
Before
The test
After
Other interfaces that custom attributes can implement are IWrapSetUpTearDown
, IApplyToContext
, and IApplyToTest
.
At Unity we have a goal that an action should never take longer than 500 ms. In the sample 16_CustomAttributes
there is a class called MyClass
, which has two methods. Both methods are supposed to return true. However someone has made a regression so that one of the two methods takes a long time to run.
The task is to create a new custom attribute, which detects if the test takes longer than 500 ms to run. If that happens, it should fail the test with a descriptive message. Apply that to the two existing tests.
System.Diagnostics.Stopwatch
to time how many miliseconds have passed.A full solution for the exercise is availiable at 16_CustomAttributes_Solution
.
The core of the solution is the execute method in the test command implementation:
public override TestResult Execute(ITestExecutionContext context)
{
var stopWatch = new Stopwatch();
stopWatch.Start();
var result = innerCommand.Execute(context);
stopWatch.Stop();
if (stopWatch.ElapsedMilliseconds > 500)
{
result.SetResult(ResultState.Failure, $"Test took {stopWatch.ElapsedMilliseconds} ms. That is longer than 500ms!");
}
return result;
}
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.