Multi-instance module test in C language

Write a test

Overview

Test your C code with google test. This time code.

Intro

I made a Counter the other day, but I don't know if it works. So I'll try a unit test.

Directory structure

.
├── counter
│   ├── build
│   ├── makefile
│   ├── src
│   └── tests
└── googletest
     ├── CMakeLists.txt
     ├── README.md
     ├── appveyor.yml
     ├── googlemock
     ├── googletest
     └── travis.sh

google test build

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.

Try to compile

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.

Write test code

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.

Summary

Recommended Posts

Multi-instance module test in C language
Machine language embedding in C language
Heapsort made in C language
Try to make a Python module in C language
Write a table-driven test in C
Realize interface class in C language
Segfault with 16 characters in C language
Linked list (list_head / queue) in C language
How to use Google Test in C
Generate C language from S-expressions in Python
How to multi-process exclusive control in C language
Set up a UDP server in C language
I tried adding a Python3 module in C
Install python's C language dependent module in wheel format with multi stage build
I made a module in C language to filter images loaded by Python
Handle signals in C
I tried using google test and CMake in C
C language ALDS1_3_B Queue
Access MongoDB in C
Next Python in C
[C language algorithm] Endianness
C API in Python 3
Go language to see and remember Part 7 C language in GO language
I tried to illustrate the time and time in C language
[C language algorithm] Block movement
Extend python in C ++ (Boost.NumPy)
C language ALDS1_4_B Binary Search
Algorithm in Python (primality test)
Use regular expressions in C
Programming language in "Hello World"
Imitated Python's Numpy in C #
Binary search in Python / C ++
Hello World in GO language
Can it be done in 1 minute? No installation required, Google Test sample for C language for Linux
[C language] readdir () vs readdir_r ()
Python debug and test module
C language ALDS1_4_A Linear Search
Minimum spanning tree in C #
Set python test in jenkins
Introduction to Socket API Learned in C Language Part 1 Server Edition