<PackageReference Include="NUnit" Version="3.6.0" />

TestExecutionContext

public class 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.Constraints; 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 { public class IsolatedContext : IDisposable { private TestExecutionContext _originalContext; public IsolatedContext() { _originalContext = CurrentContext; CurrentContext = _originalContext.CreateIsolatedContext(); } public void Dispose() { CurrentContext = _originalContext; } } private TestExecutionContext _priorContext; private TestExecutionStatus _executionStatus; private ITestListener _listener = TestListener.NULL; private int _assertCount; private Randomizer _randomGenerator; private CultureInfo _currentCulture; private CultureInfo _currentUICulture; private TestResult _currentResult; [ThreadStatic] private static TestExecutionContext _currentContext; public static TestExecutionContext CurrentContext { get { if (_currentContext == null) _currentContext = new TestExecutionContext(); return _currentContext; } private set { _currentContext = 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; } } public IWorkItemDispatcher Dispatcher { get; set; } public ParallelScope ParallelScope { get; set; } public string WorkerId { get; set; } public Randomizer RandomGenerator { get { if (_randomGenerator == null) _randomGenerator = new Randomizer(CurrentTest.Seed); return _randomGenerator; } } internal int AssertCount => _assertCount; internal int MultipleAssertLevel { get; set; } public int TestCaseTimeout { get; set; } public List<ITestAction> UpstreamActions { get; set; } public CultureInfo CurrentCulture { get { return _currentCulture; } set { _currentCulture = value; } } public CultureInfo CurrentUICulture { get { return _currentUICulture; } set { _currentUICulture = value; } } public ValueFormatter CurrentValueFormatter { get; set; } public bool IsSingleThreaded { get; set; } public TestExecutionContext() { _priorContext = null; TestCaseTimeout = 0; UpstreamActions = new List<ITestAction>(); _currentCulture = CultureInfo.CurrentCulture; _currentUICulture = CultureInfo.CurrentUICulture; CurrentValueFormatter = ((object val) => MsgUtils.DefaultValueFormatter(val)); IsSingleThreaded = false; } 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 = other.CurrentCulture; _currentUICulture = other.CurrentUICulture; CurrentValueFormatter = other.CurrentValueFormatter; Dispatcher = other.Dispatcher; ParallelScope = other.ParallelScope; IsSingleThreaded = other.IsSingleThreaded; } public static TestExecutionContext GetTestExecutionContext() { return _currentContext; } public static void ClearCurrentContext() { CurrentContext = null; } public void UpdateContextFromEnvironment() { _currentCulture = CultureInfo.CurrentCulture; _currentUICulture = CultureInfo.CurrentUICulture; } public void EstablishExecutionEnvironment() { CurrentContext = this; } public void IncrementAssertCount() { Interlocked.Increment(ref _assertCount); } public void IncrementAssertCount(int count) { while (count-- > 0) { Interlocked.Increment(ref _assertCount); } } public void AddFormatter(ValueFormatterFactory formatterFactory) { CurrentValueFormatter = formatterFactory(CurrentValueFormatter); } private TestExecutionContext CreateIsolatedContext() { TestExecutionContext testExecutionContext = new TestExecutionContext(this); if (testExecutionContext.CurrentTest != null) testExecutionContext.CurrentResult = testExecutionContext.CurrentTest.MakeTestResult(); return testExecutionContext; } } }