Googletest export

Add public entry point testing::RegisterTest.

PiperOrigin-RevId: 226350937
This commit is contained in:
Abseil Team
2018-12-20 12:54:19 -05:00
committed by Mark Barolak
parent 9494c45e75
commit a83cc11abe
4 changed files with 249 additions and 5 deletions

View File

@@ -12,7 +12,7 @@ Expected equality of these values:
3
Stack trace: (omitted)
[==========] Running 76 tests from 34 test cases.
[==========] Running 83 tests from 38 test cases.
[----------] Global test environment set-up.
FooEnvironment::SetUp() called.
BarEnvironment::SetUp() called.
@@ -870,6 +870,84 @@ Expected non-fatal failure.
Stack trace: (omitted)
[ FAILED ] ScopedFakeTestPartResultReporterTest.InterceptOnlyCurrentThread
[----------] 2 tests from DynamicFixture
DynamicFixture::SetUpTestCase
[ RUN ] DynamicFixture.DynamicTestPass
DynamicFixture()
DynamicFixture::SetUp
DynamicFixture::TearDown
~DynamicFixture()
[ OK ] DynamicFixture.DynamicTestPass
[ RUN ] DynamicFixture.DynamicTestFail
DynamicFixture()
DynamicFixture::SetUp
googletest-output-test_.cc:#: Failure
Value of: Pass
Actual: false
Expected: true
Stack trace: (omitted)
DynamicFixture::TearDown
~DynamicFixture()
[ FAILED ] DynamicFixture.DynamicTestFail
DynamicFixture::TearDownTestCase
[----------] 1 test from DynamicFixtureAnotherName
DynamicFixture::SetUpTestCase
[ RUN ] DynamicFixtureAnotherName.DynamicTestPass
DynamicFixture()
DynamicFixture::SetUp
DynamicFixture::TearDown
~DynamicFixture()
[ OK ] DynamicFixtureAnotherName.DynamicTestPass
DynamicFixture::TearDownTestCase
[----------] 2 tests from BadDynamicFixture1
DynamicFixture::SetUpTestCase
[ RUN ] BadDynamicFixture1.FixtureBase
DynamicFixture()
DynamicFixture::SetUp
DynamicFixture::TearDown
~DynamicFixture()
[ OK ] BadDynamicFixture1.FixtureBase
[ RUN ] BadDynamicFixture1.TestBase
DynamicFixture()
gtest.cc:#: Failure
Failed
All tests in the same test case must use the same test fixture
class, so mixing TEST_F and TEST in the same test case is
illegal. In test case BadDynamicFixture1,
test FixtureBase is defined using TEST_F but
test TestBase is defined using TEST. You probably
want to change the TEST to TEST_F or move it to another test
case.
Stack trace: (omitted)
~DynamicFixture()
[ FAILED ] BadDynamicFixture1.TestBase
DynamicFixture::TearDownTestCase
[----------] 2 tests from BadDynamicFixture2
DynamicFixture::SetUpTestCase
[ RUN ] BadDynamicFixture2.FixtureBase
DynamicFixture()
DynamicFixture::SetUp
DynamicFixture::TearDown
~DynamicFixture()
[ OK ] BadDynamicFixture2.FixtureBase
[ RUN ] BadDynamicFixture2.Derived
DynamicFixture()
gtest.cc:#: Failure
Failed
All tests in the same test case must use the same test fixture
class. However, in test case BadDynamicFixture2,
you defined test FixtureBase and test Derived
using two different test fixture classes. This can happen if
the two classes are from different namespaces or translation
units and have the same name. You should probably rename one
of the classes to put the tests into different test cases.
Stack trace: (omitted)
~DynamicFixture()
[ FAILED ] BadDynamicFixture2.Derived
DynamicFixture::TearDownTestCase
[----------] 1 test from PrintingFailingParams/FailingParamTest
[ RUN ] PrintingFailingParams/FailingParamTest.Fails/0
googletest-output-test_.cc:#: Failure
@@ -906,9 +984,9 @@ Failed
Expected fatal failure.
Stack trace: (omitted)
[==========] 76 tests from 34 test cases ran.
[ PASSED ] 26 tests.
[ FAILED ] 50 tests, listed below:
[==========] 83 tests from 38 test cases ran.
[ PASSED ] 30 tests.
[ FAILED ] 53 tests, listed below:
[ FAILED ] NonfatalFailureTest.EscapesStringOperands
[ FAILED ] NonfatalFailureTest.DiffForLongStrings
[ FAILED ] FatalFailureTest.FatalFailureInSubroutine
@@ -957,10 +1035,13 @@ Stack trace: (omitted)
[ FAILED ] ExpectFailureWithThreadsTest.ExpectFatalFailure
[ FAILED ] ExpectFailureWithThreadsTest.ExpectNonFatalFailure
[ FAILED ] ScopedFakeTestPartResultReporterTest.InterceptOnlyCurrentThread
[ FAILED ] DynamicFixture.DynamicTestFail
[ FAILED ] BadDynamicFixture1.TestBase
[ FAILED ] BadDynamicFixture2.Derived
[ FAILED ] PrintingFailingParams/FailingParamTest.Fails/0, where GetParam() = 2
[ FAILED ] PrintingStrings/ParamTest.Failure/a, where GetParam() = "a"
50 FAILED TESTS
53 FAILED TESTS
 YOU HAVE 1 DISABLED TEST
