views:

481

answers:

6

Hello,

I have some concerns related to the fact of testing some functions containing the assert macro from assert.h.

If the assert fails the test fails also. This leaves me with some test cases that will never work.

For example a function instead of indicating failure (return false or something similar) asserts.

Is there a solution for this (unit-testing functions containing assert)?

Thanks,

Iulian

A: 

You should be using a unit testing framework.

Nick D
This doesn't answer the original question at all
Tom
+6  A: 

No, unit testing is what you do during development. Asserts are a run-time construct.

In my experience, most of the time asserts are turned off in production. But you should always be testing.

CppUnit is a fine test framework. It's part of the nUnit family for C++.

duffymo
+8  A: 

Maybe it's just me, but I would think that if you have assertion failures, you shouldn't even be thinking about higher-level unit testing until you get them fixed. The idea is that assertions should never fail under any circumstances, including unit tests, if the code is written properly. Or at least that's how I write my code.

David Zaslavsky
Assertions could exist to validate input parameters, though. A valid unit test is "this function blows up in a predictable manner with certain classes of bad input".
Tom
+1  A: 

It basically sounds like your test framework wasn't built to test your assertions.

With an assert that will halt the process, you need something that will monitor your execution state.

Example of how boost-test does this: http://www.boost.org/doc/libs/1_34_0/libs/test/doc/components/prg_exec_monitor/index.html

I've not done C or C++ coding in some time, but, I'd start with a similar technique.

Tristan Juricek
+4  A: 

You could be testing the fact that that assert aborts when you expect it to (on bad input).

The test framework Google Test as an ASSERT_DEATH macro that will test that the program aborts where you expect it to (like an assert).

You could also compile with NDEBUG defined ( -DNDEBUG with gcc) to disable assertions for your unit tests.

crmoore
+1  A: 

Assertions should never fail under any circumstance. If they fail in your tests, it indicates a logical error. Basically, if your function is doing "assert( 0 )" instead of returning an error code, then the function should be re-written. If aborting is the desired behavior, then exit() is appropriate, but not assert().

If an assertion ever fails during your tests, then the code is in error and must be changed. The code "assert( x )" should be interpreted to mean "The logic of the program requires that x be true. Under no circumstance can it be false." If you have a unit test that causes an assertion to fail, then the statement is clearly invalid and must be modified.

William Pursell
If aborting is the desired behavior, maybe you should use `abort()`, but certainly not `exit()`.
Tom
That is a good point. Perhaps I should have said "if terminating is the desired behavior". "Abort" has a technical meaning that I was not intending to use.
William Pursell