Why would I use an assert_not_raised in test code? - unit-testing

Asserts raise an error in test code alerting that a test failed. If I am expecting a test to pass and not raise an exception, why would I use assert_not_raised (or equivalent) instead of just letting the raised exception fail the test?
I suppose it may be more explicit for others reading the test code, i.e. that the thing being tested could raise an exception, but still seems unnecessary.

Related

Test an aborting assertion failure in CppUnit

I want to unit test a C++ function which throws aborting assertion error on invalid input.
The function is as follows:
uint64_t FooBar::ReadTimeStamp(std::string& name) {
auto iter = hash_table_.find(name);
assert(iter != hash_table_.end());
....
}
In unit test, I use CPPUNIT_ASSERT_ASSERTION_FAIL to assert on assertion failure:
void FooBarTest::
TestReadNonexistentTimestamp() {
CPPUNIT_ASSERT_ASSERTION_FAIL(ReadTimestamp("NON_EXISTENT"));
}
But I got abort message and unit test failed.
I read this page. It's not clear to me if I need to throw exception here and what the correct way to unit test this scenario would be. Thanks!
Firstly, your misunderstanding is caused by the different ways to use the term "assertion". The test framework itself talks about assertions, but it does not mean the assert() macro provided by the standardlibrary. Since the standard assertion failure causes program termination, you get those results.
Now, how to fix that:
Don't use assert(). Instead, you could throw an exception.
Don't test this code path. Since this is a programming error that's not recoverable anyway, it can only be caused by misuse of your code (i.e. violating preconditions). Since it's not your code that's at fault, not testing it doesn't have any negative impact on its quality.
Hijack assert() to throw a failure that CppUnit understands. This could be tricky because for one, assert() is part of C++ and shouldn't be redefined carelessly (perhaps substituting it with a different macro would be better). Further, you now have three different behaviours: Throw in tests, abort() in regular use, UB with NDEBUG defined.
Which of these works best is up to you to decide, based on your use case and, admittedly, personal preference.

How do I make gmock treat all warnings/failures as errors?

I have some test code written using gmock. Due to some code changes, the test is not executing completely, and completes prematurely (I know this, as I can see failure messages in the logs saying expected to execute once, but did not run for many functions). However, the compilation/execution is not failing, as it getting an exception that it is expecting (as the same exception is thrown in multiple places). So the test appears to pass, but it is not executing completely. How can I make gmock treat all warnings/failures as errors?
Using
::testing::GTEST_FLAG(throw_on_failure) = true
in the method where the tests were failing helped catch these failures while running the tests. The throw_on_failure flag causes GMock to throw an exception when a mock related exception fails.
Reference

Using gtest to check correct initialization values of constructor

I'm new to cpp and gtest.
I want to use gtest to check my cpp Board class with invalid
Arguments, for example: negative number of rows, to check that the invalid Arguments exception
Is thrown, what will be the best way to do that?
Thought about try and catch bloc that catches the invalid
Arguments exception, and if it is caught then the tests should pass, but I couldnt find no pass macro for gtest.
Gtest has macros for that purpose, eg
ASSERT_ANY_THROW(statement);
asserts that statement throws (any) exception. See here for more options (eg. asserting on only specific exceptions).

How to assert in cppunit that a statement throws an exception either of type Excp1 or Excp2?

CPPUNIT_ASSERT_THROW(Expression, ExceptionType) does not seem to allow checking for exceptions of multiple types i.e. for a statement that can throw more than one kind of exceptions.
For e.x. an expression may throw Excp1 on one platform, or Excp2 on another platform. Is there a workaround to test such statements using CPPUNIT_ASSERT_THROW?
First test, you make your test conditions such that it throws exeption 1.
If it fails to throw, that is a test failure.
if it does throw, you catch it as an exception 1, and accept it as passing.
If it throws something else, the framework catches it.
Second test, you make using conditional compilation to enable code for platform 2 only. You make your test conditions such that it throws exception 2.
IF it fails to throw, that is a test failure.
If it does throw, you catch it as exception 2, and accept it as passing.
If it throws something else, the framework catches it.
On the first platform the test simply passes, as there is nothing for it to do.
On the second platform you catch exception 2 as expected.
There is no direct support for this feature in cppunit but you have basically two solutions how you can implement it easily in your code.
So the basic idea behind this assert is the following code:
bool expected_exception_thrown = false;
try
{
yourExpression();
}
catch(const ExpectedException&)
{
expected_exception_thrown = true;
}
catch(...)
{
}
if (!expected_exception_thrown)
CPPUNIT_FAIL();
Of course the actual implementation is a bit fancier and involves some additional features (like better messages for unexpected std::exception and the missing support for an error message) but the general idea is the same.
So now you can easily extend that pattern to support as many exceptions as you need. You can have a look at the existing implementation in include/cppunit/TestAssert.h and either use that implementation and extend it or use the simplified one that I posted above.

Testing for assert in the Boost Test framework

