Google testing framework 1

  • Download project https://github.com/google/googletest
  • The project file into the specified location
  • unzip unzip the project file
  • cmake ./
  • make compilation
  • Then include files in the project folder, lib folder into the current directory
  • Main.cpp file a written test, unit.h + unit.c achieve a certain function, function declarations in header files, .c in implementation.
/*************************************************************************
	> File Name: unit.h
	> Author: 
	> Mail: 
	> Created Time: 2020年01月12日 星期日 15时47分14秒
 ************************************************************************/

#ifndef _UNIT_H
#define _UNIT_H
int add(int a, int b);
#endif

/*************************************************************************
	> File Name: unit.c
	> Author: 
	> Mail: 
	> Created Time: 2020年01月12日 星期日 15时49分42秒
 ************************************************************************/

#include<stdio.h>
#include"unit.h"

int add(int a, int b){
    return a + b;
}

  • In Test main (with specific reference to the source)
/*************************************************************************
	> File Name: main.cpp
	> Author: 
	> Mail: 
	> Created Time: 2020年01月12日 星期日 15时28分00秒
 ************************************************************************/
#include"unit.h"
#include<gtest/gtest.h>
#include<iostream>
using namespace std;

TEST(test, add_function){
    EXPECT_EQ(add(3, 4), 7);

}

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

  • In the Link + compiler, the ultimate command g ++ --std = c ++ 11 -I./include -L./lib main.cpp unit.c -lgtest -lpthread achieve compilation phase
  • Finally ./a.out output
Published 48 original articles · won praise 5 · Views 761

Guess you like

Origin blog.csdn.net/weixin_43899266/article/details/103947331