NUnit 有条件拆卸?
有没有办法在 NUnit 中进行有条件的 TearDown?
我有一个 TestFixture,它只需要为几个测试运行清理代码,但我真的不想:
- 在每个测试上运行 TearDown 方法
- 创建一个私有帮助器方法,如果可以的话,从需要清理的测试中调用它躲开它
Is there a way to do a conditional TearDown in NUnit?
I have a TestFixture which has a need to run cleanup code for just a few tests, and I don't really want to:
- Run the TearDown method on every test
- Create a private helper method and call it from the tests requiring cleanup if I can avoid it
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不幸的是没有。
当所有测试完成后,您是否可以不在 [TestFixtureTearDown] 中进行清理? 我想这取决于是否必须在下一次测试运行之前完成清理。
或者,将需要清理的测试放在另一个类/TextFixture 中,远离其他测试。 然后您可以在其中使用不需要条件的 TearDown。
编辑:
我刚刚想到的一件事是,您可以扩展 NUnit - 创建您自己的自定义属性,您可以根据需要处理这些属性,尽管对于这种特殊需求来说可能实际上并不值得。 此处提到了这一点 。 就像我说的,我认为你不应该为此走这条路,但了解这一点还是很有用的
There isn't unfortunately.
Can you not do the cleanup in the [TestFixtureTearDown] instead, so once all the tests have finished? I guess that depends on whether the cleanup has to be done before the next test runs.
Alternatively, put those tests that require a cleanup in another class/TextFixture together, away from the other tests. Then you can use a TearDown in there which doesn't need to be conditional.
Edit:
One thing I've just thought of, which could be done to achieve the aim though probably isn't actually worth it for this particular need, is that you can extend NUnit - create your own custom attributes which you could handle however you wanted. This is mentioned here. Like I say, I don't think really you should go down that route for this, but is useful to know none-the-less
您可以将主 TearDown 放在基类中:
然后在具有不应运行拆卸代码的测试的类中覆盖它:
You can have the main TearDown in a base class:
and then override it in the class where you have the tests that should not run the tear down code:
使用BaseTest 中的测试扩展所有类,
使用 AfterTest 和 BeforeTest 挂钩。 无论有类别还是无类别都适用。
Extend all you classes with test from BaseTest
Use AfterTest and BeforeTest hooks. Works both with and without category.
我已经使用测试名称解决了这个问题:
或者,如果您想使用
Test2
(TestProject.TestClass.Test2
) 的全名,您可以将该行替换为
I have solved this using the name of the test:
Or if you want to use the full name of
Test2
(TestProject.TestClass.Test2
) you can replace the lineby