# Install

# Windows

下載並安裝 Visual Studio 2022 https://visualstudio.microsoft.com/zh-hant/downloads/

安裝下圖標線的套件
Google Test Setup

# Linux

更新 apt 資料庫

sudo apt update

安裝以下套件

sudo apt install gcc g++ lcov
sudo apt install libgtest-dev

進行以下操作

cd /usr/src/gtest
sudo cmake CMakeLists.txt
sudo make
sudo make install

# Testing

# Example

// vs 2019 use #include "pch.h"
#include "gtest/gtest.h"
int iCompute(int iA, int iB, char cCommand[]) {
	int i;
	for (i = 0; i < strlen(cCommand); i++) {
		if (cCommand[i] == 'A')
			iA = iA + iB;
		else if (cCommand[i] == 'B')
			iB = iA + iB;
		else
			return -1;
	}
	return iA + iB;
}
TEST(TestCaseName, TestName) {
	EXPECT_EQ(iCompute(5, 2, "ABA"), 25);
	EXPECT_EQ(iCompute(5, 2, "CCC"), -1);
}
int main(int argc, char **argv) {
	testing::InitGoogleTest(&argc, argv);
	return RUN_ALL_TESTS();
}

# Windows

VS 2019 Compiler

# Linux

// --coverage - The option is a synonym for -fprofile-arcs -ftest-coverage (when compiling) and -lgcov (when linking)
g++ -c main.cpp -o main.o --coverage
g++ main.o -o main -lgcov -lgtest -lpthread
./main > result.txt

# Coverage Testing

# Linux

gcov main.gcda
mv main.cpp.gcov coverage.txt

產生 HTML 圖表

lcov -c -d . -o test.info --rc lcov_branch_coverage=1
lcov -o test.info -e test.info "*main*" --rc lcov_branch_coverage=1 // 過濾非原始碼
genhtml --branch-coverage -o result test.info

# 參考來源

  • https://ithelp.ithome.com.tw/articles/10202905
  • https://zhuanlan.zhihu.com/p/410077415
  • https://zhuanlan.zhihu.com/p/431938210
  • https://gcc.gnu.org/onlinedocs/gcc/Instrumentation-Options.html#Instrumentation-Options