はじめに
前回、googletestのインストールまでを実施した。今回は実際の動作確認を行う。
サンプルプログラム
googletest/googletest/samples以下にサンプルコードがあるので、この中のsample1を使ってみる。
まだパスを通していないので、オプションが多いが以下のコマンドでビルドする。
$ g++ sample1.cc sample1_unittest.cc -pthread -lgtest -lgtest_main -I../include/ -L../../build/lib/ -std=c++11
$ ./a.out
./a.out: /lib64/libstdc++.so.6: version `GLIBCXX_3.4.20' not found (required by ./a.out)
./a.out: /lib64/libstdc++.so.6: version `GLIBCXX_3.4.21' not found (required by ./a.out)
とりあえずgccのバージョンをあげてみる。たまたま7.3.0を入れていたので、そちらに入れ替えてビルドしなおして実行。
以下のように6個すべてのテストがパスしているので、動作確認としてはこんなところでよいかな。
$ ./a.out
Running main() from /home/server/tmp/googletest/googletest/src/gtest_main.cc
[==========] Running 6 tests from 2 test suites.
[----------] Global test environment set-up.
[----------] 3 tests from FactorialTest
[ RUN ] FactorialTest.Negative
[ OK ] FactorialTest.Negative (0 ms)
[ RUN ] FactorialTest.Zero
[ OK ] FactorialTest.Zero (0 ms)
[ RUN ] FactorialTest.Positive
[ OK ] FactorialTest.Positive (0 ms)
[----------] 3 tests from FactorialTest (0 ms total)
[----------] 3 tests from IsPrimeTest
[ RUN ] IsPrimeTest.Negative
[ OK ] IsPrimeTest.Negative (0 ms)
[ RUN ] IsPrimeTest.Trivial
[ OK ] IsPrimeTest.Trivial (0 ms)
[ RUN ] IsPrimeTest.Positive
[ OK ] IsPrimeTest.Positive (0 ms)
[----------] 3 tests from IsPrimeTest (0 ms total)
[----------] Global test environment tear-down
[==========] 6 tests from 2 test suites ran. (0 ms total)
[ PASSED ] 6 tests.
/usr/local以下に移動
ヘッダとmakeしたライブラリを/usr/local以下に移動しておく。
$ sudo cp -r tmp/googletest/googletest/include/* /usr/local/include/
$ sudo cp -r tmp/googletest/googlemock/include/* /usr/local/include/
$ sudo cp -r tmp/googletest/build/lib/* /usr/local/lib64/
sample環境
以下自前でテストを書いてみる。gccのバージョンは5.2.0に戻している。以下のような環境を作って、
.
`-- test
|-- run.sh
`-- test.cpp
自明なテストを置いて、
test.cpp
# !/bin/sh
g++ test_rect_coord.cpp ../src/rect_coord.cpp -I. -I../src -I../ac_types_2.6 -lgtest -lgtest_main -std=c++11 -pthread
実行スクリプト書いて、
run.sh
# !/bin/sh
g++ test.cpp -I. -lgtest -lgtest_main -std=c++11 -pthread
$ ./run.sh
$ ./test_rect_coord
Running main() from /home/server/tmp/googletest/googletest/src/gtest_main.cc
[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 1 test from sample
[ RUN ] sample.sampletest
[ OK ] sample.sampletest (0 ms)
[----------] 1 test from sample (0 ms total)
[----------] Global test environment tear-down
[==========] 1 test from 1 test suite ran. (0 ms total)
[ PASSED ] 1 test.
という感じで、無事移行完了。が、毎回googletestをビルドした場所が表示されるの嫌だな…本番環境ではビルドする場所にも気を付けよう。