【问题标题】:GoogleTest: main() in different lib -> test cases not foundGoogleTest:不同库中的 main() -> 未找到测试用例
【发布时间】:2018-07-17 15:19:41
【问题描述】:

我的 VS 解决方案中有各种可执行项目,其中包含各种 GoogleTest 案例。我试图通过一个单独的.lib 项目来减少代码,该项目只包含main() 函数和GoogleTest 的初始化代码。然后我可以将它链接到所有包含实际测试的 exe 项目中:

所以:

---main.cpp in static lib project TESTMAIN---

#include <gtest/gtest.h>

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

---accounttest.cpp in exe project TESTACCOUNT

#include <gtest/gtest.h>

struct BankAccount
{
    int m_iBalance;

    BankAccount(){}
};

TEST(AccountTests, BankAccountStartsEmpty)
{
    BankAccount account;
    EXPECT_EQ(0, account.m_iBalance);
}

但是,当运行 TESTACCOUNT.exe 时,单元测试没有被拾取:

Running main() from gmock_main.cc
[==========] Running 0 tests from 0 test cases.
[==========] 0 tests from 0 test cases ran. (0 ms total)
[  PASSED  ] 0 tests.

在我的 exe 项目中显式添加 main()(带有 gtest 初始化代码)时,它确实有效。我的预感是它与链接器有关,但我不确定为什么这不起作用。我仍然想避免在我的所有测试项目中添加 main+init..

我们将不胜感激任何有关执行此操作的好方法的建议。

【问题讨论】:

  • 不是您问题的答案,但您是否尝试过链接 gtest-main 而不是您的 lib?
  • gtest 提供了一个gtest_main,这正是您正在做的事情(改用它)。当您使用TEST 宏时,它会在静态初始化时间通过this function 注册您的测试。我无法从您所写的内容中看出,但在我看来,您正在获得 2 个测试注册商 - 通常使用动态库获得多个版本的全局变量并不难,但我不知道您的设置如何实现。
  • 啊,这可能是个好主意。我试试看,谢谢大家

标签: c++ visual-c++ googletest


【解决方案1】:

正如@David 和@Michal Walenciak 所指出的,它通过链接到 gtest_main 来工作。最后,我认为我不需要链接到静态库,而是链接到 DLL。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-12
    • 2014-08-09
    • 2019-07-23
    • 1970-01-01
    相关资源
    最近更新 更多