4

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?

Pang
  • 9,564
  • 146
  • 81
  • 122
kryz
  • 116
  • 8

2 Answers2

2

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.

CodeFuller
  • 30,317
  • 3
  • 63
  • 79
  • 1
    Unfortunately I can’t reproduce your solution. Using NUnit 3.9.0, Visual Studio 2015 & ReSharper 10.0.2. Neither in the ReSharper UnitTest Runner, nor in the Visual Studio Test Explorer I can get a hint that something went wrong. I only see it in the Test-Text-Output. But this is very unhandy, I doubt the developers would see this hint. And we would need to hack something on the build servers. I also can’t find any flag to set in NUnit to reproduce your solution. Or any setting in ReSharper. – kryz Feb 05 '18 at 09:58
  • It works for me using NUnit 3.9.0, Visual Studio 2015 and ReSharper 2016.1.1. So probably this feature was introduced in some version of ReSharper UT Runner after 10.0.2. – CodeFuller Feb 06 '18 at 14:30
  • With an updated ReSharper Version it works as you described. Whether I could convince all our developers to even install ReSharper is another question ;) And this solution only covers the development machines. On our build servers the builds are still green. Therefor I would prefer to have a way where an actual TestCase fails. This would cover all scenarios. Nevertheless, thanks a lot for your input! – kryz Feb 07 '18 at 13:58
1

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

Leal Li
  • 247
  • 1
  • 2
  • 10