zoukankan      html  css  js  c++  java
  • gtest写了第一个测试用例错误和结算过程

    安装好gtest后,编写第一个測试案例test_main.cpp

    #include <iostream>
    #include <gtest/gtest.h>
    
    using namespace std;
    
    int Foo(int a,int b)
    {
     return a+b;
    }
    
    TEST(FooTest, ZeroEqual)
    {
     ASSERT_EQ(0,0);
    }
    
    TEST(FooTest, HandleNoneZeroInput)
    {
        EXPECT_EQ(12,Foo(4, 10));
        EXPECT_EQ(6, Foo(30, 18));
    }
    
    
    int main(int argc, char* argv[])
    {
        testing::InitGoogleTest(&argc, argv);
        return RUN_ALL_TESTS();
    }


    依照gtest的介绍MakeFile文件为

    TARGET=test_main
    
    all:
        gtest-config --min-version=1.0 || echo "Insufficient Google Test version."
        g++  $(gtest-config --cppflags --cxxflags) -o $(TARGET).o -c test_main.cpp
        g++  $(gtest-config --ldflags --libs) -o $(TARGET) $(TARGET).o
    clean:
        rm -rf *.o $(TARGET)


    可是编译的时候,出现错误

    cxy-/home/chenxueyou/gtest$ make
    gtest-config --min-version=1.0 || echo "Insufficient Google Test version."
    g++   -o test_main.o -c test_main.cpp
    g++  -o test_main test_main.o
    test_main.o: In function `FooTest_ZeroEqual_Test::TestBody()':
    test_main.cpp:(.text+0x9e): undefined reference to `testing::internal::AssertHelper::AssertHelper(testing::TestPartResult::Type, char const*, int, char const*)'
    ...


    省略了部分错误信息。看到了undefined reference,编译通过,可是链接失败。能够推測是没有找到相应的库。再细致看实际运行时打印的命令为

       g++  -o test_main.o -c test_main.cpp
        g++  -o test_main test_main.o


    非常显然,没有引入gtest的头文件,也没有载入gtest相应的库。


    运行命令
    >echo $(gtest-config --cppflags --cxxflags)

    echo $(gtest-config --ldflags --libs)
    能够得到gtest配置的头文件路径和库文件路径。

    cxy-/home/chenxueyou/gtest$ echo $(gtest-config --cppflags --cxxflags)
    -I/usr/include -pthread
    cxy-/home/chenxueyou/gtest$ echo $(gtest-config --ldflags --libs)
    -L/usr/lib64 -lgtest -pthread


    而在我们的Makefile中运行时上面两个命令的结果为空。

    所以改动Makefile,手动指定头文件路径和库文件路径,Makefile为

    TARGET=test_main
    
    all:
        gtest-config --min-version=1.0 || echo "Insufficient Google Test version."
        g++  -I/usr/include -pthread -o $(TARGET).o -c test_main.cpp
        g++ -L/usr/lib64 -lgtest -pthread -o $(TARGET) $(TARGET).o
    clean:
        rm -rf *.o $(TARGET)


    这样,我们的第一个gtest測试文件就能编译通过了。

    总结

    1.Makefile实际运行的命令可能与预想的命令不一样。要细致查看。
    2.gtest通过头文件和库的方式引入project。要指定其头文件和库文件的位置
    3.gtest-config命令可以帮助我们找到相应的路径


    欢迎光临我的站点----蝴蝶忽然的博客园----人既无名的专栏。 
    假设阅读本文过程中有不论什么问题,请联系作者,转载请注明出处!

    版权声明:本文博客原创文章,博客,未经同意,不得转载。

  • 相关阅读:
    java面试之String源码中equals具体实现
    JVM虚拟机—JVM的垃圾回收机制(转载)
    Mysql学习笔记—视图
    Mysql学习笔记—索引
    JVM虚拟机—JVM内存
    设计模式—装饰器模式
    设计模式—代理模式
    设计模式—适配器模式
    设计模式—观察者模式
    设计模式—建造者模式
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/4649331.html
Copyright © 2011-2022 走看看