使用CMAKE的Android NDK C 的Googletest
GoogleTest for Android NDK C++ with CMake
如何使用cmake上的Android上的本机C 代码设置Googletest?Android NDK与Googletest捆绑在一起,但仅适用于Android.mk(此处(。如何将Android.mk Gtest设置移植到我的cmakelists.txt?一旦设置了,是否可以通过Android Studio的测试配置运行测试跑者?
我只能为您的第一个和主要问题提供答案,但这是一个有效的解决方案。它不是针对Android的;只要您可以运行cmake和ctest。
我对Googletest不了解,但是我记得几个月前有一个非常相似的问题。就我而言,我想将CMAKE与Boost UnitTestFramework一起使用。
我搜索了一些,然后我陷入了某个教程。他们提供的解决方案仅仅是为了使用正则表达式解析您的测试文件内容。上行是,它非常可自定义,并且每个文件允许进行多个单位测试。我做到了,而且效果很好。当然,当您添加新的单元测试时,您必须重新运行cmake。
就像我说的那样,下面我要发布的内容是为UnitTestFramework提供的,但最大的区别在于正则表达式以及我使用的变量名称。如果您不习惯使用CMAKE的正则表达式(我不是(,这是有关字符串操作的官方文档页面。
这是我最终出现的CMakeLists.txt
文件。我将其用作test/
目录下的独立CMakeLists.txt
文件。
# =============================
# = ADDING TESTS =
# =============================
include(CTest)
find_package(Boost COMPONENTS unit_test_framework REQUIRED)
include_directories(${Boost_INCLUDE_DIRS})
file(GLOB_RECURSE TEST_SRCS RELATIVE ${TEST_SOURCE_DIR} *.cpp)
set(TEST_EXTRA_LIBS ${Boost_LIBRARIES} ${TEST_MAIN_LIB})
# Function which, given a file name and a test name, yields the
# name of the GoogleTest test case.
# That way, several different files can have the same test name.
# Adapt this to GoogleTest.
function(getTestCase testFileName testName outTestCase)
string(REGEX MATCH "Test([_a-zA-Z][_a-zA-Z0-9]*)" match ${testFileName})
string(REGEX REPLACE ".*Test([_a-zA-Z][_a-zA-Z0-9]*).*" "\1" testCase ${match})
set(testCase Test${testCase}${testName})
set(${outTestCase} ${testCase} PARENT_SCOPE)
endfunction()
# Function which adds all tests within a certain test file.
function(add_all_tests_in testSrc)
get_filename_component(testFileName ${testSrc} NAME_WE)
add_executable(${testFileName} ${testSrc})
target_link_libraries(${testFileName} ${TEST_EXTRA_LIBS})
set_target_properties(${testFileName} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
file(READ "${testSrc}" testFileContents)
# You should change this Regex for what you need in your case.
# Maybe something like:
# "TEST\( *([_a-zA-Z][_a-zA-Z0-9]* *, *[_a-zA-Z][_a-zA-Z0-9]*) *\)"
string(REGEX MATCHALL "DEF_TEST_CASE\( *([_a-zA-Z][_a-zA-Z0-9]*) *\)" unitTests ${testFileContents})
foreach(match ${unitTests})
# This replace will also probably need some change.
string(REGEX REPLACE ".*\( *([_a-zA-Z][_a-zA-Z0-9]*) *\).*" "\1" testName ${match})
getTestCase(${testFileName} ${testName} testCase)
# Actually add the test.
# I wanted my CTest test names to be in the form
# <fileName>.<testName>, but you can use any
# format you want. Suit yourself.
#
# Also, in order for CMake to run the tests one by one,
# you have to find how to invoke the test executable.
# In the case of Boost, the option --run_test=<Boost_Test_Name>
# runs only the test called <Boost_Test_Name>. There should be
# an equivalent option for GoogleTest, I'm sure.
add_test(NAME "${testFileName}.${testName}" WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} COMMAND ${CMAKE_CURRENT_BINARY_DIR}/${testFileName} --run_test=${testCase} --catch_system_error=yes)
endforeach()
endfunction()
# Actually add all tests in all test files.
foreach(testSrc ${TEST_SRCS})
add_all_tests_in(${testSrc})
endforeach()
为我工作。在build/
目录下运行CMAKE后,运行所有测试产生:
$ cd test && ctest
Test project /home/anthonyd973/Git/Git_Projects/MySweeper/build/test
Start 1: TestFieldMaker.makeFromFile
1/12 Test #1: TestFieldMaker.makeFromFile ........ Passed 0.03 sec
Start 2: TestFieldMaker.make
2/12 Test #2: TestFieldMaker.make ................ Passed 0.01 sec
Start 3: TestFieldMaker._computeFieldDims
3/12 Test #3: TestFieldMaker._computeFieldDims ... Passed 0.01 sec
Start 4: TestFieldMaker._populateField
4/12 Test #4: TestFieldMaker._populateField ...... Passed 0.00 sec
Start 5: TestInputField.InputField
5/12 Test #5: TestInputField.InputField .......... Passed 0.00 sec
Start 6: TestCell.Cell
6/12 Test #6: TestCell.Cell ...................... Passed 0.00 sec
Start 7: TestCell.initNeighbours
7/12 Test #7: TestCell.initNeighbours ............ Passed 0.00 sec
Start 8: TestCell.updateNeighbours
8/12 Test #8: TestCell.updateNeighbours .......... Passed 0.00 sec
Start 9: TestCell._mark
9/12 Test #9: TestCell._mark ..................... Passed 0.00 sec
Start 10: TestMySweeper.MySweeper
10/12 Test #10: TestMySweeper.MySweeper ............ Passed 0.00 sec
Start 11: TestField.Field
11/12 Test #11: TestField.Field .................... Passed 0.01 sec
Start 12: TestField._initNeighbours
12/12 Test #12: TestField._initNeighbours .......... Passed 0.00 sec
100% tests passed, 0 tests failed out of 12
Total Test time (real) = 0.10 sec
希望使用正则表达式不会引起两个问题:)。
在您的cmakelists中添加gtest.txt1(包括gtest/gmock头文件;
include_directories("yourgooglestestdir/googletest/include/")
include_directories("yourgooglestestdir/googlemock/include/")
2(链接gtest/gmock libs
LINK_DIRECTORIES("yourgooglestestdir/lib/")
3(在您的cmakelists.txt中包括file android.mk使用Android MK部件。
- 如何使用ndk-build.cmd构建Android.so文件
- Android NDK传感器向事件队列报告奇怪的间隔
- Android NDK 编译 LAME HAVE_MPGLIB > 'interface.h' 文件未找到
- NDK:如何通过ndkr15c包含"RefBase.h"?
- Android NDK clang 编译器错误在 Windows 上显示'No such file or directory'
- 为什么使用 NDK 不能存在不同的 stl 实现?
- googletest:测试太大的数据结构
- 禁用自动捕获 Googletest 单元测试中的C++异常
- 这是GoogleTest AssertionResult中的错误吗?
- 在 constexpr 构造函数 (c++17) 中赋值到 const char * 在使用 Android NDK 时
- 如何在 android ndk 上链接 C 和 C++ 代码,以及 C 和 C++ 运行时库(包括 STL)?
- Android NDK 未定义对ASensorEventQueue_registerSensor的引用
- 如何将 libjpeg-turbo Android NDK 添加为静态库
- 在 Flutter 应用程序中使用 Android NDK 中的 AssetManager 类
- Android NDK - 无法在 Visual Studio 2017 中调试
- 对于哪些 ABI,我可以使用 NDK v10e 构建 apk?
- -fno-unwind-tables 和 -fno-async-unwind-tables 不起作用 NDK clang
- 不再支持Android NDK APP_STL gnustl_shared
- 使用CMAKE的Android NDK C 的Googletest
- Googletest for Android NDK