Boost.Test 不将内存泄漏视为失败
Boost.Test doesn't treat memory leaks as failure
#define BOOST_TEST_MODULE test
#include <boost/test/unit_test.hpp>
BOOST_AUTO_TEST_CASE(test)
{
int * i = new int;
BOOST_CHECK(i==0);
}
以上测试用例明显失败。内存泄漏也被正确报告。
但是,如果我将测试从 i==0
更改为 i!=0
,它会毫无问题地成功并且不关心是否存在内存泄漏。
这是一个错误还是我错过了一些配置,使 Boost.Test 将内存泄漏视为故障?
编辑
如果内存泄漏,Boost.Test 可以很容易地展开以失败测试。对于未来的读者,我包括我的解决方案:
#define MY_TEST_CASE(name) \
void my_test_cases__##name(); \
BOOST_AUTO_TEST_CASE(name) \
{ \
mytests::heap heap_scope; \
my_test_cases__##name(); \
(void)heap_scope; \
} \
void my_test_cases__##name()
namespace mytests
{
class heap
{
# if !defined(NDEBUG) && defined(_MSC_VER)
public:
heap()
{
_CrtMemCheckpoint(&oldState);
}
~heap()
{
_CrtMemState curState, diffState;
_CrtMemCheckpoint(&curState);
int leaked_memory = _CrtMemDifference(&diffState,&oldState,&curState);
BOOST_REQUIRE(leaked_memory==0);
}
private:
_CrtMemState oldState;
# endif
};
}
那么就用MY_TEST_CASE
代替BOOST_AUTO_TEST_CASE
MY_TEST_CASE(test)
{
// various
// tests
// .
// .
// .
}
如果任何堆内存泄漏,您的单元测试将报告错误并失败。
Boost Test 是一个单元测试框架。
泄漏检测不是核心功能。
文档说:
Memory leaks detection
The Execution Monitor provides a limited ability to detect memory leaks during program execution, and to break program execution on specific memory allocation order-number (1 - first allocation of memory in program, 2 - second and so on).
Unfortunately this feature is, at the moment, implemented only for the Microsoft family of compilers (and Intel, if it employs Microsoft C Runtime Library).
Also it can not be tuned per instance of the monitor and is only triggered globally and reported after the whole program execution is done. In a future this ought to be improved.
#define BOOST_TEST_MODULE test
#include <boost/test/unit_test.hpp>
BOOST_AUTO_TEST_CASE(test)
{
int * i = new int;
BOOST_CHECK(i==0);
}
以上测试用例明显失败。内存泄漏也被正确报告。
但是,如果我将测试从 i==0
更改为 i!=0
,它会毫无问题地成功并且不关心是否存在内存泄漏。
这是一个错误还是我错过了一些配置,使 Boost.Test 将内存泄漏视为故障?
编辑
如果内存泄漏,Boost.Test 可以很容易地展开以失败测试。对于未来的读者,我包括我的解决方案:
#define MY_TEST_CASE(name) \
void my_test_cases__##name(); \
BOOST_AUTO_TEST_CASE(name) \
{ \
mytests::heap heap_scope; \
my_test_cases__##name(); \
(void)heap_scope; \
} \
void my_test_cases__##name()
namespace mytests
{
class heap
{
# if !defined(NDEBUG) && defined(_MSC_VER)
public:
heap()
{
_CrtMemCheckpoint(&oldState);
}
~heap()
{
_CrtMemState curState, diffState;
_CrtMemCheckpoint(&curState);
int leaked_memory = _CrtMemDifference(&diffState,&oldState,&curState);
BOOST_REQUIRE(leaked_memory==0);
}
private:
_CrtMemState oldState;
# endif
};
}
那么就用MY_TEST_CASE
代替BOOST_AUTO_TEST_CASE
MY_TEST_CASE(test)
{
// various
// tests
// .
// .
// .
}
如果任何堆内存泄漏,您的单元测试将报告错误并失败。
Boost Test 是一个单元测试框架。
泄漏检测不是核心功能。
文档说:
Memory leaks detection
The Execution Monitor provides a limited ability to detect memory leaks during program execution, and to break program execution on specific memory allocation order-number (1 - first allocation of memory in program, 2 - second and so on).
Unfortunately this feature is, at the moment, implemented only for the Microsoft family of compilers (and Intel, if it employs Microsoft C Runtime Library).
Also it can not be tuned per instance of the monitor and is only triggered globally and reported after the whole program execution is done. In a future this ought to be improved.