Test your C code with google test. This time code.
I made a Counter the other day, but I don't know if it works. So I'll try a unit test.
.
├── counter
│ ├── build
│ ├── makefile
│ ├── src
│ └── tests
└── googletest
├── CMakeLists.txt
├── README.md
├── appveyor.yml
├── googlemock
├── googletest
└── travis.sh
The test harness uses googletest. First, git clone. I often do a git submodule add in a git managed project.
git clone https://github.com/google/googletest.git
It includes googletest and googlemock, but for now build only the googletest you need.
cd googletest/googletest
mkdir build
cd build
cmake ..
make
Two files will be generated, libgtest.a and libgtest_main.a.
Before writing the test code, try compiling and running it.
cd counter
gcc -c -o build/counter.o src/counter.c
g++ -o build/unitetest -Isrc -I../googletest/googletest/include tests/test_counter.cpp build/counter.o -L../googletest/googletest/build -lgtest_main -lgtest -lpthread
build/unitetest
The execution result is
Running main() from gtest_main.cc
[==========] Running 0 tests from 0 test cases.
[==========] 0 tests from 0 test cases ran. (1 ms total)
[ PASSED ] 0 tests.
It will be.
Now that we've run it, write the test code.
mkdir tests
vim tests/test_counter.cpp
//tests/test_counter.cpp
#include "gtest/gtest.h"
extern "C" {
#include "counter.h"
}
TEST(counterTest, count) {
Counter testCounter;
testCounter = Counter_Create();
Counter_CountUp(testCounter);
ASSERT_EQ(0, Counter_GetCount(testCounter));
}
I will try it. The main function is included in gtest_main loaded in the library and will automatically execute the test function created by the TEST () macro.
gcc -c -o build/counter.o src/counter.c
g++ -o build/unitetest -Isrc -I../googletest/googletest/include tests/test_counter.cpp build/counter.o -L../googletest/googletest/build -lgtest_main -lgtest -lpthread
build/unitetest
Is the result
Running main() from gtest_main.cc
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from counterTest
[ RUN ] counterTest.count
tests/test_counter.cpp:11: Failure
Expected: 0
To be equal to: Counter_GetCount(testCounter)
Which is: 1
[ FAILED ] counterTest.count (0 ms)
[----------] 1 test from counterTest (0 ms total)
[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (0 ms total)
[ PASSED ] 0 tests.
[ FAILED ] 1 test, listed below:
[ FAILED ] counterTest.count
I'm failing. I thought that Counter_GetCount (testCounter) would return 0, but in reality it returned 1, so it was a failure. Rewrite it as follows and try again.
ASSERT_EQ(1, Counter_GetCount(testCounter));
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from counterTest
[ RUN ] counterTest.count
[ OK ] counterTest.count (0 ms)
[----------] 1 test from counterTest (0 ms total)
[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (0 ms total)
[ PASSED ] 1 test.
Succeeded.
Recommended Posts