TestMethodCommand
TestMethodCommand is the lowest level concrete command
used to run actual test cases.
using NUnit.Framework.Interfaces;
using System;
namespace NUnit.Framework.Internal.Commands
{
public class TestMethodCommand : TestCommand
{
private readonly TestMethod testMethod;
private readonly object[] arguments;
public TestMethodCommand(TestMethod testMethod)
: base(testMethod)
{
this.testMethod = testMethod;
arguments = testMethod.Arguments;
}
public override TestResult Execute(TestExecutionContext context)
{
object actual = RunTestMethod(context);
if (testMethod.HasExpectedResult)
Assert.AreEqual(testMethod.ExpectedResult, actual);
context.CurrentResult.SetResult(ResultState.Success);
if (context.CurrentResult.AssertionResults.Count > 0)
context.CurrentResult.RecordTestCompletion();
return context.CurrentResult;
}
private object RunTestMethod(TestExecutionContext context)
{
if (AsyncToSyncAdapter.IsAsyncOperation(testMethod.Method.MethodInfo))
try {
return AsyncToSyncAdapter.Await(() => InvokeTestMethod(context));
} catch (Exception inner) {
throw new NUnitException("Rethrown", inner);
}
return InvokeTestMethod(context);
}
private object InvokeTestMethod(TestExecutionContext context)
{
return testMethod.Method.Invoke(context.TestObject, arguments);
}
}
}