You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

156 lines
5.8 KiB

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