aspose file tools*
The moose likes Testing and the fly likes xUnit - assertion failures Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Testing
Bookmark "xUnit - assertion failures" Watch "xUnit - assertion failures" New topic
Author

xUnit - assertion failures

Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30751
    
156

I have been using shUnit (UNIX korn shell unit) in one of my classes. It runs all of the assertions in a method regardless of whether previous assertions fail. I found it interesting that all the xUnit frameworks don't behave the same way in this.

Personally, I like the way jUnit functions better in all this. Mainly, because I only care about the first error. shUnit's system encourages me to write "bad" tests that test a bunch of different things.

I'm curious which way people here prefer? Just the first assertion failing or all of them running?


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Originally posted by Jeanne Boyarsky:
I'm curious which way people here prefer? Just the first assertion failing or all of them running?

I'd prefer stopping on the first failing assertion because the rest of the assertions are making assumptions about the fixture's state that may or may not be true since a previous assertion failed. I can imagine that one might want to run a set of assertions regardless of whether the others failed or not, but when that happens (once in a year?), it's usually enough to just temporarily comment out the first failing assertion and re-run the test.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

Why would it be useful to run an assertion when the previous one failed ? Is the second one independent of the first?


Groovy
Alexandru Popescu
Ranch Hand

Joined: Jul 12, 2004
Posts: 995
Usually assertion mechanism is used during development to mark indispensable/mandatory conditions. It is said that a true assertion marks the fact that the system reached/is going through a wrong execution path (that will make the system unusable). It will be no benefit to continue.

./pope


blog - InfoQ.com
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30751
    
156

"Why would it be useful to run an assertion when the previous one failed ? Is the second one independent of the first?"
Pradeep,
In well crafted tests, it generally isn't. Sometimes during testing, I'll want to see if a later test fails to give me more insight into the problem. In that case, I do what Lasse does an uncomment it. If the assert doesn't fail, I can cram everything into one test method. This is a bad practice and I like that junit doesn't encourage it.

"It will be no benefit to continue."
Ali,
In the "real" code, this is true. In test code, sometimes assertions are independent. Although as I described above, this is often a code smell.
Alexandru Popescu
Ranch Hand

Joined: Jul 12, 2004
Posts: 995
Wouldn't it be the smell of at least 2 tests? (i feel i'm loosing it)

./pope
Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982
I have a question on assertion. Does it really that useful for testing private method? It is useful, if we know some methods may encounter problem, but how should we know? Is that mean, I need to put down the assertion checking for each private method?

Nick


SCJP 1.2, OCP 9i DBA, SCWCD 1.3, SCJP 1.4 (SAI), SCJD 1.4, SCWCD 1.4 (Beta), ICED (IBM 287, IBM 484, IBM 486), SCMAD 1.0 (Beta), SCBCD 1.3, ICSD (IBM 288), ICDBA (IBM 700, IBM 701), SCDJWS, ICSD (IBM 348), OCP 10g DBA (Beta), SCJP 5.0 (Beta), SCJA 1.0 (Beta), MCP(70-270), SCBCD 5.0 (Beta), SCJP 6.0, SCEA for JEE5 (in progress)
Alexandru Popescu
Ranch Hand

Joined: Jul 12, 2004
Posts: 995
There is already a very long (but very very interesting discussion) on testing private methods.
It may answer also your question . Mines were answered .

./pope
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30751
    
156

Ali,
Yes, that is the code smell I was referring to.

Nick,
I want to clarify that I was talking about assert methods in the tests (to check test results) rather than assertions in the real code for parameter checking.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: xUnit - assertion failures