私は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
int main( int argc, char* argv[]
に引数を渡す方法を見つけてください。その後、プログラムを数回実行するスクリプトを作成します。Windowsでは、可能なスクリプトの1つは.batスクリプトです。このソリューションの場合、実装方法がわかりません。 - 解決策2:可能なすべての入力ファイルのテスト名が指定されているリストファイルを作成してから、プログラムでリストファイルを読み取ります。これは実装がはるかに簡単です。
また、Pythonはテストフレームワークに非常に簡単に組み込むことができると聞きましたが、それについてはわかりません。とにかく、私はすべての可能な解決策を受け入れています、そして感謝します!