What's the way to access argc and argv inside of a test case in Google Test framework?
Asked Answered
F

3

14

I’m using Google Test to test my C++ project. Some cases, however, require access to argc and argv to load the required data.

In the main() method, when initializing, argc and argv are passed to the constructor of testing.

testing::InitGoogleTest(&argc, argv);

How can I access them later in a test?

TEST(SomeClass, myTest)
{
  // Here I would need to have access to argc and argv
}
Floatation answered 10/3, 2011 at 14:18 Comment(3)
possible duplicate of How to pass parameters to the gtestPhototube
It's not answered however and the answer is posted below, so plz don't close.Floatation
What do you mean "it's not answered"? The answer is right there: "Use your favorite command-line-parsing technique, store the results in some global variable, and refer to it during your tests." Besides, that has nothing to do with whether this should be closed. It's a duplicate question. Once one is closed, the answers can be merged.Phototube
A
13

I don't know google's test framework, so there might be a better way to do this, but this should do:

//---------------------------------------------
// some_header.h
extern int my_argc;
extern char** my_argv;
// eof
//---------------------------------------------

//---------------------------------------------
// main.cpp
int my_argc;
char** my_argv;

int main(int argc, char** argv)
{    
  ::testing::InitGoogleTest(&argc, argv);
  my_argc = argc;
  my_argv = argv;
  return RUN_ALL_TESTS();
}
// eof
//---------------------------------------------

//---------------------------------------------
// test.cpp
#include "some_header.h"

TEST(SomeClass, myTest)
{
  // Here you can access my_argc and my_argv
}
// eof
//---------------------------------------------

Globals aren't pretty, but when all you have is a test framework that won't allow you to tunnel some data from main() to whatever test functions you have, they do the job.

Adjudicate answered 10/3, 2011 at 15:48 Comment(0)
C
1

If running on Windows using Visual Studio, those are available in __argc and __argv.

Conductance answered 11/3, 2011 at 13:23 Comment(0)
H
0

You may use the internal API ::testing::internal::GetArgvs() to get the input arguemnts.

class GTestMyArgsEnv : public ::testing::Environment {
 public:
  // It's safe to parse the argvs in SetUp
  void SetUp() override {
    for (const auto& s : ::testing::internal::GetArgvs()) {
      // Deal with s for your arguments parser
      static constexpr std::string_view kArgPrefix = "--my_foo=";
      if (std::string_view(s).substr(0, kArgPrefix.size()) == kArgPrefix) {
        my_foo_ = s.substr(kArgPrefix.size());
      }
    }
  }

  const auto &my_foo() const { return my_foo_; }

 private:
    std::string my_foo_;
};

// Use global var to let your TEST access the parser's result, 
// the env will be release after RUN_ALL_TESTS
static auto* env = new GTestMyArgsEnv();

// In main function, you should regiester env before RUN_ALL_TEST
GTEST_API_ int main(int argc, char** argv) {
  testing::InitGoogleTest(&argc, argv);
  testing::AddGlobalTestEnvironment(env);
  return RUN_ALL_TESTS();
}

TEST(Foo, Bar) {
  EXPECT_EQ(env->my_foo(), "baz");
}

When you run your test case. The result will depend on the arguments.

Homeward answered 1/7 at 3:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.