3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[ubuntu]Google Testの導入方法

Last updated at Posted at 2020-02-22

#概要
仕事でGoogle Testを使うことになったので導入方法などをメモ。
#環境

  • Ubuntu 18.04.4 LTS
  • Google Test v1.10.0

#1. ダウンロード

curl -OL https://github.com/google/googletest/archive/release-1.10.0.tar.gz

#2. 解凍

tar -zxvf release-1.10.0.tar.gz

#3. ビルド & インストール

mkdir -p googletest-release-1.10.0/build
cd googletest-release-1.10.0/build
cmake ..
make install

/usr/local/includeと/usr/loca/lib下にヘッダファイルとライブラリが展開されるので、あとはそれらをテスト環境に取り込むだけ。

#4. テスト

ソースコード
#include  <gtest/gtest.h>

//テスト対象の関数
int sum(int a, int b){
    return a+b;
}

//テストコード
TEST(TestCase, sum) {
    EXPECT_EQ(2, sum(1,1));        //OK
    EXPECT_EQ(1000, sum(123,877)); //OK
    EXPECT_EQ(-8, sum(-12,3));     //NG
}
ビルド
g++ test.cpp -lgtest_main -lgtest -lpthread -o test

libpthredもリンクする必要がある。

テスト結果
[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 1 test from TestCase
[ RUN      ] TestCase.sum
test.cpp:10: Failure
Expected equality of these values:
  -8
  sum(-12,3)
    Which is: -9
[  FAILED  ] TestCase.sum (0 ms)
[----------] 1 test from TestCase (0 ms total)

[----------] Global test environment tear-down
[==========] 1 test from 1 test suite ran. (0 ms total)
[  PASSED  ] 0 tests.
[  FAILED  ] 1 test, listed below:
[  FAILED  ] TestCase.sum

 1 FAILED TEST
3
3
1

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
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?