I am trying to combine basic Design by Contract (implemented by macros, which use the built-in assert function) and Google Test unit testing in an application.
So, for example, I have the following code:
AppFavorite* AppFavorites::Add(const UnicodeString& link)
{
REQUIRE(!link.IsEmpty());
...
}
Now, I can't have the following test in my unit tests, because the IDE aborts when reaching the assert (REQUIRE(!link.IsEmpty());):
TEST(AppFavoritesTest, AddEmpty)
{
AppFavorites favorites;
ASSERT_THROW(favorites.Add(L""), std::invalid_argument);
}
So, my question is should I either:
You need to decide whether passing an empty string to that function is undefined behaviour or whether it's contract guarantees that it throws an exception if passed an empty string.
It sounds like your intent is for it to be undefined behaviour, i.e. the contract says nothing about what will happen if an empty string is passed. In that case, the easiest way to test the validation of your contract is to build your unit tests for negative testing as a special build with REQUIRE
modified to throw some 'precondition violated' exception, which you would then test for with ASSERT_THROW
I highly recommend watching John Lakos' talk on Defensive Programming from ACCU 2011, it covers exactly this problem