sample9_unittest.cc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // Copyright 2009 Google Inc. All Rights Reserved.
  2. //
  3. // Redistribution and use in source and binary forms, with or without
  4. // modification, are permitted provided that the following conditions are
  5. // met:
  6. //
  7. // * Redistributions of source code must retain the above copyright
  8. // notice, this list of conditions and the following disclaimer.
  9. // * Redistributions in binary form must reproduce the above
  10. // copyright notice, this list of conditions and the following disclaimer
  11. // in the documentation and/or other materials provided with the
  12. // distribution.
  13. // * Neither the name of Google Inc. nor the names of its
  14. // contributors may be used to endorse or promote products derived from
  15. // this software without specific prior written permission.
  16. //
  17. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  20. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  21. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  22. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  23. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  24. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  25. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  27. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. // This sample shows how to use Google Test listener API to implement
  29. // an alternative console output and how to use the UnitTest reflection API
  30. // to enumerate test suites and tests and to inspect their results.
  31. #include <stdio.h>
  32. #include "gtest/gtest.h"
  33. using ::testing::EmptyTestEventListener;
  34. using ::testing::InitGoogleTest;
  35. using ::testing::Test;
  36. using ::testing::TestEventListeners;
  37. using ::testing::TestInfo;
  38. using ::testing::TestPartResult;
  39. using ::testing::UnitTest;
  40. namespace {
  41. // Provides alternative output mode which produces minimal amount of
  42. // information about tests.
  43. class TersePrinter : public EmptyTestEventListener {
  44. private:
  45. // Called before any test activity starts.
  46. void OnTestProgramStart(const UnitTest& /* unit_test */) override {}
  47. // Called after all test activities have ended.
  48. void OnTestProgramEnd(const UnitTest& unit_test) override {
  49. fprintf(stdout, "TEST %s\n", unit_test.Passed() ? "PASSED" : "FAILED");
  50. fflush(stdout);
  51. }
  52. // Called before a test starts.
  53. void OnTestStart(const TestInfo& test_info) override {
  54. fprintf(stdout, "*** Test %s.%s starting.\n", test_info.test_suite_name(),
  55. test_info.name());
  56. fflush(stdout);
  57. }
  58. // Called after a failed assertion or a SUCCEED() invocation.
  59. void OnTestPartResult(const TestPartResult& test_part_result) override {
  60. fprintf(stdout, "%s in %s:%d\n%s\n",
  61. test_part_result.failed() ? "*** Failure" : "Success",
  62. test_part_result.file_name(), test_part_result.line_number(),
  63. test_part_result.summary());
  64. fflush(stdout);
  65. }
  66. // Called after a test ends.
  67. void OnTestEnd(const TestInfo& test_info) override {
  68. fprintf(stdout, "*** Test %s.%s ending.\n", test_info.test_suite_name(),
  69. test_info.name());
  70. fflush(stdout);
  71. }
  72. }; // class TersePrinter
  73. TEST(CustomOutputTest, PrintsMessage) {
  74. printf("Printing something from the test body...\n");
  75. }
  76. TEST(CustomOutputTest, Succeeds) {
  77. SUCCEED() << "SUCCEED() has been invoked from here";
  78. }
  79. TEST(CustomOutputTest, Fails) {
  80. EXPECT_EQ(1, 2)
  81. << "This test fails in order to demonstrate alternative failure messages";
  82. }
  83. } // namespace
  84. int main(int argc, char** argv) {
  85. InitGoogleTest(&argc, argv);
  86. bool terse_output = false;
  87. if (argc > 1 && strcmp(argv[1], "--terse_output") == 0)
  88. terse_output = true;
  89. else
  90. printf("%s\n",
  91. "Run this program with --terse_output to change the way "
  92. "it prints its output.");
  93. UnitTest& unit_test = *UnitTest::GetInstance();
  94. // If we are given the --terse_output command line flag, suppresses the
  95. // standard output and attaches own result printer.
  96. if (terse_output) {
  97. TestEventListeners& listeners = unit_test.listeners();
  98. // Removes the default console output listener from the list so it will
  99. // not receive events from Google Test and won't print any output. Since
  100. // this operation transfers ownership of the listener to the caller we
  101. // have to delete it as well.
  102. delete listeners.Release(listeners.default_result_printer());
  103. // Adds the custom output listener to the list. It will now receive
  104. // events from Google Test and print the alternative output. We don't
  105. // have to worry about deleting it since Google Test assumes ownership
  106. // over it after adding it to the list.
  107. listeners.Append(new TersePrinter);
  108. }
  109. int ret_val = RUN_ALL_TESTS();
  110. // This is an example of using the UnitTest reflection API to inspect test
  111. // results. Here we discount failures from the tests we expected to fail.
  112. int unexpectedly_failed_tests = 0;
  113. for (int i = 0; i < unit_test.total_test_suite_count(); ++i) {
  114. const testing::TestSuite& test_suite = *unit_test.GetTestSuite(i);
  115. for (int j = 0; j < test_suite.total_test_count(); ++j) {
  116. const TestInfo& test_info = *test_suite.GetTestInfo(j);
  117. // Counts failed tests that were not meant to fail (those without
  118. // 'Fails' in the name).
  119. if (test_info.result()->Failed() &&
  120. strcmp(test_info.name(), "Fails") != 0) {
  121. unexpectedly_failed_tests++;
  122. }
  123. }
  124. }
  125. // Test that were meant to fail should not affect the test program outcome.
  126. if (unexpectedly_failed_tests == 0) ret_val = 0;
  127. return ret_val;
  128. }