sample9_unittest.cc 5.8 KB

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