Search code examples
c++unit-testingunittest++

UnitTest++ command line arguments


I want to use a command line argument in one of my tests. I couldn't find any example of this on the web.

TEST(SomeTest)
{
    std::string file("this is some command line argument");
    CHECK(something);
}

int main(int argc, char** argv)
{
    return UnitTest::RunAllTests();
}

Any ideas?


Solution

  • Answer

    There's no real reason to test command-line arguments directly. Instead, write your unit tests to check the behavior of your code (functions and classes) given different arguments. Once you are satisfied that your code is working properly under unit test, simply plug it into main and it should work properly there, as well.

    Clarification

    Imagine that you have your unit test on the argument to the std::string constructor.

    TEST(SomeTest)
    {
        std::string file("this is some command line argument");
        CHECK(something);
    }
    

    Then you plug it into main.

    int main(int argc, char** argv)
    {
        std::string file(argv[1]);
    
        // do stuff....
        
        return 0;
    }
    

    Because nothing should happen to the command-line argument before it is passed to the constructor, you have effectively tested it already. If, on the other hand, your main is a mess, I would suggest refactoring that first.