简体   繁体   English

每次NUnit测试后如何运行方法(即使拆解失败)

[英]How to run method after every NUnit test (even failure within teardown)

I am working on a test library using NUnit, and am generating a custom report while the tests run. 我正在使用NUnit开发测试库,并在测试运行时生成自定义报告。

In the TearDown of my tests I call a method that will report the result of the test. 在我的测试的TearDown ,我调用了一个将报告测试结果的方法。 It works just fine if the test passes, but is never reached if the test fails, is ignored, or inconclusive. 如果测试通过,它就可以正常工作,但是如果测试失败,被忽略或没有结论,则永远无法实现。

To make things more difficult, the "//do stuff" in the TearDown can also cause the test to fail in which case it would still need to be logged. 为了使事情变得更加困难, TearDown的“ //东西”还可能导致测试失败,在这种情况下,仍然需要记录该测试。 BUT Assert throws an exception which means it leaves the block and never reaches the reporting code. 但是, Assert抛出一个异常,这意味着它将离开该块并且永远不会到达报告代码。

[SetUp]
public void ExampleSetup()
{
    //whatever
}

[Test]
public void ExampleTest()
{
    //whatever
}

[TearDown]
public void ExampleTearDown()
{
    //do stuff
    someObject.ReportTestResult();
}

More Info - Using NUnit 3.2.0 更多信息 -使用NUnit 3.2.0

By looking up the NUnit documentation 's Framework Extensibility chapter we can see that you need to use the Action Attribute . 通过查看NUnit文档的框架可扩展性”一章,我们可以看到您需要使用Action Attribute

The Action Attribute extension point is: 动作属性扩展点是:

designed to better enable composability of test logic by creating attributes that encapsulate specific actions to be taken before or after a test is run. 通过创建包含封装在测试运行之前或之后要采取的特定操作的属性,旨在更好地实现测试逻辑的可组合性。

Summary of a basic implementation of this extension point 此扩展点的基本实现摘要

You need to implement the ITestAction interface in your given say FooBarActionAttribute() class. 您需要在给定的FooBarActionAttribute()类中实现ITestAction接口。

Given this you implement BeforeTest() , AfterTest() and Targets property. 鉴于此,您实现了BeforeTest()AfterTest()Targets属性。

For a basic scenario you need to perform your custom operations in the above two methods. 对于基本方案,您需要通过以上两种方法执行自定义操作。

The final thing you need to do is to use this attribute, for example as: 您需要做的最后一件事是使用此属性,例如:

[Test][FooBarActionAttribute()]
public void Should_Baz_a_FooBar() { 
    ...
}

This will get executed right before and after the test method run. 这将在测试方法运行之前和之后立即执行。

For more advanced techniques please consult the linked documentation, it's pretty straightforward. 有关更高级的技术,请查阅链接的文档,这非常简单。

Frankly, this is just a bad idea. 坦白说,这只是一个坏主意。 TearDown is part of your test. TearDown是您测试的一部分。 That's why it can fail. 这就是为什么它会失败。 (But note that NUnit treats an assert failure in teardown as an error rather than a failure) (但请注意,NUnit将拆卸中的断言失败视为错误而不是失败)

Since a test can do anything - good or bad, right or wrong - putting your logging into the test code is unreliable. 由于测试可以执行任何操作-好的或坏的,正确的或错误的,因此将日志记录到测试代码中是不可靠的。 You should be logging in the code that runs tests, not in the tests themselves. 您应该登录的是运行测试的代码,而不是测试本身。 As stated above, the TearDown is a part of your test. 如上所述,TearDown是测试的一部分。 So is any ActionAttribute you may define, as suggested in another answer. 正如您在另一个答案中所建议的那样,您可以定义的任何ActionAttribute也是如此。 Don't do logging there. 不要在那里记录日志。

Unfortunately, there is a history of doing logging within the tests because NUnit either didn't supply an alternative - at least not one that was easy to use. 不幸的是,在测试中有记录日志的历史,因为NUnit要么没有提供替代方案-至少没有一种易于使用。 For NUnit V2, you could create a test event listener addin for exactly this purpose. 对于NUnit V2,您可以为此目的创建一个测试事件侦听器插件。 You still can if that's the version of NUnit you are using. 如果那是您正在使用的NUnit的版本,您仍然可以。

For NUnit 3.0 and higher, you should create a listener extension that works with the TestEngine. 对于NUnit 3.0及更高版本,您应该创建一个与TestEngine一起使用的侦听器扩展 The TestEngine and its extensions are completely separate from your tests. TestEngine及其扩展名与测试完全分开。 Once you have a well-tested extension, you will be able to use it with all your tests and the tests won't be cluttered with logging code. 有了经过良好测试的扩展,您就可以在所有测试中使用它,并且测试不会因日志代码而混乱。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM