Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NUnit: Fail Test when OneTimeTearDown fails

Abstract goal: I’m working with NUnit (3.9.0 at the moment). Some work to finish the tests is done in the OneTimeTearDown method. When this work fails, I need an indication, i.e. one or more tests should fail.

Background: I’m implementing PACT Tests using NUnit as the Unit Test framework. On the consumer, after all tests are run, I have to generate and upload the PACT file to the PACT broker. If this fails, e.g. when the pact broker is down, I need to have an indication; i.e. one or all tests should fail.

Question: Is this possible using NUnit? If so: How? Or would there be a better approach to achieve my objective?

like image 518
kryz Avatar asked Oct 27 '25 08:10

kryz


2 Answers

You could call the same assert methods from your OneTimeTearDown method as you call from the tests. Here is example that fails test run from teardown method:

[TestFixture]
public class SomeClassTests
{
    [OneTimeTearDown]
    public void Cleanup()
    {
        Assert.Fail("Failed to upload file to the PACT broker");
    }

    [Test]
    public void TestMethod()
    {
        Assert.Pass();
    }
}

Here is execution result in ReSharper UT runner:

enter image description here

Another option to fail test run from teardown method is to throw exception.

like image 102
CodeFuller Avatar answered Oct 29 '25 22:10

CodeFuller


Yes, only you can use ReSharper, the output is clearly visible.but you also can use output or Nunit 2.X,Please see the detailsNunit3 console doesn't show test output continuously

like image 43
Leal Li Avatar answered Oct 29 '25 21:10

Leal Li



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!