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

TestContext

public class TestContext
Provide the context information of the current test. This is an adapter for the internal ExecutionContext class, hiding the internals from the user test.
using NUnit.Framework.Constraints; using NUnit.Framework.Interfaces; using NUnit.Framework.Internal; using NUnit.Framework.Internal.Execution; using System; using System.Collections.Generic; using System.IO; using System.Reflection; namespace NUnit.Framework { public class TestContext { public class TestAdapter { private readonly Test _test; public string ID => _test.Id; public string Name => _test.Name; public string MethodName { get { if (!(_test is TestMethod)) return null; return _test.Method.Name; } } public string FullName => _test.FullName; public string ClassName => _test.ClassName; public PropertyBagAdapter Properties => new PropertyBagAdapter(_test.Properties); public object[] Arguments => _test.Arguments; public TestAdapter(Test test) { _test = test; } } public class ResultAdapter { private readonly TestResult _result; public ResultState Outcome => _result.ResultState; public IEnumerable<AssertionResult> Assertions => _result.AssertionResults; public string Message => _result.Message; public virtual string StackTrace => _result.StackTrace; public int FailCount => _result.FailCount; public int WarningCount => _result.WarningCount; public int PassCount => _result.PassCount; public int SkipCount => _result.SkipCount; public int InconclusiveCount => _result.InconclusiveCount; public ResultAdapter(TestResult result) { _result = result; } } public class PropertyBagAdapter { private IPropertyBag _source; public IEnumerable<object> this[string key] { get { List<object> list = new List<object>(); foreach (object item in _source[key]) { list.Add(item); } return list; } } public ICollection<string> Keys => _source.Keys; public PropertyBagAdapter(IPropertyBag source) { _source = source; } public object Get(string key) { return _source.Get(key); } public bool ContainsKey(string key) { return _source.ContainsKey(key); } public int Count(string key) { return _source[key].Count; } } private readonly TestExecutionContext _testExecutionContext; private TestAdapter _test; private ResultAdapter _result; public static TextWriter Error = new EventListenerTextWriter("Error", Console.Error); public static readonly TextWriter Progress = new EventListenerTextWriter("Progress", Console.Error); public static readonly TestParameters Parameters = new TestParameters(); internal static string DefaultWorkDirectory; public static TestContext CurrentContext => new TestContext(TestExecutionContext.CurrentContext); public static TextWriter Out => TestExecutionContext.CurrentContext.OutWriter; public TestAdapter Test => _test ?? (_test = new TestAdapter(_testExecutionContext.CurrentTest)); public ResultAdapter Result => _result ?? (_result = new ResultAdapter(_testExecutionContext.CurrentResult)); public string WorkerId => _testExecutionContext.TestWorker.Name; public string TestDirectory { get { Assembly assembly = _testExecutionContext?.CurrentTest?.TypeInfo?.Assembly; if (assembly != (Assembly)null) return AssemblyHelper.GetDirectoryName(assembly); return AssemblyHelper.GetDirectoryName(Assembly.GetCallingAssembly()); } } public string WorkDirectory => DefaultWorkDirectory; public Randomizer Random => _testExecutionContext.RandomGenerator; public int AssertCount => _testExecutionContext.AssertCount; public int CurrentRepeatCount => _testExecutionContext.CurrentRepeatCount; public TestContext(TestExecutionContext testExecutionContext) { _testExecutionContext = testExecutionContext; } public static void Write(bool value) { Out.Write(value); } public static void Write(char value) { Out.Write(value); } public static void Write(char[] value) { Out.Write(value); } public static void Write(double value) { Out.Write(value); } public static void Write(int value) { Out.Write(value); } public static void Write(long value) { Out.Write(value); } public static void Write(decimal value) { Out.Write(value); } public static void Write(object value) { Out.Write(value); } public static void Write(float value) { Out.Write(value); } public static void Write(string value) { Out.Write(value); } [CLSCompliant(false)] public static void Write(uint value) { Out.Write(value); } [CLSCompliant(false)] public static void Write(ulong value) { Out.Write(value); } public static void Write(string format, object arg1) { Out.Write(format, arg1); } public static void Write(string format, object arg1, object arg2) { Out.Write(format, arg1, arg2); } public static void Write(string format, object arg1, object arg2, object arg3) { Out.Write(format, arg1, arg2, arg3); } public static void Write(string format, params object[] args) { Out.Write(format, args); } public static void WriteLine() { Out.WriteLine(); } public static void WriteLine(bool value) { Out.WriteLine(value); } public static void WriteLine(char value) { Out.WriteLine(value); } public static void WriteLine(char[] value) { Out.WriteLine(value); } public static void WriteLine(double value) { Out.WriteLine(value); } public static void WriteLine(int value) { Out.WriteLine(value); } public static void WriteLine(long value) { Out.WriteLine(value); } public static void WriteLine(decimal value) { Out.WriteLine(value); } public static void WriteLine(object value) { Out.WriteLine(value); } public static void WriteLine(float value) { Out.WriteLine(value); } public static void WriteLine(string value) { Out.WriteLine(value); } [CLSCompliant(false)] public static void WriteLine(uint value) { Out.WriteLine(value); } [CLSCompliant(false)] public static void WriteLine(ulong value) { Out.WriteLine(value); } public static void WriteLine(string format, object arg1) { Out.WriteLine(format, arg1); } public static void WriteLine(string format, object arg1, object arg2) { Out.WriteLine(format, arg1, arg2); } public static void WriteLine(string format, object arg1, object arg2, object arg3) { Out.WriteLine(format, arg1, arg2, arg3); } public static void WriteLine(string format, params object[] args) { Out.WriteLine(format, args); } public static void AddFormatter(ValueFormatterFactory formatterFactory) { TestExecutionContext.CurrentContext.AddFormatter(formatterFactory); } public static void AddTestAttachment(string filePath, string description = null) { Guard.ArgumentNotNull(filePath, "filePath"); Guard.ArgumentValid(filePath.IndexOfAny(Path.GetInvalidPathChars()) == -1, $"""{filePath}", "filePath"); if (!Path.IsPathRooted(filePath)) filePath = Path.Combine(CurrentContext.WorkDirectory, filePath); if (!File.Exists(filePath)) throw new FileNotFoundException("Test attachment file path could not be found.", filePath); TestExecutionContext.CurrentContext.CurrentResult.AddTestAttachment(new TestAttachment(filePath, description)); } public static void AddFormatter<TSUPPORTED>(ValueFormatter formatter) { AddFormatter((ValueFormatter next) => delegate(object val) { if (!(val is TSUPPORTED)) return next(val); return formatter(val); }); } } }