lightweight_test轻量级单元测试框架, 只支持最基本的单元测试, 不支持测试用例, 测试套件的概念, 简单小巧, 适合要求不高或者快速测试的工作.
基本用法
需要包含头文件#include <boost/core/lightweight_test.hpp>
lightweight_test库定义了五个断言宏
BOOST_TEST(e) | 断言表达式成立 |
BOOST_ERROR(s) | 直接断言失败, 输出消息s |
BOOST_TEST_EQ(e1, e2) | 断言两个表达式相等 |
BOOST_TEST_NE(e1, e2) | 断言两个表达式不等 |
BOOST_TEST_THROWS(e, ex) | 断言表达式e抛出异常ex |
// Copyright (c) 2015
// Author: Chrono Law
#include <iostream>
using namespace std;
#include <boost/smart_ptr.hpp> // shared_ptr
#include <boost/core/lightweight_test.hpp>
int main()
{
auto p = make_shared<int>(10);
BOOST_TEST(*p == 10);
BOOST_TEST(p.unique());
BOOST_TEST(p);
BOOST_TEST_EQ(p.use_count(), 1);
BOOST_TEST_NE(*p, 20);
p.reset();
BOOST_TEST(!p);
BOOST_TEST_THROWS(*p, std::runtime_error);
BOOST_ERROR("error accured!!");
return boost::report_errors(); // 输出测试报告
}
输出以下信息:
lightweight_test.cpp(23): Exception 'std::runtime_error' not thrown in function 'int main()'
lightweight_test.cpp(24): error accured!! in function 'int main()'
2 errors detected.
注意: 在测试结束时必须调用boost::report_errors()
, 否则会发生BOOST_ASSERT断言错误.
测试元编程
lightweight_test提供对元编程的有限支持, 需要包含头文件#include <boost/core/lightweight_test_trait.hpp>
// Copyright (c) 2015
// Author: Chrono Law
#include <type_traits> // is_integral<T>, is_function<T>
#include <iostream>
using namespace std;
#include <boost/core/lightweight_test.hpp>
#include <boost/core/lightweight_test_trait.hpp>
int main()
{
BOOST_TEST_TRAIT_TRUE((is_integral<int>));
BOOST_TEST_TRAIT_FALSE((is_function<int>));
return boost::report_errors();
}