可以在 AssemblyCleanup 方法中访问 TestContext 吗?
在 Microsoft 的 UnitTesting 命名空间 (Microsoft.VisualStudio.TestTools.UnitTesting
) 中,您可以将 AssemblyInitialize
和 AssemblyCleanup
属性应用于静态方法,它们将分别在所有测试之前和之后调用。
[AssemblyInitialize]
static public void AssemblyInitialize(TestContext testCtx)
{
// allocate resources
}
[AssemblyCleanup]
static public void AssemblyCleanup()
{
// free resources
}
我的问题:在 AssemblyCleanup()
中访问 TestContext
是否可能且安全? 如果不是,将资源引用存储为静态成员是否是一个合理的替代方案,或者是否也会导致问题?
另外/可选:不将对 TestContext
的引用传递给清理方法的原因是什么?
In Microsoft's UnitTesting namespace (Microsoft.VisualStudio.TestTools.UnitTesting
) there are AssemblyInitialize
and AssemblyCleanup
attributes you can apply to static methods and they will be called before and after all tests respectively.
[AssemblyInitialize]
static public void AssemblyInitialize(TestContext testCtx)
{
// allocate resources
}
[AssemblyCleanup]
static public void AssemblyCleanup()
{
// free resources
}
My question: is it possible and safe to access the TestContext
within AssemblyCleanup()
? If not, is storing resource references as static members a reasonable alternative or could that cause problems as well?
Additionally/optionally: what is the reasoning behind not passing a reference to the TestContext
to clean-up methods?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我正在访问同一个类的静态属性,它似乎工作正常。 如果遇到任何问题,我会更新这个答案。 然而,我没有访问
TestContext
,所以我很好奇这是否也有效。I'm accessing a static property on the same class and it seems to be working fine. I'll update this answer if I encounter any problems. I am not, however, accessing the
TestContext
so I'm curious if that would work too.您无法将任何参数传递给 AssemblyCleanup 方法。 如果您尝试这样做,则会出现以下错误:
You can't pass any paramaters to AssemblyCleanup method. Here's the error if you try to do so: