Passing user parameters to a test case .

6,013 views
Skip to first unread message

nishant...@gmail.com

unread,
Oct 28, 2013, 2:56:46 AM10/28/13
to googletes...@googlegroups.com
How do I pass user parameters to a test case.  

TEST(Count,Value) {
   //use argv here to get arguments. 
   //How to access argv here 
   

}


int main(int argc, char *argv[])
{
   ::testing::InitGoogleTest (argc, argv);
   
    return RUN_ALL_TESTS();

}

whem...@qiggroup.com

unread,
Nov 11, 2013, 4:57:57 PM11/11/13
to googletes...@googlegroups.com
Very simply, you can't. At least, not directly. You have created a simple test case named Count with a single test named Count.Value and those names have no relationship to argc or argv.

In general, I would recommend using the SetUp() method of a text fixture or a parametric test fixture, instead of command line arguments. The purpose of unit testing is to repeatably perform a series of tests so it is not desirable to use an external script or manual command line input to ensure that all tests are performed and are passing.

That said, you can use a test fixture to do what you describe, something like (caveat: this is not tested code, just a quick example). Note that this imposes some limitations that may become awkward as your test grow to non-trivial size (e.g., testing a complex class fully may require over a dozen test cases with scores or hundreds of tests). Note my addition to your main, in red.

class myTestClass {
private:
    //  put any instance variables you need here, to hold the results of your argc/argv processing for later use by individual tests
    //  these have to be static to be retained from test to test
    static int myArgC;   // example instance variable

public:
    static init(int argc, char *argv[]);   // process argc and argv in this method, retaining such values as your test requires, as with myArgC above
}

TEST_F(myTestClass, myFirstTest)
{
     // if your test requires 3 arguments, do this test first
     ASSERT_EQ(3, myArgC) << "Passed " << myArgC << " arguments but require exactly 3";

    // put your tests here
}

On Monday, October 28, 2013 12:56:46 AM UTC-6, nishant...@gmail.com wrote:
How do I pass user parameters to a test case.  

TEST(Count,Value) {
   //use argv here to get arguments. 
   //How to access argv here 
   

}


int main(int argc, char *argv[])
{
   ::testing::InitGoogleTest (argc, argv);
   myTestClass::init(argc, argv);
    return RUN_ALL_TESTS();

}
Reply all
Reply to author
Forward
0 new messages