2

Google テストを使用するプログラムがあり、オプションを解析するためにプログラム オプション ライブラリをブーストします。問題は、Google テストにも独自のオプション パーサーがあるため、Google テストにオプションを与える前に除外する必要があることです。

たとえば、hello を実行するときは、次のように使用します

hello --option1=X --gtest_filter=Footest.*

--option1 は、 --gtest_filter オプションを Google テストに渡す前に使用するオプションです。

次のコードを実行すると--gtest_filter、ブースト プログラム オプションに使用するオプションではないため、例外が発生しました。ブースト プログラムのオプションが gtest の入力を認識しないオプションをどのように組み合わせることができますか?

#include <boost/program_options.hpp>
namespace po = boost::program_options;

#include <iostream>
#include <fstream>
#include <iterator>
using namespace std;

#include <gtest/gtest.h>   

int main(int argc, char **argv) {
    // filter out the options so that only the google related options survive
    try {
        int opt;
        string config_file;

        po::options_description generic("Generic options");
        generic.add_options()
            ("option1,o", "print version string")
            ;

            ...           
    }
    catch(exception& e) // *****************
    {
        cout << e.what() << "\n";
        return 1;
    } 

    testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}
4

2 に答える 2

2

InitGoogleTestは、Google Test が認識しているオプションを削除argvし、残りを に残します。argcもそれに応じて調整されます。への呼び出しをInitGoogleTest他のオプション解析コードの前に置くだけです。

于 2011-03-24T22:12:11.363 に答える