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?
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:

Another option to fail test run from teardown method is to throw exception.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With