Note: Google Test filter = FatalFailureTest.*:LoggingTest.*

View File

@@ -1024,6 +1024,56 @@ TEST_F(ExpectFailureTest, ExpectNonFatalFailureOnAllThreads) {
"Some other non-fatal failure.");
}
class DynamicFixture : public testing::Test {
protected:
DynamicFixture() { printf("DynamicFixture()\n"); }
~DynamicFixture() override { printf("~DynamicFixture()\n"); }
void SetUp() override { printf("DynamicFixture::SetUp\n"); }
void TearDown() override { printf("DynamicFixture::TearDown\n"); }
static void SetUpTestCase() { printf("DynamicFixture::SetUpTestCase\n"); }
static void TearDownTestCase() {
printf("DynamicFixture::TearDownTestCase\n");
}
};
template <bool Pass>
class DynamicTest : public DynamicFixture {
public:
void TestBody() override { EXPECT_TRUE(Pass); }
};
auto dynamic_test = (
// Register two tests with the same fixture correctly.
testing::RegisterTest(
"DynamicFixture", "DynamicTestPass", nullptr, nullptr, __FILE__,
__LINE__, []() -> DynamicFixture* { return new DynamicTest<true>; }),
testing::RegisterTest(
"DynamicFixture", "DynamicTestFail", nullptr, nullptr, __FILE__,
__LINE__, []() -> DynamicFixture* { return new DynamicTest<false>; }),
// Register the same fixture with another name. That's fine.
testing::RegisterTest(
"DynamicFixtureAnotherName", "DynamicTestPass", nullptr, nullptr,
__FILE__, __LINE__,
[]() -> DynamicFixture* { return new DynamicTest<true>; }),
// Register two tests with the same fixture incorrectly.
testing::RegisterTest(
"BadDynamicFixture1", "FixtureBase", nullptr, nullptr, __FILE__,
__LINE__, []() -> DynamicFixture* { return new DynamicTest<true>; }),
testing::RegisterTest(
"BadDynamicFixture1", "TestBase", nullptr, nullptr, __FILE__, __LINE__,
[]() -> testing::Test* { return new DynamicTest<true>; }),
// Register two tests with the same fixture incorrectly by ommiting the
// return type.
testing::RegisterTest(
"BadDynamicFixture2", "FixtureBase", nullptr, nullptr, __FILE__,
__LINE__, []() -> DynamicFixture* { return new DynamicTest<true>; }),
testing::RegisterTest("BadDynamicFixture2", "Derived", nullptr, nullptr,
__FILE__, __LINE__,
[]() { return new DynamicTest<true>; }));
// Two test environments for testing testing::AddGlobalTestEnvironment().

View File

@@ -7547,3 +7547,30 @@ TEST_F(AdHocTestResultTest, AdHocTestResultTestForUnitTestDoesNotShowFailure) {
testing::UnitTest::GetInstance()->ad_hoc_test_result();
EXPECT_FALSE(test_result.Failed());
}
class DynamicUnitTestFixture : public testing::Test {};
class DynamicTest : public DynamicUnitTestFixture {
void TestBody() override { EXPECT_TRUE(true); }
};
auto* dynamic_test = testing::RegisterTest(
"DynamicUnitTestFixture", "DynamicTest", "TYPE", "VALUE", __FILE__,
__LINE__, []() -> DynamicUnitTestFixture* { return new DynamicTest; });
TEST(RegisterTest, WasRegistered) {
auto* unittest = testing::UnitTest::GetInstance();
for (int i = 0; i < unittest->total_test_case_count(); ++i) {
auto* tests = unittest->GetTestCase(i);
if (tests->name() != std::string("DynamicUnitTestFixture")) continue;
for (int j = 0; j < tests->total_test_count(); ++j) {
if (tests->GetTestInfo(j)->name() != std::string("DynamicTest")) continue;
// Found it.
EXPECT_STREQ(tests->GetTestInfo(j)->value_param(), "VALUE");
EXPECT_STREQ(tests->GetTestInfo(j)->type_param(), "TYPE");
return;
}
}
FAIL() << "Didn't find the test!";
}