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

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.
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; private static readonly AsyncLocal<TestExecutionContext> _currentContext = new AsyncLocal<TestExecutionContext>(); public static TestExecutionContext CurrentContext { get { return _currentContext.Value; } private set { _currentContext.Value = 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 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 Tolerance DefaultFloatingPointTolerance { 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; DefaultFloatingPointTolerance = Tolerance.Default; } public TestExecutionContext(TestExecutionContext other) { _priorContext = other; CurrentTest = other.CurrentTest; CurrentResult = other.CurrentResult; TestObject = other.TestObject; _listener = other._listener; StopOnError = other.StopOnError; TestCaseTimeout = other.TestCaseTimeout; UpstreamActions = new List<ITestAction>(other.UpstreamActions); _currentCulture = other.CurrentCulture; _currentUICulture = other.CurrentUICulture; DefaultFloatingPointTolerance = other.DefaultFloatingPointTolerance; CurrentValueFormatter = other.CurrentValueFormatter; Dispatcher = other.Dispatcher; ParallelScope = other.ParallelScope; IsSingleThreaded = other.IsSingleThreaded; } 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; } } }