简体   繁体   English

如何在2012年在Linux上设置googletest?

[英]How to setup googletest on Linux in the year 2012?

I am using Linux machine. 我正在使用Linux机器。 I have download the googletest package from here 我从这里下载了googletest软件包

However, there is no installation guide or other blogs related on how to set it up properly The README file is no good that I can't understand what it is talking about? 但是,没有关于如何正确设置的安装指南或其他博客自述文件是不行的,我无法理解它在说什么?

Can anyone provide a simple example on how to test a simple function inside a .cc file with that gtest package? 任何人都可以提供一个简单的例子来说明如何使用该gtest包测试.cc文件中的简单函数吗?

Here's what I did and you can adjust as necessary. 这就是我所做的,你可以根据需要进行调整。 I downloaded gtest-1.6.0.zip (from the releases page) on my Linux box into ~/Downloads which typed out fully is /home/me/Downloads/ 我在我的Linux机器上下载了gtest-1.6.0.zip(从发布页面)到〜/下载完全输出的是/ home / me / Downloads /

Unzip the contents of gtest-1.6.0.zip into ~/Downloads/gtest-1.6.0/ 将gtest-1.6.0.zip的内容解压缩到〜/ Downloads / gtest-1.6.0 /

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

Build the gtest library because it's something you need to "include" in your test executable. 构建gtest库,因为它需要在测试可执行文件中“包含”。 Compile the object file gtest-all.o: 编译目标文件gtest-all.o:

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

Then build the library archive libgtest.a: 然后构建库存档libgtest.a:

ar -rv libgtest.a gtest-all.o

Now you can create your test.cc file in ~/Downloads. 现在,您可以在〜/ Downloads中创建test.cc文件。 Here is an example test file that I used to make sure it compiles. 这是我用来确保编译的示例测试文件。

#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;
}

To compile your own test and run it: 编译自己的测试并运行它:

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

Then to execute it: 然后执行它:

./test_executable

And it should run fine. 它应该运行正常。 Modify as necessary from there. 根据需要进行修改。

These instructions get the testing framework working for the Debug configuration. 这些指令使测试框架适用于Debug配置。

Get Google C++ Testing Framework 获取Google C ++测试框架

1.Download the latest gtest framework 1.下载最新的gtest框架

2.Unzip to C:\\gtest 2.解压缩到C:\\gtest

Build the Framework Libraries 构建框架库

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

2.Set Configuration to "Debug" 2.将配置设置为“调试”

3.Build Solution 3.Build解决方案

Create and Configure Your Test Project 创建和配置您的测试项目

1.Create a new solution and choose the template Visual C++ > Win32 > Win32 Console Application 1.创建一个新的解决方案,然后选择模板Visual C ++> Win32> Win32控制台应用程序

2.Right click the newly created project and choose Properties 2.右键单击新创建的项目,然后选择“属性”

3.Change Configuration to Debug. 3.更改配置到调试。

4.Configuration Properties > C/C++ > General > Additional Include Directories: Add C:\\gtest\\include 4.配置属性> C / C ++>常规>其他包含目录:添加C:\\gtest\\include

5.Configuration Properties > C/C++ > Code Generation > Runtime Library: If your code links to a runtime DLL, choose Multi-threaded Debug DLL (/MDd). 5.配置属性> C / C ++>代码生成>运行时库:如果您的代码链接到运行时DLL,请选择多线程调试DLL(/ MDd)。 If not, choose Multi-threaded Debug (/MTd). 如果没有,请选择多线程调试(/ MTd)。

6.Configuration Properties > Linker > General > Additional Library Directories: Add C:\\gtest\\msvc\\gtest\\Debug 6.配置属性>链接器>常规>其他库目录:添加C:\\gtest\\msvc\\gtest\\Debug

7.Configuration Properties > Linker > Input > Additional Dependencies: Add gtestd.lib 7.配置属性>链接器>输入>附加依赖项:添加gtestd.lib

Verifying Everything Works 验证一切正常

1.Open the cpp in your Test Project containing the main() function. 1.在包含main()函数的Test Project中打开cpp。

2.Paste the following code: 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 > Start Debugging 1.Debug>开始调试

If this works you should see the console window open with your test results. 如果这样做,您应该看到控制台窗口打开您的测试结果。

An addendum to James C's answer : 詹姆斯C答案的附录:

Note that building the library using gtest-1.6.0/src/gtest-all.cc will require you to provide with a main method yourself. 请注意,使用gtest-1.6.0/src/gtest-all.cc构建库需要您自己提供main方法。 If you want to avoid that altogether and use the default implementation of the main method provided by Googletest, build your library including gtest_main.cc . 如果您想完全避免这种情况并使用Googletest提供的main方法的默认实现,请构建包含gtest_main.cc的库。

That is: 那是:

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

Also, keep in mind that implementing your own main method is not the recommended way of defining the SetUp and TearDown behaviours; 另外,请记住,实现自己的main方法不是定义SetUpTearDown行为的推荐方法; you should be using fixtures instead. 你应该使用灯具代替。 Check the Googletest documentation on the topic . 查看有关该主题Googletest文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM