问题
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)?
回答1:
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++.
回答2:
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.
回答3:
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.
回答4:
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.
回答5:
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.
来源:https://stackoverflow.com/questions/1116018/is-assert-and-unit-testing-incompatible