我的测试代码中有很多地方的代码:
//
// Make a function call while expecting an exception should be thrown
//
bool exceptionThrown = false;
try
{
expectNotEqual(someData, anotherData, methodName);
}
catch(std::logic_error&)
{
exceptionThrown = true;
}
if(!exceptionThrown)
throw std::logic_error(methodName+"exception not thrown");
如果我可以封装所有内容并做类似的事情,那将会更好(更具可读性和简洁性):
exceptionShouldBeThrown(expectNotEqual(someData, anotherData, methodName));
我不想使用宏...有谁知道如何用C ++实现上面的单行程?
答案 0 :(得分:11)
我知道你说没有宏,但为什么?它们用于生成代码:
#define SHOULD_THROW(x, name) \
{ \
bool didThrow = false; \
try \
{ \
x; \
} \
catch(...) { didThrow = true; } \
\
if (!didThrow) \
throw std::logic_error(name " did not throw."); \
}
SHOULD_THROW(expectNotEqual(someData, anotherData), "expectNotEqual")
如果你真的不想使用宏,你需要创建一个函子来调用:
template <typename Func>
void should_throw(Func pFunc, const std::string& pName)
{
bool didThrow = false;
try
{
pFunc();
}
catch (...)
{
didThrow = true;
}
if (!didThrow)
throw std::logic_error(pName + " did not throw.");
}
Boost Bind在这里有所帮助:
should_throw(boost::bind(expectNotEqual, someData, anotherData),
"expectNotEqual");
当然,任何使编程器都可以工作的东西,比如lambda等。但是如果Boost可用,只需使用他们的testing library:
#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>
BOOST_AUTO_TEST_CASE(test)
{
BOOST_CHECK_THROW(expectNotEqual(someData, anotherData) , std::logic_error);
}
答案 1 :(得分:2)
例外情况适用于特殊情况。也就是说,在运行时你不会想到的东西,例如,内存不足错误。您不希望在运行时使用异常来测试常见事物。只要expectNotEqual在成功时返回true / false:
if (expectNotEqual(someData, anotherData, methodName))
{
//handle success
}
else
{
//handle failure (which would include someData==anotherData)
}