LoginSignup
3
3

More than 3 years have passed since last update.

Macで google test をお試し利用する

Posted at

目的

Macで google test をお試し利用した際の備忘録です。

google test

環境

$ sw_vers
ProductName:    Mac OS X
ProductVersion: 10.13.6
BuildVersion:   17G4015

準備

cmakeのインストール

brew install cmake

googletestをクローン

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

ビルド用ディレクトリを作成して移動

mkdir build
cd build

cmakeを実行

ln -s /usr/local/bin/gcc-9 /usr/local/bin/gcc
ln -s /usr/local/bin/g++-9 /usr/local/bin/g++
cmake -DCMAKE_C_COMPILER=/usr/local/bin/gcc -DCMAKE_CXX_COMPILER=/usr/local/bin/g++ .. # Macの場合はgcc,g++パス指定が必要

makeを実行

make
...
[100%] Built target gtest_main

mock,gtest,libをパスへコピー

sudo cp -r ../googlemock/include/gmock /usr/local/include/.
sudo cp -r ../googletest/include/gtest /usr/local/include/.
sudo cp lib/*.a /usr/local/lib/

テスト実行

test.cpp
#include <gtest/gtest.h>

TEST(TestCaseName, TestName){
  EXPECT_EQ(1, 1);
}

コンパイル

g++ test.cpp -pthread -lgtest_main -lgtest

実行ファイル(a.out)を実行

$ ./a.out 
Running main() from /Users/seigo/Desktop/python/gtest/googletest/googletest/src/gtest_main.cc
[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 1 test from TestCaseName
[ RUN      ] TestCaseName.TestName
[       OK ] TestCaseName.TestName (0 ms)
[----------] 1 test from TestCaseName (0 ms total)

[----------] Global test environment tear-down
[==========] 1 test from 1 test suite ran. (1 ms total)
[  PASSED  ] 1 test.

テスト結果を出力する際は、実行時に --gtest_output を付与する。

$ ./a.out --gtest_output=xml:./result.xml
result.xml
<?xml version="1.0" encoding="UTF-8"?>
<testsuites tests="1" failures="0" disabled="0" errors="0" time="0.001" timestamp="2020-05-19T23:56:22" name="AllTests">
  <testsuite name="TestCaseName" tests="1" failures="0" disabled="0" errors="0" time="0" timestamp="2020-05-19T23:56:22">
    <testcase name="TestName" status="run" result="completed" time="0" timestamp="2020-05-19T23:56:22" classname="TestCaseName" />
  </testsuite>
</testsuites>

json形式も指定可能

$ ./a.out --gtest_output=json:./result.json
result.json
{
  "tests": 1,
  "failures": 0,
  "disabled": 0,
  "errors": 0,
  "timestamp": "2020-05-19T23:58:37Z",
  "time": "0s",
  "name": "AllTests",
  "testsuites": [
    {
      "name": "TestCaseName",
      "tests": 1,
      "failures": 0,
      "disabled": 0,
      "errors": 0,
      "timestamp": "2020-05-19T23:58:37Z",
      "time": "0s",
      "testsuite": [
        {
          "name": "TestName",
          "status": "RUN",
          "result": "COMPLETED",
          "timestamp": "2020-05-19T23:58:37Z",
          "time": "0s",
          "classname": "TestCaseName"
        }
      ]
    }
  ]
}

gtest-port.h:1814:40: error: 'override' keyword is a C++11 extension [-Werror,-Wc++11-extensions]

以下の記事を見つけて解決

(続) Google Testを導入してみた
Google TestはC++11をベースに実装されている様です。故に、macOSデフォルトのgccやg++(試した環境ではVer4.2)ではビルドエラーが発生し、ビルドすることが出来ません。(∵ C++11で導入された新しい構文等に対応していないため)

gcc,g++のバージョンを更新する

$ brew install gcc
$ ln -s /usr/local/bin/gcc-9 /usr/local/bin/gcc
$ ln -s /usr/local/bin/g++-9 /usr/local/bin/g++
~/.bash_profileに以下を記載し、有効化します。
$ echo "export PATH=$PATH:/usr/local/bin" >> ~/.bash_profile
$ . ~/.bash_profile

MacOSの場合は、cmake時にgcc/g++パスを指定する必要がある

cmake -DCMAKE_C_COMPILER=/usr/local/bin/gcc -DCMAKE_CXX_COMPILER=/usr/local/bin/g++ ..

cmake実行後、makeするとエラーが解決できていた。

参考

googletest
Google Testを導入してみた
Google Testの使い方
(続) Google Testを導入してみた
Jenkinsでgoogle testの結果表示
Using gtest in jenkins

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