Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Assert.Fail() considered bad practice?

I use Assert.Fail a lot when doing TDD. I'm usually working on one test at a time but when I get ideas for things I want to implement later I quickly write an empty test where the name of the test method indicates what I want to implement as sort of a todo-list. To make sure I don't forget I put an Assert.Fail() in the body.

When trying out xUnit.Net I found they hadn't implemented Assert.Fail. Of course you can always Assert.IsTrue(false) but this doesn't communicate my intention as well. I got the impression Assert.Fail wasn't implemented on purpose. Is this considered bad practice? If so why?


@Martin Meredith That's not exactly what I do. I do write a test first and then implement code to make it work. Usually I think of several tests at once. Or I think about a test to write when I'm working on something else. That's when I write an empty failing test to remember. By the time I get to writing the test I neatly work test-first.

@Jimmeh That looks like a good idea. Ignored tests don't fail but they still show up in a separate list. Have to try that out.

@Matt Howells Great Idea. NotImplementedException communicates intention better than assert.Fail() in this case

@Mitch Wheat That's what I was looking for. It seems it was left out to prevent it being abused in another way I abuse it.

like image 921
Mendelt Avatar asked Sep 23 '08 12:09

Mendelt


People also ask

What will happen if assert fails?

The assert Statement If the assertion fails, Python uses ArgumentExpression as the argument for the AssertionError. AssertionError exceptions can be caught and handled like any other exception using the try-except statement, but if not handled, they will terminate the program and produce a traceback.

What happens when JUnit assertion fails?

The fail assertion fails a test throwing an AssertionError. It can be used to verify that an actual exception is thrown or when we want to make a test failing during its development. In JUnit 5 all JUnit 4 assertion methods are moved to org.

What is assert fail in TestNG?

What does "assert fail" mean in TestNG? Assert fail refers to the failure of the assertion test method. The conditions for failing depends totally on the assertion methods. When an assertion fails, they throw an exception error onto the console describing the failed test (only in hard asserts).

Is assert good practice C++?

Assertions are entirely appropriate in C++ code. Exceptions and other error handling mechanisms aren't really intended for the same thing as assertions. Error handling is for when there's a potential for recovering or reporting an error nicely to the user.


1 Answers

For this scenario, rather than calling Assert.Fail, I do the following (in C# / NUnit)

[Test] public void MyClassDoesSomething() {     throw new NotImplementedException(); } 

It is more explicit than an Assert.Fail.

There seems to be general agreement that it is preferable to use more explicit assertions than Assert.Fail(). Most frameworks have to include it though because they don't offer a better alternative. For example, NUnit (and others) provide an ExpectedExceptionAttribute to test that some code throws a particular class of exception. However in order to test that a property on the exception is set to a particular value, one cannot use it. Instead you have to resort to Assert.Fail:

[Test] public void ThrowsExceptionCorrectly() {     const string BAD_INPUT = "bad input";     try     {         new MyClass().DoSomething(BAD_INPUT);         Assert.Fail("No exception was thrown");     }     catch (MyCustomException ex)     {          Assert.AreEqual(BAD_INPUT, ex.InputString);      } } 

The xUnit.Net method Assert.Throws makes this a lot neater without requiring an Assert.Fail method. By not including an Assert.Fail() method xUnit.Net encourages developers to find and use more explicit alternatives, and to support the creation of new assertions where necessary.

like image 92
Matt Howells Avatar answered Sep 18 '22 19:09

Matt Howells