BeforeAndAfterTestCommand
TestActionCommand handles a single ITestAction applied
to a test. It runs the BeforeTest method, then runs the
test and finally runs the AfterTest method.
using System;
using System.Runtime.CompilerServices;
namespace NUnit.Framework.Internal.Commands
{
[NullableContext(1)]
[Nullable(0)]
public abstract class BeforeAndAfterTestCommand : DelegatingTestCommand
{
[Nullable(new byte[] {
2,
1
})]
protected Action<TestExecutionContext> BeforeTest;
[Nullable(new byte[] {
2,
1
})]
protected Action<TestExecutionContext> AfterTest;
public BeforeAndAfterTestCommand(TestCommand innerCommand)
: base(innerCommand)
{
}
public override TestResult Execute(TestExecutionContext context)
{
Guard.OperationValid(BeforeTest != null, "BeforeTest was not set by the derived class constructor");
Guard.OperationValid(AfterTest != null, "AfterTest was not set by the derived class constructor");
Test test = base.Test;
if (test.Fixture == null) {
object obj = test.Fixture = context.TestObject;
}
DelegatingTestCommand.RunTestMethodInThreadAbortSafeZone(context, delegate {
BeforeTest(context);
context.CurrentResult = innerCommand.Execute(context);
});
if (context.ExecutionStatus != TestExecutionStatus.AbortRequested)
DelegatingTestCommand.RunTestMethodInThreadAbortSafeZone(context, delegate {
AfterTest(context);
});
return context.CurrentResult;
}
}
}