背景
的NUnit的框架扩展,我的工作需要从ITestAction
属性中了解每个测试的结果/结果,就像这样:NUnit TestContext.CurrentContext是否可安全地用于并行测试?
public class MyFrameworkExtensionAttribute : Attribute, ITestAction
{
public void BeforeTest(ITest test)
{ /* Omitted, not relevant to this question */ }
public void AfterTest(ITest test)
{
var result = TestContext.CurrentContext.Result;
var resultIsSuccess = result.Outcome.Status == TestStatus.Passed;
// Then go on to do stuff with that resultIsSuccess variable
}
}
注意在AfterTest
第一线方法;我正在使用静态引用:TestContext.CurrentContext。 ITest
接口不公开测试结果,所以它似乎是唯一的方法。
问题
而现在我的问题 - 因为NUnit的3.X包括功能并行运行测试,通过ParallelizableAttribute
- 我想知道:是否可以安全使用静态参考电流测试情境?我在这里应该担心的是线程安全问题吗?有没有更好的/更强大的方式来获得测试结果而不诉诸静态参考?
我在使用NUnit框架3.7.1以防万一。