Can it be done in 1 minute? No installation required, Google Test sample for C language for Linux

First of all, I apologize for the exaggerated expression in the title. .. ..

http://qiita.com/igayu/items/eaa63d017e0590504999 I've mentioned the good points of Google Test for C developers, but let's try it next time, so I prepared a reasonable sample. It seems that GoogleTest itself provides sample code, but some people don't feel like reading it because it's in English, such as how to build it if they're not used to it. If you cut it down to that common sense and study, your work will not turn. I'll push my back a little more for those who think that Google Test seems to be useful.

The sample introduced here is written on the following assumptions.

・ C for C language developers++Do not touch the grammar of
· Linux gcc based(I have CentOS 6 on Windows 7 with vmware.Use 7 as a virtual OS)
・ Leave the minimum important essence of Google Test that I think(I will write the next step in another article)
・ Although it is not for super amateurs, it is intended to be difficult enough for anyone with basic knowledge of Linux or C to understand.

Preparation

・ Get Google Test and unzip it. https://github.com/google/googletest/releases Use googletest-release-1.8.0.zip from googletest-release-1.8.0.zip this time (latest version at the time of writing). -Create target.c, target.h, test.cpp, and Makefile as empty files.

In summary, it has the following configuration.

C:\Users\xxx\Desktop\gtestSample
        googletest-release-1.8.0 (directory)
        Makefile (Empty file)
        target.c (Empty file)
        target.h (Empty file)
        test.cpp (Empty file)

I will put the sample code, so copy and paste it to an empty file as it is, make it, and move it. If you are in a position to recommend to others, I think it is better to make the above configuration so that it works and let others touch the environment.

Create a file

Copy the contents of target.c, target.c, target.h, test.cpp, and Makefile below to the created empty file.

target.c


//Function to be tested
//Returns 0 if 0 is entered, 1 otherwise
int function(int a){
    if (a == 0){
        return 0;
    } else {
        return 1;
    }
}

target.h


//Prototype declaration of the function under test
#ifndef _TARGET_H_
#define _TARGET_H_

int function(int a);

#endif /* _TARGET_H_ */

test.cpp


//Test case description file
#include "gtest/gtest.h" //This is all you need to do with googleTest
//I want to be able to call the function under test
// extern "C"Without it, it is not interpreted as C, and it tends to be surprisingly addictive.
extern "C" {
#include "target.h"
}

//fixtureName can be thought of as a group name that groups test cases together, any character string
//Other classes-testing::I don't think it's magic until Test
class fixtureName : public ::testing::Test {
protected:
    //Test cases grouped by fixtureName before executing each test case
    //Call this function. Test code is neat if you put in common initialization processing
    virtual void SetUp(){
    }
    //A function called after executing a test case like SetUp. Describe the common cleanup.
    virtual void TearDown(){
    }
};

//A successful test case. See the Google Test manual for a detailed explanation.
TEST_F(fixtureName, testOk)
{
    EXPECT_EQ(0, function(0));
    EXPECT_EQ(1, function(100));
}
//Also write a test case that dares to fail.
TEST_F(fixtureName, testNg)
{
    EXPECT_EQ(1, function(0));
    EXPECT_EQ(0, function(100));
}
# Makefile
# all:Lower 3 lines of indentation,
# gtest-gen:One line of indentation below
#Is half-width blank, but please use one tab
# target.Since c is C, gcc, otherwise C++So g++Build with
# gtest_main.cc is the main function provided by Google Test,
# gtest-all.cc is a file containing all Google Test
# -Also note that lpthread is attached.
#After make or make all, build and execute.
all:
	gcc -c target.c
	g++ test.cpp googletest-release-1.8.0/googletest/src/gtest_main.cc gtest/gtest-all.cc -I. -lpthread target.o 
	./a.out

gtest-gen:
	python googletest-release-1.8.0/googletest/scripts/fuse_gtest_files.py ./

Install Google Test (alternative)

In the preparation stage, you can make it on a Windows PC, but from here on, it is assumed that you will be working on Linux.

cd <Directory with samples>/gtestSample
make gtest-gen

Some files are spit out in gtestSample / gtest. What I'm doing is as in the Makefile python googletest-release-1.8.0 / googletest / scripts / fuse_gtest_files.py ./, run fuse_gtest_files.py and get the output in the current directory./ Letspit out (maybe you may be addicted to the presence / absence and version of python). See later what will be done. Since you can create a huge C file packed with all the functions of Google Test, you can compile it with your own test code without creating a library. This is one of the motivations for me to strongly recommend Google Test. You don't need to make install as root, and you won't have the test running on this person's PC even though it's installed on this person's PC.

Run

cd <Directory with samples>/gtestSample
make

that's all. Two test cases should be run, one should be PASS and one should be NG (I wrote in the Makefile comment but don't forget to replace the whitespace with tabs). I think you can intuitively understand how to read the log without any special explanation.

Commitment

You can write the test case with TEST (), but dare to write the test case only with TEST_F (). Experienced function testing does not end with one test case, and if you create two or more test cases, you can perform common pre-processing and post-processing with a high probability. Also, the test code in which TEST () and TEST_F () are mixed is hard to see. So my team only uses TEST_F ().

Finally

Except for the preparation, if you get used to the process from file creation to execution, it will take 1 minute! (= `E'=) ゞ Based on this sample, I will add the functions that can be used by Google Test (for example, temporarily disabling test cases), but I am lazy, so someday ...

Recommended Posts

Can it be done in 1 minute? No installation required, Google Test sample for C language for Linux
Morphological analysis and tfidf (with test code) that can be done in about 1 minute
Multi-instance module test in C language
Guidelines for reincarnating in the world of linux programming development (C / C ++ language)
How to use Google Test in C
It can be achieved in 1 minute! Decorator that caches function execution results in memcached