sample9_unittest.cc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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::TestSuite;
  40. using ::testing::UnitTest;
  41. namespace {
  42. // Provides alternative output mode which produces minimal amount of
  43. // information about tests.
  44. class TersePrinter : public EmptyTestEventListener {
  45. private:
  46. // Called before any test activity starts.
  47. void OnTestProgramStart(const UnitTest& /* unit_test */) override {}
  48. // Called after all test activities have ended.
  49. void OnTestProgramEnd(const UnitTest& unit_test) override {
  50. fprintf(stdout, "TEST %s\n", unit_test.Passed() ? "PASSED" : "FAILED");
  51. fflush(stdout);
  52. }
  53. // Called before a test starts.
  54. void OnTestStart(const TestInfo& test_info) override {
  55. fprintf(stdout, "*** Test %s.%s starting.\n", test_info.test_suite_name(),
  56. test_info.name());
  57. fflush(stdout);
  58. }
  59. // Called after a failed assertion or a SUCCEED() invocation.
  60. void OnTestPartResult(const TestPartResult& test_part_result) override {
  61. fprintf(stdout, "%s in %s:%d\n%s\n",
  62. test_part_result.failed() ? "*** Failure" : "Success",
  63. test_part_result.file_name(), test_part_result.line_number(),
  64. test_part_result.summary());
  65. fflush(stdout);
  66. }
  67. // Called after a test ends.
  68. void OnTestEnd(const TestInfo& test_info) override {
  69. fprintf(stdout, "*** Test %s.%s ending.\n", test_info.test_suite_name(),
  70. test_info.name());
  71. fflush(stdout);
  72. }
  73. }; // class TersePrinter
  74. TEST(CustomOutputTest, PrintsMessage) {
  75. printf("Printing something from the test body...\n");
  76. }
  77. TEST(CustomOutputTest, Succeeds) {
  78. SUCCEED() << "SUCCEED() has been invoked from here";
  79. }
  80. TEST(CustomOutputTest, Fails) {
  81. EXPECT_EQ(1, 2)
  82. << "This test fails in order to demonstrate alternative failure messages";
  83. }
  84. } // namespace
  85. int main(int argc, char** argv) {
  86. InitGoogleTest(&argc, argv);
  87. bool terse_output = false;
  88. if (argc > 1 && strcmp(argv[1], "--terse_output") == 0)
  89. terse_output = true;
  90. else
  91. printf("%s\n",
  92. "Run this program with --terse_output to change the way "
  93. "it prints its output.");
  94. UnitTest& unit_test = *UnitTest::GetInstance();
  95. // If we are given the --terse_output command line flag, suppresses the
  96. // standard output and attaches own result printer.
  97. if (terse_output) {
  98. TestEventListeners& listeners = unit_test.listeners();
  99. // Removes the default console output listener from the list so it will
  100. // not receive events from Google Test and won't print any output. Since
  101. // this operation transfers ownership of the listener to the caller we
  102. // have to delete it as well.
  103. delete listeners.Release(listeners.default_result_printer());
  104. // Adds the custom output listener to the list. It will now receive
  105. // events from Google Test and print the alternative output. We don't
  106. // have to worry about deleting it since Google Test assumes ownership
  107. // over it after adding it to the list.
  108. listeners.Append(new TersePrinter);
  109. }
  110. int ret_val = RUN_ALL_TESTS();
  111. // This is an example of using the UnitTest reflection API to inspect test
  112. // results. Here we discount failures from the tests we expected to fail.
  113. int unexpectedly_failed_tests = 0;
  114. for (int i = 0; i < unit_test.total_test_suite_count(); ++i) {
  115. const testing::TestSuite& test_suite = *unit_test.GetTestSuite(i);
  116. for (int j = 0; j < test_suite.total_test_count(); ++j) {
  117. const TestInfo& test_info = *test_suite.GetTestInfo(j);
  118. // Counts failed tests that were not meant to fail (those without
  119. // 'Fails' in the name).
  120. if (test_info.result()->Failed() &&
  121. strcmp(test_info.name(), "Fails") != 0) {
  122. unexpectedly_failed_tests++;
  123. }
  124. }
  125. }
  126. // Test that were meant to fail should not affect the test program outcome.
  127. if (unexpectedly_failed_tests == 0) ret_val = 0;
  128. return ret_val;
  129. }