1

I have medium level experience on unit testing and now I'm learning about NUnit.

As far as I can see, a NUnit test passes by default if you don't call Assert.Fail method.

In my test case, I want it to fail by default unless I call Assert.Pass method.

How can I do that if it's possible?

Simplified Example

Current behavior:

// This test passes
public void Test1()
{
}

// This test fails
public void Test1()
{
    Assert.Fail();
}

The behavior I want:

// This test must fail
public void Test1()
{
}

// This test must pass
public void Test1()
{
    Assert.Pass();
}

Detailed Example for curious cats

The last line of this test looks unnecessary to me. I would like to remove Assert.Fail() call at the end if it was possible to mark the test as "fail by default" somehow. Maybe a parameter for [Test] attribute???

[Test]
public void SetValue_RaisesChangedEvent()
{
    var Property = new Property();
    Property.Changed += (Sender, Args) => Assert.Pass();

    Property.SetValue(1);
    Assert.Fail();
}
7
  • May I ask why you're wanting this type of functionality? If anything, this should be an Inconclusive test but I do not see the need to fail a test that hasn't been properly written. Understanding your need will likely result in a better answer.
    – Paul Muir
    Mar 9, 2018 at 19:50
  • My actual test method won't be empty. I just simplified the example to better explain what I need.
    – Xtro
    Mar 9, 2018 at 23:04
  • 1
    I think you have simplified your example too much, in that it is not clear why you would want a test to fail if it ran without "error". Something that may not be obvious if new to NUnit: if your test has an unhandled exception, then the test will Fail, regardless if you have an Assert line or not. It is considered bad practice to forgo having an Assert at end of test though.
    – evets17
    Mar 10, 2018 at 1:17
  • Every test should assert something at last which is the main test objective without it the test is meaningless. Mar 10, 2018 at 20:39
  • @VishalAggarwal I'm a minimalist programmer. I don't write any extra word in my code unless it's mandatory for compiler and runtime. Since the test passes by default, I don't call Assert.Pass at the end of the test.
    – Xtro
    Mar 12, 2018 at 13:41

1 Answer 1

1

Editing my original answer as I incorrectly stated that test would continue after Assert.Pass() call.

http://nunit.org/docs/2.5/utilityAsserts.html

Your specific example could be re-written like this as well to avoid the strange Assert.Fail() call at end.

[Test]
public void SetValue_RaisesChangedEvent()
{
    var eventFired = false;
    var Property = new Property();
    Property.Changed += (Sender, Args) => eventFired = true;

    Property.SetValue(1);
    Assert.IsTrue(eventFired);
}

One could say, by default this test will fail, unless the Property.Changed event fires.

3
  • I think you are wrong. Assert.Pass and Assert.Fail throws an exception which breaks the execution of the test method.
    – Xtro
    Mar 14, 2018 at 1:40
  • you are right, edited my response to be accurate on that front. I guess I didn't answer question, but I would caution getting too fancy with trying to limit lines of code where a couple added lines can aid in others better understanding of the intent.
    – evets17
    Mar 14, 2018 at 4:24
  • That's a different approach to the problem but the problem still exists therefore, this is not the answer to my question. Thank you for your answer anyways.
    – Xtro
    Mar 15, 2018 at 19:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.