我可以在NUnit运行测试之前和之后执行代码吗?

Bil*_*eal 14 c# nunit

我在NUnit中有一堆测试,它们在文件系统上创建了垃圾数据(糟糕,我知道,但我对此无法控制).目前我们有一个清除工具,可以删除这些临时工具等,但我希望能够自动运行该清理工具.所有测试运行完毕后,我必须能够运行它.我有类似的检查,我想在开始时做,以确保以前的运行中没有任何这些临时工具可能会改变测试的结果.

这样的事情很简单,还是我要为这样的事情实施一个全新的测试运行器?

Chr*_*isF 22

Yes,

Use the [SetUpFixture] attribute on a class and the [SetUp] and [TearDown] attributes on methods with that class.

The SetUp method in a SetUpFixture is executed once before any of the fixtures contained in its namespace. The TearDown method is executed once after all the fixtures have completed execution. In the examples below, the method RunBeforeAnyTests() is called before any tests or setup methods in the NUnit.Tests namespace. The method RunAfterAnyTests() is called after all the tests in the namespace as well as their individual or fixture teardowns have completed exection.

Source (it says 2.4 on the page, but it is available in 2.5)