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.Compatibility;
using NUnit.Framework.Interfaces;
using NUnit.Framework.Internal.Execution;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Runtime.Remoting.Messaging;
using System.Security.Principal;
using System.Threading;
namespace NUnit.Framework.Internal
{
public class TestExecutionContext : LongLivedMarshalByRefObject
{
private TestExecutionContext _priorContext;
private TestExecutionStatus _executionStatus;
private ITestListener _listener = TestListener.NULL;
private int _assertCount;
private Randomizer _randomGenerator;
private IWorkItemDispatcher _dispatcher;
private CultureInfo _currentCulture;
private CultureInfo _currentUICulture;
private TestResult _currentResult;
private IPrincipal _currentPrincipal;
private static readonly string CONTEXT_KEY = "NUnit.Framework.TestContext";
public static TestExecutionContext CurrentContext {
get {
TestExecutionContext testExecutionContext = GetTestExecutionContext();
if (testExecutionContext == null) {
testExecutionContext = new TestExecutionContext();
CallContext.SetData(CONTEXT_KEY, (object)testExecutionContext);
}
return testExecutionContext;
}
private set {
if (value == null)
CallContext.FreeNamedDataSlot(CONTEXT_KEY);
else
CallContext.SetData(CONTEXT_KEY, (object)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 Randomizer RandomGenerator {
get {
if (_randomGenerator == null)
_randomGenerator = new Randomizer(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 IPrincipal CurrentPrincipal {
get {
return _currentPrincipal;
}
set {
_currentPrincipal = value;
Thread.CurrentPrincipal = _currentPrincipal;
}
}
public TestExecutionContext()
{
_priorContext = null;
TestCaseTimeout = 0;
UpstreamActions = new List<ITestAction>();
_currentCulture = CultureInfo.CurrentCulture;
_currentUICulture = CultureInfo.CurrentUICulture;
_currentPrincipal = Thread.CurrentPrincipal;
}
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;
_currentPrincipal = other.CurrentPrincipal;
Dispatcher = other.Dispatcher;
ParallelScope = other.ParallelScope;
}
public static TestExecutionContext GetTestExecutionContext()
{
return CallContext.GetData(CONTEXT_KEY) as TestExecutionContext;
}
public static void ClearCurrentContext()
{
CurrentContext = null;
}
public void UpdateContextFromEnvironment()
{
_currentCulture = CultureInfo.CurrentCulture;
_currentUICulture = CultureInfo.CurrentUICulture;
_currentPrincipal = Thread.CurrentPrincipal;
}
public void EstablishExecutionEnvironment()
{
Thread.CurrentThread.CurrentCulture = _currentCulture;
Thread.CurrentThread.CurrentUICulture = _currentUICulture;
Thread.CurrentPrincipal = _currentPrincipal;
CurrentContext = this;
}
public void IncrementAssertCount()
{
Interlocked.Increment(ref _assertCount);
}
public void IncrementAssertCount(int count)
{
while (count-- > 0) {
Interlocked.Increment(ref _assertCount);
}
}
public override object InitializeLifetimeService()
{
return null;
}
}
}