Table of Contents

Class WorkItem

Namespace
NUnit.Framework.Internal.Execution
Assembly
nunit.framework.dll

A WorkItem may be an individual test case, a fixture or a higher level grouping of tests. All WorkItems inherit from the abstract WorkItem class, which uses the template pattern to allow derived classes to perform work in whatever way is needed.

A WorkItem is created with a particular TestExecutionContext and is responsible for re-establishing that context in the current thread before it begins or resumes execution.

public abstract class WorkItem : IDisposable
Inheritance
WorkItem
Implements
Derived
Inherited Members

Constructors

WorkItem(WorkItem)

Construct a work Item that wraps another work Item. Wrapper items are used to represent independently dispatched tasks, which form part of the execution of a single test, such as OneTimeTearDown.

public WorkItem(WorkItem wrappedItem)

Parameters

wrappedItem WorkItem

The WorkItem being wrapped

WorkItem(Test, ITestFilter)

Construct a WorkItem for a particular test.

public WorkItem(Test test, ITestFilter filter)

Parameters

test Test

The test that the WorkItem will run

filter ITestFilter

Filter used to include or exclude child items

Properties

Context

The execution context

public TestExecutionContext Context { get; }

Property Value

TestExecutionContext

ExecutionStrategy

The ParallelExecutionStrategy to use for this work item

public virtual ParallelExecutionStrategy ExecutionStrategy { get; }

Property Value

ParallelExecutionStrategy

Filter

Filter used to include or exclude child tests

public ITestFilter Filter { get; }

Property Value

ITestFilter

IsolateChildTests

Indicates whether this work item should use a separate dispatcher.

public virtual bool IsolateChildTests { get; }

Property Value

bool

Name

The name of the work item - defaults to the Test name.

public virtual string Name { get; }

Property Value

string

ParallelScope

Gets the ParallelScope associated with the test, if any, otherwise returning ParallelScope.Default;

public ParallelScope ParallelScope { get; }

Property Value

ParallelScope

Result

The test result

public TestResult Result { get; protected set; }

Property Value

TestResult

State

Gets the current state of the WorkItem

public WorkItemState State { get; protected set; }

Property Value

WorkItemState

Test

The test being executed by the work item

public Test Test { get; }

Property Value

Test

TestWorker

The worker executing this item.

public TestWorker? TestWorker { get; }

Property Value

TestWorker

Methods

BuildSetUpTearDownList(IMethodInfo[], IMethodInfo[], IMethodValidator?)

Builds the set up tear down list.

protected List<SetUpTearDownItem> BuildSetUpTearDownList(IMethodInfo[] setUpMethods, IMethodInfo[] tearDownMethods, IMethodValidator? methodValidator = null)

Parameters

setUpMethods IMethodInfo[]

Unsorted array of setup MethodInfos.

tearDownMethods IMethodInfo[]

Unsorted array of teardown MethodInfos.

methodValidator IMethodValidator

Method validator used before each method execution.

Returns

List<SetUpTearDownItem>

A list of SetUpTearDownItems

Cancel(bool)

Cancel (abort or stop) a WorkItem

public virtual void Cancel(bool force)

Parameters

force bool

true if the WorkItem should be aborted, false if it should run to completion

ChangeResult(ResultState, string)

Changes the result of the test, logging the old and new states

protected void ChangeResult(ResultState resultState, string message)

Parameters

resultState ResultState

The new ResultState

message string

The new message

Dispose()

Standard Dispose

public void Dispose()

Execute()

Execute the current work item, including any child work items.

public virtual void Execute()

InitializeContext(TestExecutionContext)

Initialize the TestExecutionContext. This must be done before executing the WorkItem.

public void InitializeContext(TestExecutionContext context)

Parameters

context TestExecutionContext

The TestExecutionContext to use

Remarks

Originally, the context was provided in the constructor but delaying initialization of the context until the item is about to be dispatched allows changes in the parent context during OneTimeSetUp to be reflected in the child.

MarkNotRunnable(string)

Marks the WorkItem as NotRunnable.

public void MarkNotRunnable(string reason)

Parameters

reason string

Reason for test being NotRunnable.

PerformWork()

Method that performs actually performs the work. It should set the State to WorkItemState.Complete when done.

protected abstract void PerformWork()

WaitForCompletion()

Wait until the execution of this item is complete

public void WaitForCompletion()

WorkItemComplete()

Method called by the derived class when all work is complete

protected void WorkItemComplete()

Events

Completed

Event triggered when the item is complete

public event EventHandler? Completed

Event Type

EventHandler