googletest-failfast-unittest_.cc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // Copyright 2005, Google Inc.
  2. // All rights reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are
  6. // met:
  7. //
  8. // * Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above
  11. // copyright notice, this list of conditions and the following disclaimer
  12. // in the documentation and/or other materials provided with the
  13. // distribution.
  14. // * Neither the name of Google Inc. nor the names of its
  15. // contributors may be used to endorse or promote products derived from
  16. // this software without specific prior written permission.
  17. //
  18. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. // Unit test for Google Test test filters.
  30. //
  31. // A user can specify which test(s) in a Google Test program to run via
  32. // either the GTEST_FILTER environment variable or the --gtest_filter
  33. // flag. This is used for testing such functionality.
  34. //
  35. // The program will be invoked from a Python unit test. Don't run it
  36. // directly.
  37. #include "gtest/gtest.h"
  38. namespace {
  39. // Test HasFixtureTest.
  40. class HasFixtureTest : public testing::Test {};
  41. TEST_F(HasFixtureTest, Test0) {}
  42. TEST_F(HasFixtureTest, Test1) { FAIL() << "Expected failure."; }
  43. TEST_F(HasFixtureTest, Test2) { FAIL() << "Expected failure."; }
  44. TEST_F(HasFixtureTest, Test3) { FAIL() << "Expected failure."; }
  45. TEST_F(HasFixtureTest, Test4) { FAIL() << "Expected failure."; }
  46. // Test HasSimpleTest.
  47. TEST(HasSimpleTest, Test0) {}
  48. TEST(HasSimpleTest, Test1) { FAIL() << "Expected failure."; }
  49. TEST(HasSimpleTest, Test2) { FAIL() << "Expected failure."; }
  50. TEST(HasSimpleTest, Test3) { FAIL() << "Expected failure."; }
  51. TEST(HasSimpleTest, Test4) { FAIL() << "Expected failure."; }
  52. // Test HasDisabledTest.
  53. TEST(HasDisabledTest, Test0) {}
  54. TEST(HasDisabledTest, DISABLED_Test1) { FAIL() << "Expected failure."; }
  55. TEST(HasDisabledTest, Test2) { FAIL() << "Expected failure."; }
  56. TEST(HasDisabledTest, Test3) { FAIL() << "Expected failure."; }
  57. TEST(HasDisabledTest, Test4) { FAIL() << "Expected failure."; }
  58. // Test HasDeathTest
  59. TEST(HasDeathTest, Test0) { EXPECT_DEATH_IF_SUPPORTED(exit(1), ".*"); }
  60. TEST(HasDeathTest, Test1) {
  61. EXPECT_DEATH_IF_SUPPORTED(FAIL() << "Expected failure.", ".*");
  62. }
  63. TEST(HasDeathTest, Test2) {
  64. EXPECT_DEATH_IF_SUPPORTED(FAIL() << "Expected failure.", ".*");
  65. }
  66. TEST(HasDeathTest, Test3) {
  67. EXPECT_DEATH_IF_SUPPORTED(FAIL() << "Expected failure.", ".*");
  68. }
  69. TEST(HasDeathTest, Test4) {
  70. EXPECT_DEATH_IF_SUPPORTED(FAIL() << "Expected failure.", ".*");
  71. }
  72. // Test DISABLED_HasDisabledSuite
  73. TEST(DISABLED_HasDisabledSuite, Test0) {}
  74. TEST(DISABLED_HasDisabledSuite, Test1) { FAIL() << "Expected failure."; }
  75. TEST(DISABLED_HasDisabledSuite, Test2) { FAIL() << "Expected failure."; }
  76. TEST(DISABLED_HasDisabledSuite, Test3) { FAIL() << "Expected failure."; }
  77. TEST(DISABLED_HasDisabledSuite, Test4) { FAIL() << "Expected failure."; }
  78. // Test HasParametersTest
  79. class HasParametersTest : public testing::TestWithParam<int> {};
  80. TEST_P(HasParametersTest, Test1) { FAIL() << "Expected failure."; }
  81. TEST_P(HasParametersTest, Test2) { FAIL() << "Expected failure."; }
  82. INSTANTIATE_TEST_SUITE_P(HasParametersSuite, HasParametersTest,
  83. testing::Values(1, 2));
  84. class MyTestListener : public ::testing::EmptyTestEventListener {
  85. void OnTestSuiteStart(const ::testing::TestSuite& test_suite) override {
  86. printf("We are in OnTestSuiteStart of %s.\n", test_suite.name());
  87. }
  88. void OnTestStart(const ::testing::TestInfo& test_info) override {
  89. printf("We are in OnTestStart of %s.%s.\n", test_info.test_suite_name(),
  90. test_info.name());
  91. }
  92. void OnTestPartResult(
  93. const ::testing::TestPartResult& test_part_result) override {
  94. printf("We are in OnTestPartResult %s:%d.\n", test_part_result.file_name(),
  95. test_part_result.line_number());
  96. }
  97. void OnTestEnd(const ::testing::TestInfo& test_info) override {
  98. printf("We are in OnTestEnd of %s.%s.\n", test_info.test_suite_name(),
  99. test_info.name());
  100. }
  101. void OnTestSuiteEnd(const ::testing::TestSuite& test_suite) override {
  102. printf("We are in OnTestSuiteEnd of %s.\n", test_suite.name());
  103. }
  104. };
  105. TEST(HasSkipTest, Test0) { SUCCEED() << "Expected success."; }
  106. TEST(HasSkipTest, Test1) { GTEST_SKIP() << "Expected skip."; }
  107. TEST(HasSkipTest, Test2) { FAIL() << "Expected failure."; }
  108. TEST(HasSkipTest, Test3) { FAIL() << "Expected failure."; }
  109. TEST(HasSkipTest, Test4) { FAIL() << "Expected failure."; }
  110. } // namespace
  111. int main(int argc, char **argv) {
  112. ::testing::InitGoogleTest(&argc, argv);
  113. ::testing::UnitTest::GetInstance()->listeners().Append(new MyTestListener());
  114. return RUN_ALL_TESTS();
  115. }