在 Google 测试框架中的测试用例中访问 argc 和 argv 的方法是什么?

发布于 2024-10-21 09:03:25 字数 332 浏览 5 评论 0原文

我正在使用 Google Test 来测试我的 C++ 项目。然而,某些情况下需要访问 argc 和 argv 来加载所需的数据。

main()方法中,初始化时,argc和argv被传递给测试的构造函数。

testing::InitGoogleTest(&argc, argv);

我如何在稍后的测试中访问它们?

TEST(SomeClass, myTest)
{
  // Here I would need to have access to argc and argv
}

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
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

笑,眼淚并存 2024-10-28 09:03:25

我不知道谷歌的测试框架,所以可能有更好的方法来做到这一点,但这应该可以:

//---------------------------------------------
// 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
//---------------------------------------------

全局变量并不漂亮,但是当你拥有的只是一个不允许你传输某些数据的测试框架时从 main() 到您拥有的任何测试函数,它们都能完成工作。

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.

若沐 2024-10-28 09:03:25

如果使用 Visual Studio 在 Windows 上运行,则它们在 __argc 和 __argv 中可用。

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

小嗷兮 2024-10-28 09:03:25

您可以使用内部 API ::testing::internal::GetArgvs() 来获取输入 argumentmnts。

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");
}

当您运行测试用例时。结果将取决于论点。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文