LoginSignup
2
1

More than 5 years have passed since last update.

C言語でマルチインスタンスモジュール テスト編

Posted at

テストを書く

概要

google test で C のコードをテストします。
今回のコード

イントロ

先日つくった Counter ですが、ちゃんと動くのかわかりません。
なので、ユニットテストやってみます。

ディレクトリ構成

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

google test のビルド

テストハーネスはgoogletest をつかいます。まず git clone します。
わたしは git 管理しているプロジェクトの中に git submodule add することが多いです。

git clone https://github.com/google/googletest.git

googletest と googlemock が含まれていますが、とりあえず必要な googletest だけビルドします。

cd googletest/googletest
mkdir build
cd build
cmake ..
make

libgtest.a と libgtest_main.a という二つのファイルが生成されます。

ためしにコンパイルしてみる

テストコードを書く前に試しにコンパイルして実行してみます。

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

実行結果は、

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.

となります。

テストコードを書く

実行できたのでテストコードを書きます。

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));
}
  • extern "C" {} は C++ で C のヘッダをインクルードするためのものです
  • TEST(test_case_name, test_name) マクロをつかってテスト関数をつくります
  • ASSERT_EQ() マクロで第一引数と第二引数が等しかったらテスト通過です

実行してみます。main 関数はライブラリでロードしている gtest_main に含まれていて、TEST() マクロで作成したテスト関数を自動で実行してくれます。

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

結果です

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

失敗しています。Counter_GetCount(testCounter) が 0 を返してくるとおもってたのに実際は 1 が返ってきたので失敗だよとのことです。以下のように書きなおしてもう一度やってみましょう。

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.

成功しました。

まとめ

  • googletest でユニットテストを実行できた
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1