$ cd /path/project
$ git submodule add https://github.com/google/googletest/tree/master/googletest 3rdParty/googletest
$ vim CMakeLists.txt
...
add_subdirectory(3rdParty/googletest)
add_executable(your_unit_test
your_unit_test.cpp
)
add_dependencies(your_unit_test gtest)
include_directories(${gtest_SOURCE_DIR}/include)
target_link_libraries(your_unit_test
gtest
)
2016年4月4日 星期一
透過 Git Submodule 和 CMake 使用 GoogleTest
大概兩年前有練習一下 [CPP] Unit Test for C++ Usage via Google Test,連假來複習一下,著重在 CMakefile 的使用:
標籤:
cmake,
git,
googletest,
gtest,
submodule
2014年7月22日 星期二
[CPP] Incremental Test Case for C++ Usage via Google Test @ Ubuntu 14.04
既然做了 Unit Test 了,就希望 Test Case 可以不斷地累積下來。昨晚跟總監級的高手閒聊工作瑣事,以 Python 跟 MongoDB 的互動為例,其實 PyMono 很多操作都還是撰寫 Javascript ,只是從 Python 發動罷了,這時候最佳的設計是讓 Javascript 獨立出來,如此一來可以切割工作出來,讓把玩 Javascript 就專心把玩,而不要每次要改 Javascript 時,還得去動 Python code。
這樣的情境跟 Test Case 有點類似,有沒有辦法讓 Test case 增加時,不必動到測試的邏輯程式?解法就是把 Test Case 用目錄管理,每次做 Unit Test 時,是去掃目錄的檔案出來即可。
片段程式碼:
這樣的情境跟 Test Case 有點類似,有沒有辦法讓 Test case 增加時,不必動到測試的邏輯程式?解法就是把 Test Case 用目錄管理,每次做 Unit Test 時,是去掃目錄的檔案出來即可。
片段程式碼:
#include <dirent.h>
bool getFiles(std::string dir, std::vector<std::string> &files) {
DIR *dp;
struct dirent *dirp;
if((dp = opendir(dir.c_str())) == NULL)
return false;
while ((dirp = readdir(dp)) != NULL)
if(dirp->d_name && dirp->d_name[0] != '.')
files.push_back(dir+"/"+std::string(dirp->d_name));
closedir(dp);
return true;
}
const std::string testcase_dir_Func1 = "testcase_dir";
TEST(MyJob, testFunc1) {
std::vector<std::string> testcase;
std::string dirTarget = testcase_dir_Func1;
ASSERT_EQ(getFiles(dirTarget,testcase), true);
ASSERT_EQ(testcase.size() > 0, true);
for(int i=0 ; i<testcase.size() ; ++i) {
std::fstream input(testcase[i], std::fstream::binary|std::fstream::in);
std::stringstream buffer;
buffer << input.rdbuf();
ASSERT_EQ(buffer.str().length() > 10, true);
// ...
}
}
2014年7月21日 星期一
[CPP] Unit Test for C++ Usage via Google Test @ Ubuntu 14.04
跟友人閒聊把玩 CPP 的心得,就被推坑到 Google test 啦:Google C++ Testing Framework
看一下簡介就...準備跳槽了 XD
Who Is Using Google Test?
In addition to many internal projects at Google, Google Test is also used by the following notable projects:
試用:
測試失敗範例:
測試成功的範例:
看一下簡介就...準備跳槽了 XD
Who Is Using Google Test?
In addition to many internal projects at Google, Google Test is also used by the following notable projects:
- The Chromium projects (behind the Chrome browser and Chrome OS)
- The LLVM compiler
- Protocol Buffers (Google's data interchange format)
$ sudo apt-get install cmake libgtest-dev
$ mkdir gtest && cd gtest
$ cmake /usr/src/gtest/
-- The CXX compiler identification is GNU 4.8.2
-- The C compiler identification is GNU 4.8.2
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Found PythonInterp: /usr/bin/python (found version "2.7.6")
-- Looking for include file pthread.h
-- Looking for include file pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - not found
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE
-- Configuring done
-- Generating done
-- Build files have been written to: /path/gtest
$ make
Scanning dependencies of target gtest
[ 50%] Building CXX object CMakeFiles/gtest.dir/src/gtest-all.cc.o
Linking CXX static library libgtest.a
[ 50%] Built target gtest
Scanning dependencies of target gtest_main
[100%] Building CXX object CMakeFiles/gtest_main.dir/src/gtest_main.cc.o
Linking CXX static library libgtest_main.a
[100%] Built target gtest_main
$ sudo cp *.a /usr/lib
試用:
$ vim gtest.cpp
#include <gtest/gtest.h>
int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
$ g++ gtest.cpp -lgtest -lpthread
$ ./a.out
[==========] Running 0 tests from 0 test cases.
[==========] 0 tests from 0 test cases ran. (0 ms total)
[ PASSED ] 0 tests.
測試失敗範例:
$ vim gtest.cpp
#include <gtest/gtest.h>
TEST(MyJob, Action1) {
ASSERT_EQ(0, 1);
}
int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
$ g++ gtest.cpp -lgtest -lpthread
$ ./a.out
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from MyJob
[ RUN ] MyJob.Action1
gtest.cpp:4: Failure
Value of: 1
Expected: 0
[ FAILED ] MyJob.Action1 (1 ms)
[----------] 1 test from MyJob (1 ms total)
[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (1 ms total)
[ PASSED ] 0 tests.
[ FAILED ] 1 test, listed below:
[ FAILED ] MyJob.Action1
1 FAILED TEST
測試成功的範例:
$ vim gtest.cpp
#include <gtest/gtest.h>
TEST(MyJob, Action1) {
ASSERT_EQ(0, 0);
}
int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
$ g++ gtest.cpp -lgtest -lpthread
$ ./a.out
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from MyJob
[ RUN ] MyJob.Action1
[ OK ] MyJob.Action1 (0 ms)
[----------] 1 test from MyJob (0 ms total)
[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (1 ms total)
[ PASSED ] 1 test.
訂閱:
文章 (Atom)