SimpleWorkItem
A SimpleWorkItem represents a single test case and is
marked as completed immediately upon execution. This
class is also used for skipped or ignored test suites.
using NUnit.Framework.Interfaces;
using NUnit.Framework.Internal.Abstractions;
using NUnit.Framework.Internal.Builders;
using NUnit.Framework.Internal.Commands;
using NUnit.Framework.Internal.Extensions;
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
namespace NUnit.Framework.Internal.Execution
{
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
public class SimpleWorkItem : WorkItem
{
private readonly IDebugger _debugger;
private readonly TestMethod _testMethod;
internal SimpleWorkItem(TestMethod test, ITestFilter filter, IDebugger debugger)
: base(test, filter)
{
_debugger = debugger;
_testMethod = test;
}
protected override void PerformWork()
{
try {
TestCommand testCommand = MakeTestCommand();
base.Result = ContextUtils.DoIsolated(() => testCommand.Execute(base.Context));
} catch (Exception ex) {
base.Context.CurrentResult.RecordException(ex);
base.Result = base.Context.CurrentResult;
} finally {
WorkItemComplete();
}
}
internal TestCommand MakeTestCommand()
{
if (base.Test.RunState == RunState.Runnable || (base.Test.RunState == RunState.Explicit && base.Filter.IsExplicitMatch(base.Test))) {
TestCommand testCommand = new TestMethodCommand(_testMethod);
MethodInfoCache.TestMethodMetadata testMethodMetadata = MethodInfoCache.Get(_testMethod.Method);
IWrapTestMethod[] wrapTestMethodAttributes = testMethodMetadata.WrapTestMethodAttributes;
foreach (IWrapTestMethod wrapTestMethod in wrapTestMethodAttributes) {
testCommand = wrapTestMethod.Wrap(testCommand);
}
ITestAction[] actions = base.Test.Actions;
foreach (ITestAction testAction in actions) {
if (testAction.Targets == ActionTargets.Default || testAction.Targets.HasFlag(ActionTargets.Test))
testCommand = new TestActionCommand(testCommand, testAction);
}
TestFixture testFixture = (base.Test.Parent as TestFixture) ?? (base.Test.Parent?.Parent as TestFixture);
ITypeInfo typeInfo = base.Test.TypeInfo;
IMethodInfo[] setUpMethods = testFixture?.SetUpMethods ?? typeInfo.GetMethodsWithAttribute<SetUpAttribute>(true);
IMethodInfo[] tearDownMethods = testFixture?.TearDownMethods ?? typeInfo.GetMethodsWithAttribute<TearDownAttribute>(true);
List<SetUpTearDownItem> list = BuildSetUpTearDownList(setUpMethods, tearDownMethods, null);
foreach (SetUpTearDownItem item in list) {
testCommand = new SetUpTearDownCommand(testCommand, item);
}
bool flag = base.Test.HasLifeCycle(LifeCycle.InstancePerTestCase);
if (flag && testFixture != null && DisposeHelper.IsDisposable(testFixture.TypeInfo.Type))
testCommand = new DisposeFixtureCommand(testCommand);
int num = base.Context.UpstreamActions.Count;
while (--num >= 0) {
ITestAction action = base.Context.UpstreamActions[num];
testCommand = new TestActionCommand(testCommand, action);
}
IWrapSetUpTearDown[] wrapSetupTearDownAttributes = testMethodMetadata.WrapSetupTearDownAttributes;
foreach (ICommandWrapper commandWrapper in wrapSetupTearDownAttributes) {
testCommand = commandWrapper.Wrap(testCommand);
}
IApplyToContext[] applyToContextAttributes = testMethodMetadata.ApplyToContextAttributes;
foreach (IApplyToContext change in applyToContextAttributes) {
testCommand = new ApplyChangesToContextCommand(testCommand, change);
}
if (flag)
testCommand = new FixturePerTestCaseCommand(testCommand);
int num2 = base.Test.Properties.TryGet("Timeout", base.Context.TestCaseTimeout);
bool flag2 = base.Test.Properties.TryGet("UseCancellation", base.Context.UseCancellation);
if (num2 > 0)
testCommand = (flag2 ? ((DelegatingTestCommand)new CancelAfterCommand(testCommand, num2, _debugger)) : ((DelegatingTestCommand)new TimeoutCommand(testCommand, num2, _debugger)));
IRepeatTest[] repeatTestAttributes = testMethodMetadata.RepeatTestAttributes;
foreach (IRepeatTest repeatTest in repeatTestAttributes) {
testCommand = repeatTest.Wrap(testCommand);
}
return testCommand;
}
return new SkipCommand(_testMethod);
}
}
}