使用TEST CASE
TEST() and TEST_F() implicitly register their tests with googletest. So, unlike with many other C++ testing frameworks, you don't have to re-list all your defined tests in order to run them.
TEST()和TEST_F() 用GoogleTest隱式注冊它們的測試爽哎。因此监憎,與許多其他C++測試框架不同汗茄,您不必重新列出所有定義的測試以便運行它們。
After defining your tests, you can run them with RUN_ALL_TESTS() , which returns 0 if all the tests are successful, or 1 otherwise. Note that RUN_ALL_TESTS() runs all tests in your link unit -- they can be from different test cases, or even different source files.
定義測試后,可以使用RUN_ALL_TESTS() 運行它們痹换,如果所有測試都成功醋虏,則返回0嫂易,否則返回1斧抱。
請注意常拓,RUN_ALL_TESTS() 運行鏈接單元中的所有測試——它們可以來自不同的測試用例,甚至是不同的源文件辉浦。
When invoked, the RUN_ALL_TESTS() macro:
當使用RUN_ALL_TESTS() 時, 以下過程會被執(zhí)行:
Saves the state of all googletest flags
保存所有GoogleTest標志的狀態(tài)Creates a test fixture object for the first test.
為第一個測試創(chuàng)建測試設備對象弄抬。Initializes it via SetUp().
通過Setup()初始化它。Runs the test on the fixture object.
在fixture對象上運行測試宪郊。Cleans up the fixture via TearDown().
通過TearDown()清理fixture掂恕。Deletes the fixture.
刪除fixture。Restores the state of all googletest flags
恢復所有GoogleTest標志的狀態(tài)Repeats the above steps for the next test, until all tests have run.
為下一個測試重復上述步驟弛槐,直到所有測試都運行完畢懊亡。
IMPORTANT: You must not ignore the return value of
RUN_ALL_TESTS()
, or you will get a compiler error. The rationale for this design is that the automated testing service determines whether a test has passed based on its exit code, not on its stdout/stderr output; thus yourmain()
function must return the value ofRUN_ALL_TESTS()
.
Also, you should callRUN_ALL_TESTS()
only once. Calling it more than once conflicts with some advanced googletest features (e.g. thread-safe death tests) and thus is not supported.
注意:RUN_ALL_TESTS()返回值不能被忽略,而且使用過程中只能被調(diào)用一次乎串。
當然在使用RUN_ALL_TESTS()之前要進行初始化操作店枣。
testing::InitGoogleTest(&argc, argv);
android代碼實例
工程路徑為:
androidCode/test/gtest_add
androidCode為源碼根目錄。
test.cpp:
#include "stdio.h"
#include "gtest/gtest.h"
int add_sum(int a, int b)
{
return a + b;
}
TEST(addsumTest, OneAddZeroInput) {
EXPECT_EQ(add_sum(1,0), 1);
}
TEST(addsumTest, addSomeInput) {
EXPECT_EQ(add_sum(1, 0), 1);
EXPECT_EQ(add_sum(2, 0), 2);
EXPECT_EQ(add_sum(3, 3), 6);
EXPECT_EQ(add_sum(8, 1024), 40320);
}
int main(int argc, char **argv){
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
Android.mk:
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := \
test.cpp
LOCAL_CFLAGS := \
-Wall
LOCAL_C_INCLUDES := \
$(LOCAL_PATH)/../../external/googletest/googletest/include
LOCAL_STATIC_LIBRARIES += libgtest libgtest_main
LOCAL_CLANG_CFLAGS += -Wno-error=unused-lambda-capture
LOCAL_MODULE:= bymanbu_test
include $(BUILD_EXECUTABLE)
include $(call all-makefiles-under, $(LOCAL_PATH))
編譯
進入源碼的根路徑:
$ source build/envsetup.sh
$ lunch aosp_arm64-eng
將工程添加到test/
下叹誉。
執(zhí)行mma
編譯:
OK鸯两,大功告成,可以放到真機上去運行了长豁,不過是native層甩卓,需要使用adb。O(∩_∩)O哈哈~