LoginSignup
0
0

More than 5 years have passed since last update.

パッケージマネージャでGoogle Testをインストールした際の使用方法

Last updated at Posted at 2018-12-09

はじめに

Arch Linuxではパッケージマネージャで簡単にGoogle Testをインストールすることができる.
しかし,パッケージマネージャでインストールしたGoogle Testの使用方法が見つからなかったため,
自分用の備忘録として残す.

使用方法

1. インストール

パッケージマネージャでインストール.
Arch Linuxを使っている場合は以下.
sudo pacman -S gtest

2. テストコードの準備

テスト対象コード

簡単な足し算のプログラム例を準備

sum.cpp
#include "sum.hpp"

int sum(int x, int y) {
    return x + y;
}
sum.hpp
int sum(int x, int y);

テスト実行コード

test.cpp
#include <gtest/gtest.h> //gtestのヘッダのinclude
#include "sum.hpp"

/*
テスト本体の書き方は以下の通り.
TEST(テストケース名, テスト名) {
 テストコード本体
} 
*/
// テストが通るケース
TEST(SumTest, TestCaseTrue) {
    EXPECT_EQ(1+2, sum(1, 2));
    EXPECT_EQ(2+3, sum(2, 3));
    EXPECT_EQ(3+4, sum(3, 4));
}

// テストが通らないケース
TEST(SumTest, TestCaseWrong) {
    EXPECT_EQ(1, sum(1, 2));
    EXPECT_EQ(2, sum(2, 3));
    EXPECT_EQ(3, sum(3, 4));
}

3. コンパイル,実行

g++ -o test -lgtest -lgtest_main -lpthread test.cpp sum.cppでコンパイルした後,./testで実行.

4. 結果

Running main() from /build/gtest/src/googletest-release-1.8.1/googletest/src/gtest_main.cc
[==========] Running 2 tests from 1 test case.
[----------] Global test environment set-up.
[----------] 2 tests from SumTest
[ RUN      ] SumTest.TestCaseTrue
[       OK ] SumTest.TestCaseTrue (0 ms)
[ RUN      ] SumTest.TestCaseWrong
test.cpp:11: Failure
Expected equality of these values:
  1
  sum(1, 2)
    Which is: 3
test.cpp:12: Failure
Expected equality of these values:
  2
  sum(2, 3)
    Which is: 5
test.cpp:13: Failure
Expected equality of these values:
  3
  sum(3, 4)
    Which is: 7
[  FAILED  ] SumTest.TestCaseWrong (0 ms)
[----------] 2 tests from SumTest (0 ms total)

[----------] Global test environment tear-down
[==========] 2 tests from 1 test case ran. (0 ms total)
[  PASSED  ] 1 test.
[  FAILED  ] 1 test, listed below:
[  FAILED  ] SumTest.TestCaseWrong

 1 FAILED TEST

テストが実行できたことが確認できた.

参考にしたページ

入門ガイド — Google Test ドキュメント日本語訳
UbuntuでサクッとGoogle Testを使ってみよう - がんじろうの日記

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