[QtCratot]_0_0_googletest单元测试
创建时间:20210822 16:36:00
获得googletest工程
- 下载googletest工程到本地
- git clone https://github.com/google/googletest.git
创建QT测试工程
-
添加BaseUtil的测试工程
-
选择googletest框架和cmake/qmake管理工程, 推荐qmake
-
执行验证
编写测试用例,及运行后的测试结果
项目整理:GoogleTest可以只留下使用的文件
-
删除没有用的示例和文档来减少文件管理
googletest ├── googlemock │ ├── include │ └── src └── googletest ├── include └── src
googletest ├── googlemock │ ├── include │ │ └── gmock │ │ ├── gmock-actions.h │ │ ├── gmock-cardinalities.h │ │ ├── gmock-function-mocker.h │ │ ├── gmock.h │ │ ├── gmock-matchers.h │ │ ├── gmock-more-actions.h │ │ ├── gmock-more-matchers.h │ │ ├── gmock-nice-strict.h │ │ ├── gmock-spec-builders.h │ │ └── internal │ │ ├── custom │ │ │ ├── gmock-generated-actions.h │ │ │ ├── gmock-matchers.h │ │ │ ├── gmock-port.h │ │ │ └── README.md │ │ ├── gmock-internal-utils.h │ │ ├── gmock-port.h │ │ └── gmock-pp.h │ └── src │ ├── gmock-all.cc │ ├── gmock-cardinalities.cc │ ├── gmock.cc │ ├── gmock-internal-utils.cc │ ├── gmock_main.cc │ ├── gmock-matchers.cc │ └── gmock-spec-builders.cc └── googletest ├── include │ └── gtest │ ├── gtest-death-test.h │ ├── gtest.h │ ├── gtest-matchers.h │ ├── gtest-message.h │ ├── gtest-param-test.h │ ├── gtest_pred_impl.h │ ├── gtest-printers.h │ ├── gtest_prod.h │ ├── gtest-spi.h │ ├── gtest-test-part.h │ ├── gtest-typed-test.h │ └── internal │ ├── custom │ │ ├── gtest.h │ │ ├── gtest-port.h │ │ ├── gtest-printers.h │ │ └── README.md │ ├── gtest-death-test-internal.h │ ├── gtest-filepath.h │ ├── gtest-internal.h │ ├── gtest-param-util.h │ ├── gtest-port-arch.h │ ├── gtest-port.h │ ├── gtest-string.h │ └── gtest-type-util.h └── src ├── gtest-all.cc ├── gtest.cc ├── gtest-death-test.cc ├── gtest-filepath.cc ├── gtest-internal-inl.h ├── gtest_main.cc ├── gtest-matchers.cc ├── gtest-port.cc ├── gtest-printers.cc ├── gtest-test-part.cc └── gtest-typed-test.cc 12 directories, 57 files
-
项目整理:将GoogleTest的配置文件,直接写到pro文件中
# GoogleTest配置 isEmpty(GOOGLETEST_DIR) { warning("Using googletest src dir specified at Qt Creator wizard") message("set GOOGLETEST_DIR as environment variable or qmake variable to get rid of this message") GOOGLETEST_DIR = ../googletest } !isEmpty(GOOGLETEST_DIR): { GTEST_SRCDIR = $$GOOGLETEST_DIR/googletest GMOCK_SRCDIR = $$GOOGLETEST_DIR/googlemock } requires(exists($$GTEST_SRCDIR):exists($$GMOCK_SRCDIR)) !exists($$GOOGLETEST_DIR):message("No googletest src dir found - set GOOGLETEST_DIR to enable.") DEFINES += \ GTEST_LANG_CXX11 INCLUDEPATH *= \ $$GTEST_SRCDIR \ $$GTEST_SRCDIR/include \ $$GMOCK_SRCDIR \ $$GMOCK_SRCDIR/include SOURCES += \ $$GTEST_SRCDIR/src/gtest-all.cc \ $$GMOCK_SRCDIR/src/gmock-all.cc