googletest:sample9分析
目录
googletest:sample1
googletest:sample2
googletest:sample3
googletest:sample4
googletest:sample5
googletest:sample6
googletest:sample7
googletest:sample8
googletest:sample9
测试文件
GoogleTest提供了一套event listener API,sample9展示如何用这套API,实现替代console输出,以及如何用UnitTest反射API 枚举 test suites和test并检查其结果.
必须继承自testing::TestEventListener 或 testing::EmptyEventListener,前者是一个纯抽象类,必须实现所有接口,后者只需要重写所关心的接口.
当一个事件触发时,它的上下文(context)作为参数送入处理函数.
- UnitTest 反映了整个测试程序的状态
- TestSuite 测试集的状态
- TestInfo 具体测试的状态
- TestPartResult 测试结果
如何使用Event Listener监听test?
- 定义一个Event Listener
// Provides alternative output mode which produces minimal amount of
// information about tests.
class TersePrinter : public EmptyTestEventListener {
private:
// Called before any test activity starts.
void OnTestProgramStart(const UnitTest& /* unit_test */) override {}
// Called after all test activities have ended.
void OnTestProgramEnd(const UnitTest& unit_test) override {
fprintf(stdout, "TEST %s\n", unit_test.Passed() ? "PASSED" : "FAILED");
fflush(stdout);
}
// Called before a test starts.
void OnTestStart(const TestInfo& test_info) override {
fprintf(stdout, "*** Test %s.%s starting.\n", test_info.test_suite_name(),
test_info.name());
fflush(stdout);
}
// Called after a failed assertion or a SUCCEED() invocation.
void OnTestPartResult(const TestPartResult& test_part_result) override {
fprintf(stdout, "%s in %s:%d\n%s\n",
test_part_result.failed() ? "*** Failure" : "Success",
test_part_result.file_name(), test_part_result.line_number(),
test_part_result.summary());
fflush(stdout);
}
// Called after a test ends.
void OnTestEnd(const TestInfo& test_info) override {
fprintf(stdout, "*** Test %s.%s ending.\n", test_info.test_suite_name(),
test_info.name());
fflush(stdout);
}
}; // class TersePrinter
- 注册Event Listener
main函数中,用listeners.Append()
将自定义监听器注册到GoogleTest中.
int main(int argc, char** argv) {
InitGoogleTest(&argc, argv);
bool terse_output = false;
if (argc > 1 && strcmp(argv[1], "--terse_output") == 0)
terse_output = true;
else
printf("%s\n",
"Run this program with --terse_output to change the way "
"it prints its output.");
UnitTest& unit_test = *UnitTest::GetInstance();
// If we are given the --terse_output command line flag, suppresses the
// standard output and attaches own result printer.
if (terse_output) {
TestEventListeners& listeners = unit_test.listeners();
// Removes the default console output listener from the list so it will
// not receive events from Google Test and won't print any output. Since
// this operation transfers ownership of the listener to the caller we
// have to delete it as well.
delete listeners.Release(listeners.default_result_printer());
// Adds the custom output listener to the list. It will now receive
// events from Google Test and print the alternative output. We don't
// have to worry about deleting it since Google Test assumes ownership
// over it after adding it to the list.
listeners.Append(new TersePrinter);
}
...
}
注意:GTest本身有默认的处理函数,需要将其关闭
delete listeners.Release(listeners.default_result_printer());
- 设计test
下面是用户自定义的test
TEST(CustomOutputTest, PrintsMessage) {
printf("Printing something from the test body...\n");
}
TEST(CustomOutputTest, Succeeds) {
SUCCEED() << "SUCCEED() has been invoked from here";
}
TEST(CustomOutputTest, Fails) {
EXPECT_EQ(1, 2)
<< "This test fails in order to demonstrate alternative failure messages";
}
- 运行test
执行RUN_ALL_TESTS()
,运行所有test.
int main(int argc, char** argv) {
// 注册Event Listner
...
RUN_ALL_TESTS();
...
}
如何通过反射API枚举test suite及获取test检查结果?
1)获取 UnitTest 实例
用testing::UnitTest::GetInstance()
获取当前测试程序的UnitTest实例.
2)遍历test suite
用UnitTest::total_test_suite_count()
获取test suite的总数,并通过 UnitTest::GetTestSuite(i) 获取每个test suite.
3)遍历test
对于每个test suite,使用TestSuite::total_test_count()
获取test的总数,并通过 TestSuite::GetTestInfo(j)
获取每个test执行信息,如执行结果,test名称等信息.
4)输出test suite, test信息
如:
test_suite->name()
返回test suite的名称;
test_info->name()
返回test的名称.
int main(int argc, char* argv[]) {
UnitTest& unit_test = *UnitTest::GetInstance();
...
// This is an example of using the UnitTest reflection API to inspect test
// results. Here we discount failures from the tests we expected to fail.
int unexpectedly_failed_tests = 0;
for (int i = 0; i < unit_test.total_test_suite_count(); ++i) {
const testing::TestSuite& test_suite = *unit_test.GetTestSuite(i);
for (int j = 0; j < test_suite.total_test_count(); ++j) {
const TestInfo& test_info = *test_suite.GetTestInfo(j);
// Counts failed tests that were not meant to fail (those without
// 'Fails' in the name).
if (test_info.result()->Failed() &&
strcmp(test_info.name(), "Fails") != 0) {
unexpectedly_failed_tests++;
}
}
}
// Test that were meant to fail should not affect the test program outcome.
if (unexpectedly_failed_tests == 0) ret_val = 0;
...
}
sample9 unittest运行结果:
Run this program with --terse_output to change the way it prints its output.
[==========] Running 3 tests from 1 test suite.
[----------] Global test environment set-up.
[----------] 3 tests from CustomOutputTest
[ RUN ] CustomOutputTest.PrintsMessage
Printing something from the test body...
[ OK ] CustomOutputTest.PrintsMessage (0 ms)
[ RUN ] CustomOutputTest.Succeeds
[ OK ] CustomOutputTest.Succeeds (1 ms)
[ RUN ] CustomOutputTest.Fails
D:\Programs\googletest\googletest-main\googletest\samples\sample9_unittest.cc(91): error: Expected equality of these values:
1
2
This test fails in order to demonstrate alternative failure messages
[ FAILED ] CustomOutputTest.Fails (0 ms)
[----------] 3 tests from CustomOutputTest (2 ms total)
[----------] Global test environment tear-down
[==========] 3 tests from 1 test suite ran. (5 ms total)
[ PASSED ] 2 tests.
[ FAILED ] 1 test, listed below:
[ FAILED ] CustomOutputTest.Fails
1 FAILED TEST