【问题标题】:How to setup googletest on Linux in the year 2012?如何在 2012 年在 Linux 上设置 googletest?
【发布时间】:2012-01-05 08:17:39
【问题描述】:

我正在使用 Linux 机器。 我已经从here下载了googletest包

但是,没有安装指南或其他有关如何正确设置的博客 README文件不好,看不懂在说什么?

谁能提供一个简单的示例,说明如何使用该 gtest 包测试 .cc 文件中的简单函数?

【问题讨论】:

    标签: c++ linux unit-testing googletest


    【解决方案1】:

    这是我所做的,您可以根据需要进行调整。我在我的 Linux 机器上将 gtest-1.6.0.zip(来自releases 页面)下载到 ~/Downloads 中,完全输入的是 /home/me/Downloads/

    将gtest-1.6.0.zip的内容解压到~/Downloads/gtest-1.6.0/

    cd /home/me/Downloads
    unzip gtest-1.6.0.zip
    

    构建 gtest 库,因为您需要将其“包含”在您的测试可执行文件中。 编译目标文件gtest-all.o:

    g++ -Igtest-1.6.0/include -Igtest-1.6.0 -c gtest-1.6.0/src/gtest-all.cc
    

    然后构建库归档 libgtest.a:

    ar -rv libgtest.a gtest-all.o
    

    现在您可以在 ~/Downloads 中创建 test.cc 文件。这是我用来确保它编译的示例测试文件。

    #include "gtest/gtest.h"
    
    TEST(blahTest, blah1) {
        EXPECT_EQ(1, 1);
    }
    
    int main (int argc, char** argv) {
        ::testing::InitGoogleTest(&argc, argv);
    
        int returnValue;
    
        //Do whatever setup here you will need for your tests here
        //
        //
    
        returnValue =  RUN_ALL_TESTS();
    
        //Do Your teardown here if required
        //
        //
    
        return returnValue;
    }
    

    编译您自己的测试并运行它:

    g++ -I/home/me/Downloads/gtest-1.6.0/include -pthread test.cc libgtest.a -o test_executable
    

    然后执行它:

    ./test_executable
    

    它应该运行良好。从那里根据需要进行修改。

    【讨论】:

    • 请注意,使用 gtest-1.6.0/src/gtest-all.cc 构建库需要您提供一个 main 方法。如果您想完全避免这种情况并使用 main 方法的默认实现,请改用 gtest_main.cc 构建您的库。
    【解决方案2】:

    这些说明使测试框架适用于 Debug 配置。

    获取 Google C++ 测试框架

    1.下载最新的gtest框架

    2.解压到C:\gtest

    构建框架库

    1.在Visual Studio中打开C:\gtest\msvc\gtest.sln

    2.将配置设置为“调试”

    3.构建解决方案

    创建和配置您的测试项目

    1.新建解决方案,选择模板Visual C++ > Win32 > Win32 Console Application

    2.右键新建项目,选择属性

    3.将配置更改为调试。

    4.Configuration Properties > C/C++ > General > Additional Include Directories: 添加C:\gtest\include

    5.Configuration Properties > C/C++ > Code Generation > Runtime Library:如果您的代码链接到运行时 DLL,请选择 Multi-threaded Debug DLL (/MDd)。如果没有,请选择多线程调试 (/MTd)。

    6.Configuration Properties > Linker > General > Additional Library Directories: 添加C:\gtest\msvc\gtest\Debug

    7.配置属性>链接器>输入>附加依赖:添加gtestd.lib

    验证一切正常

    1.在包含main() 函数的测试项目中打开cpp。

    2.粘贴以下代码:

    #include "stdafx.h"
    #include <iostream>
    
    #include "gtest/gtest.h"
    
        TEST(sample_test_case, sample_test)
        {
            EXPECT_EQ(1, 1);
        }
    
        int main(int argc, char** argv) 
        { 
            testing::InitGoogleTest(&argc, argv); 
            RUN_ALL_TESTS(); 
            std::getchar(); // keep console window open until Return keystroke
        }
    

    1.Debug > 开始调试

    如果这可行,您应该会看到控制台窗口打开并显示您的测试结果。

    【讨论】:

    • 还是谢谢了,请问你们有linux环境的解决方案吗?
    • 哦,我很抱歉,没看到 Linux。
    • 我需要windows设置的步骤。谢谢你。
    • 对不起,这个答案不是取自这个吗? stackoverflow.com/questions/531941/…。如果是,我会提到原始答案的参考
    【解决方案3】:

    James C's answer 的附录:

    请注意,使用 gtest-1.6.0/src/gtest-all.cc 构建库需要您自己提供一个 main 方法。如果您想完全避免这种情况并使用 Googletest 提供的 main 方法的默认实现,请构建您的库,包括 gtest_main.cc

    即:

    g++ -Igtest-1.6.0/include -Igtest-1.6.0 -c gtest-1.6.0/src/gtest-all.cc gtest-1.6.0/src/gtest_main.cc
                                                                                           ^^^^^^^^^^^^^^
    ar -rv libgtest_main.a gtest_main.o gtest-all.o
                           ^^^^^^^^^^^^
    

    另外,请记住,实现自己的 main 方法不是定义SetUpTearDown 行为的推荐方法;您应该改用固定装置。检查Googletest documentation on the topic

    【讨论】:

      猜你喜欢
      • 2012-11-10
      • 2014-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-31
      相关资源
      最近更新 更多