I use the Boost Test framework to unit test my C++ code and wondered if it is possible to test if a function will assert? Yes, sounds a bit strange but bear with me! Many of my functions check the input parameters upon entry, asserting if they are invalid, and it would be useful to test for this. For example:
void MyFunction(int param)
{
assert(param > 0); // param cannot be less than 1
...
}
I would like to be able to do something like this:
BOOST_CHECK_ASSERT(MyFunction(0), true);
BOOST_CHECK_ASSERT(MyFunction(-1), true);
BOOST_CHECK_ASSERT(MyFunction(1), false);
...
You can check for exceptions being thrown using Boost Test so I wondered if there was some assert magic too...
Having the same problem, I digged through the documentation (and code) and
found a "solution".
The Boost UTF uses boost::execution_monitor (in
<boost/test/execution_monitor.hpp>). This is designed with the aim to catch
everything that could happen during test execution. When an assert is found
execution_monitor intercepts it and throws boost::execution_exception. Thus,
by using BOOST_REQUIRE_THROW you may assert the failure of an assert.
so:
#include <boost/test/unit_test.hpp>
#include <boost/test/execution_monitor.hpp> // for execution_exception
BOOST_AUTO_TEST_CASE(case_1)
{
BOOST_REQUIRE_THROW(function_w_failing_assert(),
boost::execution_exception);
}
Should do the trick. (It works for me.)
However (or disclaimers):
It works for me. That is, on Windows XP, MSVC 7.1, boost 1.41.0. It might
be unsuitable or broken on your setup.
It might not be the intention of the author of Boost Test.
(although it seem to be the purpose of execution_monitor).
It will treat every form of fatal error the same way. I e it could be
that something other than your assert is failing. In this case you
could miss e g a memory corruption bug, and/or miss a failed failed assert.
It might break on future boost versions.
I expect it would fail if run in Release config, since the assert will be
disabled and the code that the assert was set to prevent will
run. Resulting in very undefined behavior.
If, in Release config for msvc, some assert-like or other fatal error
would occur anyway it would not be caught. (see execution_monitor docs).
If you use assert or not is up to you. I like them.
See:
http://www.boost.org/doc/libs/1_41_0/libs/test/doc/html/execution-monitor/reference.html#boost.execution_exception
the execution-monitor user-guide.
Also, thanks to Gennadiy Rozental (Author of Boost Test), if you happen to
read this, Great Work!!
There are two kinds of errors I like to check for: invariants and run-time errors.
Invariants are things that should always be true, no matter what. For those, I use asserts. Things like you shouldn't be passing me a zero pointer for the output buffer you're giving me. That's a bug in the code, plain and simple. In a debug build, it will assert and give me a chance to correct it. In a retail build, it will cause an access violation and generate a minidump (Windows, at least in my code) or a coredump (Mac/unix). There's no catch that I can do that makes sense to deal with dereferencing a zero pointer. On Windows catch (...) can suppress access violations and give the user a false sense of confidence that things are OK when they've already gone horribly, horribly wrong.
This is one reason why I've come to believe that catch (...) is generally a code smell in C++ and the only reasonable place where I can think of that being present is in main (or WinMain) right before you generate a core dump and politely exit the app.
Run-time errors are things like "I can't write this file because of permissions" or "I can't write this file because the disk is full". For these sorts of errors throwing an exception makes sense because the user can do something about it like change the permission on a directory, delete some files or choose an alternate location to save the file. These run-time errors are correctable by the user. A violation of an invariant can't be corrected by the user, only by a programmer. (Sometimes the two are the same, but typically they aren't.)
Your unit tests should force code to throw the run-time error exceptions that your code could generate. You might also want to force exceptions from your collaborators to ensure that your system under test is exception safe.
However, I don't believe there is value in trying to force your code to assert against invariants with unit tests.
I don't think so. You could always write your own assert which throws an exception and then use BOOST_CHECK_NOTHROW() for that exception.
I think this question, and some of replies, confuse run-time errors detection with bug detection. They also confuse intent and mechanism.
Run-time error is something that can happen in a 100% correct program. It need detection, and it needs proper reporting and handling, and it should be tested. Bugs also happen, and for programmer's convenience it's better to catch them early using precondition checks or invariant checks or random assert. But this is programmer's tool. The error message will make no sense for ordinary user, and it does not seem reasonable to test function behaviour on the data that properly written program will never pass to it.
As for intent and mechanism, it should be noted that exception is nothing magic. Some time ago, Peter Dimov said on Boost mailing list (approximately) that "exceptions are just non-local jump mechanism". And this is very true. If you have application where it's possible to continue after some internal error, without the risk that something will be corrupted before repair, you can implement custom assert that throws C++ exception. But it would not change the intent, and won't make testing for asserts much more reasonable.
At work I ran into the same problem. My solution is to use a compile flag. When my flag GROKUS_TESTABLE is on my GROKUS_ASSERT is turned into an exception and with Boost you can test code paths that throw exceptions. When GROKUS_TESTABLE is off, GROKUS_ASSERT is translated to c++ assert().
#if GROKUS_TESTABLE
#define GROKUS_ASSERT ... // exception
#define GROKUS_CHECK_THROW BOOST_CHECK_THROW
#else
#define GROKUS_ASSERT ... // assert
#define GROKUS_CHECK_THROW(statement, exception) {} // no-op
#endif
My original motivation was to aid debugging, i.e. assert() can be debugged quickly and exceptions often are harder to debug in gdb. My compile flag seems to balance debuggability and testability pretty well.
Hope this helps
Sorry, but you're attacking your problem the wrong way.
"assert" is the spawn of the devil (a.k.a. "C") and is useless with any language that has proper exceptions. It's waaaaaay better to reimplement an assert-like functionality with exceptions. This way you actually get a chance of handling errors the right way (incl proper cleanup procedures) or triggering them at will (for unit testing).
Besides, if your code ever runs in Windows, when you fail an assertion you get a useless popup offering you to debug/abort/retry. Nice for automated unit tests.
So do yourself a favor and re-code an assert function that throws exceptions. There's one here:
How can I assert() without using abort()?
Wrap it in a macro so you get _ _FILE _ _ and _ _ LINE _ _ (useful for debug) and you're done.