TestExecutionContext
Helper class used to save and restore certain static or
singleton settings in the environment that affect tests
or which might be changed by the user tests.
An internal class is used to hold settings and a stack
of these objects is pushed and popped as Save and Restore
are called.
using NUnit.Framework.Interfaces;
using NUnit.Framework.Internal.Execution;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Threading;
namespace NUnit.Framework.Internal
{
public class TestExecutionContext
{
private TestExecutionContext _priorContext;
private TestExecutionStatus _executionStatus;
private ITestListener _listener = TestListener.NULL;
private int _assertCount;
private RandomGenerator _randomGenerator;
private IWorkItemDispatcher _dispatcher;
private CultureInfo _currentCulture;
private CultureInfo _currentUICulture;
private TestResult _currentResult;
[ThreadStatic]
private static TestExecutionContext current;
public static TestExecutionContext CurrentContext {
get {
if (current == null)
current = new TestExecutionContext();
return current;
}
private set {
current = value;
}
}
public Test CurrentTest { get; set; }
public DateTime StartTime { get; set; }
public long StartTicks { get; set; }
public TestResult CurrentResult {
get {
return _currentResult;
}
set {
_currentResult = value;
if (value != null)
OutWriter = value.OutWriter;
}
}
public TextWriter OutWriter { get; set; }
public object TestObject { get; set; }
public string WorkDirectory { get; set; }
public bool StopOnError { get; set; }
public TestExecutionStatus ExecutionStatus {
get {
if (_executionStatus == TestExecutionStatus.Running && _priorContext != null)
_executionStatus = _priorContext.ExecutionStatus;
return _executionStatus;
}
set {
_executionStatus = value;
if (_priorContext != null)
_priorContext.ExecutionStatus = value;
}
}
internal ITestListener Listener {
get {
return _listener;
}
set {
_listener = value;
}
}
internal IWorkItemDispatcher Dispatcher {
get {
if (_dispatcher == null)
_dispatcher = new SimpleWorkItemDispatcher();
return _dispatcher;
}
set {
_dispatcher = value;
}
}
public ParallelScope ParallelScope { get; set; }
public RandomGenerator RandomGenerator {
get {
if (_randomGenerator == null)
_randomGenerator = new RandomGenerator(CurrentTest.Seed);
return _randomGenerator;
}
}
internal int AssertCount => _assertCount;
public int TestCaseTimeout { get; set; }
public List<ITestAction> UpstreamActions { get; set; }
public CultureInfo CurrentCulture {
get {
return _currentCulture;
}
set {
_currentCulture = value;
Thread.CurrentThread.CurrentCulture = _currentCulture;
}
}
public CultureInfo CurrentUICulture {
get {
return _currentUICulture;
}
set {
_currentUICulture = value;
Thread.CurrentThread.CurrentUICulture = _currentUICulture;
}
}
public TestExecutionContext()
{
_priorContext = null;
TestCaseTimeout = 0;
UpstreamActions = new List<ITestAction>();
_currentCulture = CultureInfo.CurrentCulture;
_currentUICulture = CultureInfo.CurrentUICulture;
}
public TestExecutionContext(TestExecutionContext other)
{
_priorContext = other;
CurrentTest = other.CurrentTest;
CurrentResult = other.CurrentResult;
TestObject = other.TestObject;
WorkDirectory = other.WorkDirectory;
_listener = other._listener;
StopOnError = other.StopOnError;
TestCaseTimeout = other.TestCaseTimeout;
UpstreamActions = new List<ITestAction>(other.UpstreamActions);
_currentCulture = CultureInfo.CurrentCulture;
_currentUICulture = CultureInfo.CurrentUICulture;
Dispatcher = other.Dispatcher;
ParallelScope = other.ParallelScope;
}
public static void ClearCurrentContext()
{
CurrentContext = null;
}
public void UpdateContextFromEnvironment()
{
_currentCulture = CultureInfo.CurrentCulture;
_currentUICulture = CultureInfo.CurrentUICulture;
}
public void EstablishExecutionEnvironment()
{
Thread.CurrentThread.CurrentCulture = _currentCulture;
Thread.CurrentThread.CurrentUICulture = _currentUICulture;
CurrentContext = this;
}
public void IncrementAssertCount()
{
Interlocked.Increment(ref _assertCount);
}
public void IncrementAssertCount(int count)
{
while (count-- > 0) {
Interlocked.Increment(ref _assertCount);
}
}
}
}