Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

xUnit - assertion failures

 
author & internet detective
Posts: 41184
848
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 8944
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why would it be useful to run an assertion when the previous one failed ? Is the second one independent of the first?
 
Ranch Hand
Posts: 995
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Jeanne Boyarsky
author & internet detective
Posts: 41184
848
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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
Posts: 995
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wouldn't it be the smell of at least 2 tests? (i feel i'm loosing it)

./pope
 
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Alexandru Popescu
Ranch Hand
Posts: 995
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 41184
848
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic