努力使Boost库单元测试框架正常工作 [英] Struggling getting Boost library unit test framework to work

查看:61
本文介绍了努力使Boost库单元测试框架正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用VS2013成功构建了Boost库.我还成功地包含并使用了一些Boost.Filesystem和Boost.Log的东西.但是,我正在努力使以下单元测试在Visual Studio 2013中工作:

I've successfully built the Boost libraries using VS2013. I've also successfully included and used some of Boost.Filesystem and Boost.Log stuff. However, I'm struggling to get the following unit test to work in Visual Studio 2013:

#define BOOST_TEST_MODULE MyTest
#include <boost/test/included/unit_test.hpp>

class Multiplier {
public:
    explicit Multiplier(int i) : _value{ i } {}
    int multiply(int i) { return _value * i; }
private:
    int _value;
};

BOOST_AUTO_TEST_CASE(everything_test) {
    Multiplier m{ 5 };
    BOOST_CHECK_EQUAL(m.multiply(2), 10);
}

这显然不是真正的单元测试,但这不是重点...:)

This obviously isn't a real unit test, but that's not the point... :)

通过包含boost/test/included/unit_text.hpp,我应该得到一个为我提供的main()函数,并且看来我是因为我能够解决该错误.但是,当我运行生成的可执行文件时,会得到一个Access violation reading location 0x00000000.

By including boost/test/included/unit_text.hpp, I should be getting a main() function supplied for me, and it appears I am since I was able to get that error resolved. When I run my resulting executable, though, I get an Access violation reading location 0x00000000.

我应该运行可执行文件来运行测试吗?如果没有,我该如何运行它们?运行测试似乎是一个简单的操作,从文档中可以明显看出这一点,但是我必须错过它.

Am I supposed to run the executable to run the tests? If not, how do I run them? Running the tests seems like such a simple operation that would be obviously evident from the documentation, but I must be missing it.

推荐答案

首先请确保您的makefile中具有链接信息;例子:

First make sure that you have linkage info in your makefile; example :

-lboost_system -lboost_log -lboost_signals -lboost_thread -lboost_filesystem -lboost_regex 

当然只添加特定测试用例套件所需的功能

of course only add the boost which is needed for your specific testcase suite

然后,您的测试套件文件中将包含以下内容:

Then in your test suite file have following:

#define BOOST_TEST_MODULE YourTestSuiteName                  
#define BOOST_TEST_DYN_LINK                            
#include <boost/test/unit_test.hpp>      
#include <boost/test/results_reporter.hpp>             
#define BOOST_AUTO_TEST_MAIN 

#ifndef NOTESTRESULTFILE                                                           
#ifdef BOOST_AUTO_TEST_MAIN                                                        
std::ofstream out;                                                                 

struct ReportRedirector                                                            
{                                                                                  
    ReportRedirector()                                                             
    {                                                                              
        out.open("test_results.xml");                                              
        assert( out.is_open() );                                                   
        boost::unit_test::results_reporter::set_stream(out);                       
    }                                                                              
};                                                                                 

BOOST_GLOBAL_FIXTURE(ReportRedirector)                                             
#endif                                                                             
#endif   

BOOST_AUTO_TEST_SUITE (YourTestSuiteName)            
BOOST_AUTO_TEST_SUITE_END( )                                            

BOOST_AUTO_TEST_CASE(YourTestCaseName)                                
{                                                                             
   cout<<"BOOST_AUTO_TEST_CASE( YourTestCaseName )\n{"<<endl;         

   BOOST_CHECK(false == true);  //TODO: testcase not finished  

   cout<<"}"<<endl;                  
}                                     

此设置对我来说很好,但是我敢肯定,您可以通过其他方式设置增强单元测试套件

This setup works fine for me, but I am sure you can setup boost unit tests suites in a different way

这篇关于努力使Boost库单元测试框架正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