c++ - 如何使用boost::unit_test 编写脚本来执行自动测试?

标签 c++ python unit-testing boost boost-test

我是 C++ 自动单元测试的新手。我按照boost::unit_test的说明,通过调用boost::unit_test中的函数unit_test_main完成了一个测试方案。我运行测试程序没问题。但是,我在将参数传递给测试函数时遇到问题。也许,以下代码可以更好地说明我的问题:

#ifndef MAIN_CPP_
#define MAIN_CPP_



#include <string>
#include <vector>
#include <iostream>
#include <assert.h>

#include <boost/program_options.hpp>
#include <boost/test/test_tools.hpp>
#include <boost/test/execution_monitor.hpp>
#include <boost/test/unit_test.hpp>




using namespace boost::program_options;
using namespace std;
using namespace boost;
using namespace boost::unit_test;




/**
* the global test suite
*/

boost::unit_test::test_suite* get_feelfree_test_suite();
boost::unit_test::test_suite* main_global_test_suite;

/**
* name of the test suite
*/
std::string current_global_test_suite_name;

#ifdef BOOST_TEST_ALTERNATIVE_INIT_API

bool  run_global_test_suite () {
    boost::unit_test::test_suite& masterTestSuite = framework::master_test_suite();

    if(masterTestSuite.size() != 0) {
        test_unit_id formerTestSuite = masterTestSuite.get(current_global_test_suite_name);
        masterTestSuite.remove(formerTestSuite);

    }
    masterTestSuite.add(main_global_test_suite);
    current_global_test_suite_name = main_global_test_suite->p_name.get();

    return true;
}
#else
    test_suite* run_global_test_suite(int, char* []) {
    return main_global_test_suite;
}
#endif

/**
* Obtain test program options
*/
int obtain_options(char **optionLine, int argc, char** argv); 





/**
* This function is used to run the test program, and the procedure is really standard.
*/
int main( int argc, char* argv[] )
{
    try 
    {
        /**
        * Step 1. obtain options
        */
        char* optionLine[1024];
        int len ;
        len = obtain_options(optionLine, argc, argv);
        /**
        * Step 2. perform unit test based on user's options
        */
        int test_status=0; 
        main_global_test_suite =   get_feelfree_test_suite();
        test_status = unit_test_main(run_global_test_suite, len, optionLine);
        return test_status;
    } 
    catch(std::exception& e)
    {
        std::cout << e.what() << std::endl;
        return 1;
    }   
    catch (const std::string& s) 
    {
        std::cout << s << std::endl;
        return 1;
    }
    catch (...)
    {
        return 1;
    }


}
/** @} */ 


int obtain_options(char **optionLine, int argc,  char* argv[])
{
    // 1. All the options for testing the program
        options_description desc("Allowed options");
        desc.add_options()("help", "produce help message")
        ("detect_memory_leaks", value<bool>()->default_value(false), "test configuration option (option of boost framework)");
        // 2. Perform parsing 
        variables_map vm;
        store(parse_command_line(argc, argv, desc), vm);
        notify(vm);
        // 3. Illustrate the input 
        std::vector<const char*> options;
        std::string testSuiteToRun;
        if(vm.count("test_suite")){  
            testSuiteToRun = vm["test_suite"].as<string>(); 
        }
        else {
            testSuiteToRun = "main";
        }   

        options.push_back(argv[0]);
        if(vm.count("detect_memory_leaks")) {  
            bool detect = vm["detect_memory_leaks"].as<bool>();
            if(detect) {
                options.push_back("--detect_memory_leaks=1");
            }
            else {
            options.push_back("--detect_memory_leaks=0");
            }
        }
        else {
            options.push_back("--detect_memory_leaks=0");
        }

        // 4. Obtain all the parameters in the format of char** 

        assert(options.size() < 1024);
        std::copy(options.begin(), options.end(), const_cast<const char**>(optionLine));

        return options.size();

}

void Testsub(const std::string &name)
{
    cout<<"File_name: "<<name<<endl;
}
void Testabc( )
{
    std::vector<std::string > name_array;
    name_array.push_back("name 1");
    name_array.push_back("name 2");
    for(int i=0; i<name_array.size(); i++)
        Testsub(name_array[i]);
}


boost::unit_test::test_suite* get_feelfree_test_suite()
{
    test_suite* ts = BOOST_TEST_SUITE( "unit_geometric" );
    ts->add( BOOST_TEST_CASE(&Testabc) ); 
    return ts;
}


#endif

如您所见,在这个测试框架中,我要测试的主要函数是Testsub,它依赖于输入参数const std::string &name。但是,我无法通过测试套件函数 get_feelfree_test_suite 传递任何参数。因此,在这个测试程序中,我写了另一个测试函数Testabc,其中给出了所有可能的文件测试列表,并传递给Testsub。这绝对不是最好的解决方案。我想知道是否还有其他解决方案。脑子里有几个方案,不知道是不是好的方案:

  • 解决方案 1:尝试找出一种将参数传递给的方法 get_feelfree_test_suite 来自 main 函数 ( int main( int argc, char* argv[] )。之后,编写脚本来运行程序 几次。在 Windows 中,一种可能的脚本是 .bat 脚本。为了 这个解决方案,我不知道如何实现。
  • 解决方案 2:编写一个列表文件,其中包含所有可能的输入 给出文件测试名称,然后读取列表文件中的 程序。这更容易实现。

我还听说 Python 可以很容易地集成到测试框架中,但我对此一无所知。无论如何,我对所有可能的解决方案持开放态度,谢谢!

最佳答案

您真的需要在单独的文件中使用不同的“名称”吗? 将它们放入您的测试套件中可能会更简单。每个名字一个 BOOST_AUTO_TEST_CASE。或者名称数组,您可以在测试用例中对其进行迭代。

关于c++ - 如何使用boost::unit_test 编写脚本来执行自动测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12731079/

相关文章:

python - 如何使用 pyparsing 解析缩进和缩进?

python - 使用请求和 psycopg2 在 Postgres 中创建/插入 Json

java - Java EE 环境中的单元测试

javascript - 如何通过 Jest 来兑现这个 promise ?

c++ - operator << argument-dependent lookup 不在全局命名空间中查找

c++ - 以参数为类指针的虚函数

c++ - CUDA 内核中的竞争条件

python - 你能像在 Pytorch 中那样将变量传递给 Python 中的数据成员吗?

C# Windows 窗体单元测试

c++ - 如何从函数地址或类中找到 CFBundleRef