最近升级到NUnit 2.5.8以便使用新的TestContext功能.这样我们就可以确定当前的测试名称以便检索属性:
private T GetAttribute<T>() where T : class
{
return Attribute.GetCustomAttribute(GetType().GetMethod(TestContext.CurrentContext.Test.Name), typeof(T)) as T;
}
Run Code Online (Sandbox Code Playgroud)
但是,我们在访问属性TestContext.CurrentContext.Test.Name时看到NullReferenceExceptions,因为似乎没有正确注册NUnit CallContext.
有没有其他人遇到过这个问题,或者可以建议一种从SetUp确定当前测试名称的替代方法?
这不是您问题的答案,但我在使用TestDriven.NET 的 xcopy-deployable NUnit Test Runner来使用 NUnit 2.5.7 时遇到了类似的问题,并点击了NullReferenceException因为 TestDriven.NET 仍在 2.5 下运行测试。 5(它附带)而不是我的项目引用的 NUnit。
问题结果是,当我需要引用nunit.framework.dll中找到的内容NUnit\NUnit-2.5.7.10213\bin\net-2.0时,我正在引用 中找到的内容NUnit\NUnit-2.5.7.10213\bin\net-2.0\framework。我不知道为什么有两个副本,但指向正确的 DLL 就造成了一切不同。