Googletest export
Add public entry point testing::RegisterTest. PiperOrigin-RevId: 226350937
This commit is contained in:
parent
9494c45e75
commit
a83cc11abe
@ -2354,6 +2354,92 @@ GTEST_API_ std::string TempDir();
|
||||
# pragma warning(pop)
|
||||
#endif
|
||||
|
||||
// Dynamically registers a test with the framework.
|
||||
//
|
||||
// This is an advanced API only to be used when the `TEST` macros are
|
||||
// insufficient. The macros should be preferred when possible, as they avoid
|
||||
// most of the complexity of calling this function.
|
||||
//
|
||||
// The `factory` argument is a factory callable (move-constructible) object or
|
||||
// function pointer that creates a new instance of the Test object. It
|
||||
// handles ownership to the caller. The signature of the callable is
|
||||
// `Fixture*()`, where `Fixture` is the test fixture class for the test. All
|
||||
// tests registered with the same `test_case_name` must return the same
|
||||
// fixture type. This is checked at runtime.
|
||||
//
|
||||
// The framework will infer the fixture class from the factory and will call
|
||||
// the `SetUpTestCase` and `TearDownTestCase` for it.
|
||||
//
|
||||
// Must be called before `RUN_ALL_TESTS()` is invoked, otherwise behavior is
|
||||
// undefined.
|
||||
//
|
||||
// Use case example:
|
||||
//
|
||||
// class MyFixture : public ::testing::Test {
|
||||
// public:
|
||||
// // All of these optional, just like in regular macro usage.
|
||||
// static void SetUpTestCase() { ... }
|
||||
// static void TearDownTestCase() { ... }
|
||||
// void SetUp() override { ... }
|
||||
// void TearDown() override { ... }
|
||||
// };
|
||||
//
|
||||
// class MyTest : public MyFixture {
|
||||
// public:
|
||||
// explicit MyTest(int data) : data_(data) {}
|
||||
// void TestBody() override { ... }
|
||||
//
|
||||
// private:
|
||||
// int data_;
|
||||
// };
|
||||
//
|
||||
// void RegisterMyTests(const std::vector<int>& values) {
|
||||
// for (int v : values) {
|
||||
// ::testing::RegisterTest(
|
||||
// "MyFixture", ("Test" + std::to_string(v)).c_str(), nullptr,
|
||||
// std::to_string(v).c_str(),
|
||||
// __FILE__, __LINE__,
|
||||
// // Important to use the fixture type as the return type here.
|
||||
// [=]() -> MyFixture* { return new MyTest(v); });
|
||||
// }
|
||||
// }
|
||||
// ...
|
||||
// int main(int argc, char** argv) {
|
||||
// std::vector<int> values_to_test = LoadValuesFromConfig();
|
||||
// RegisterMyTests(values_to_test);
|
||||
// ...
|
||||
// return RUN_ALL_TESTS();
|
||||
// }
|
||||
//
|
||||
template <int&... ExplicitParameterBarrier, typename Factory>
|
||||
TestInfo* RegisterTest(const char* test_case_name, const char* test_name,
|
||||
const char* type_param, const char* value_param,
|
||||
const char* file, int line, Factory factory) {
|
||||
using TestT = typename std::remove_pointer<decltype(factory())>::type;
|
||||
|
||||
// Helper class to get SetUpTestCase and TearDownTestCase when they are in a
|
||||
// protected scope.
|
||||
struct Helper : TestT {
|
||||
using TestT::SetUpTestCase;
|
||||
using TestT::TearDownTestCase;
|
||||
};
|
||||
|
||||
class FactoryImpl : public internal::TestFactoryBase {
|
||||
public:
|
||||
explicit FactoryImpl(Factory f) : factory_(std::move(f)) {}
|
||||
Test* CreateTest() override { return factory_(); }
|
||||
|
||||
private:
|
||||
Factory factory_;
|
||||
};
|
||||
|
||||
return internal::MakeAndRegisterTestInfo(
|
||||
test_case_name, test_name, type_param, value_param,
|
||||
internal::CodeLocation(file, line), internal::GetTypeId<TestT>(),
|
||||
&Helper::SetUpTestCase, &Helper::TearDownTestCase,
|
||||
new FactoryImpl{std::move(factory)});
|
||||
}
|
||||
|
||||
} // namespace testing
|
||||
|
||||
// Use this function in main() to run all tests. It returns 0 if all
|
||||
|
@ -12,7 +12,7 @@ Expected equality of these values:
|
||||
3
|
||||
Stack trace: (omitted)
|
||||
|
||||
[0;32m[==========] [mRunning 76 tests from 34 test cases.
|
||||
[0;32m[==========] [mRunning 83 tests from 38 test cases.
|
||||
[0;32m[----------] [mGlobal test environment set-up.
|
||||
FooEnvironment::SetUp() called.
|
||||
BarEnvironment::SetUp() called.
|
||||
@ -870,6 +870,84 @@ Expected non-fatal failure.
|
||||
Stack trace: (omitted)
|
||||
|
||||
[0;31m[ FAILED ] [mScopedFakeTestPartResultReporterTest.InterceptOnlyCurrentThread
|
||||
[0;32m[----------] [m2 tests from DynamicFixture
|
||||
DynamicFixture::SetUpTestCase
|
||||
[0;32m[ RUN ] [mDynamicFixture.DynamicTestPass
|
||||
DynamicFixture()
|
||||
DynamicFixture::SetUp
|
||||
DynamicFixture::TearDown
|
||||
~DynamicFixture()
|
||||
[0;32m[ OK ] [mDynamicFixture.DynamicTestPass
|
||||
[0;32m[ RUN ] [mDynamicFixture.DynamicTestFail
|
||||
DynamicFixture()
|
||||
DynamicFixture::SetUp
|
||||
googletest-output-test_.cc:#: Failure
|
||||
Value of: Pass
|
||||
Actual: false
|
||||
Expected: true
|
||||
Stack trace: (omitted)
|
||||
|
||||
DynamicFixture::TearDown
|
||||
~DynamicFixture()
|
||||
[0;31m[ FAILED ] [mDynamicFixture.DynamicTestFail
|
||||
DynamicFixture::TearDownTestCase
|
||||
[0;32m[----------] [m1 test from DynamicFixtureAnotherName
|
||||
DynamicFixture::SetUpTestCase
|
||||
[0;32m[ RUN ] [mDynamicFixtureAnotherName.DynamicTestPass
|
||||
DynamicFixture()
|
||||
DynamicFixture::SetUp
|
||||
DynamicFixture::TearDown
|
||||
~DynamicFixture()
|
||||
[0;32m[ OK ] [mDynamicFixtureAnotherName.DynamicTestPass
|
||||
DynamicFixture::TearDownTestCase
|
||||
[0;32m[----------] [m2 tests from BadDynamicFixture1
|
||||
DynamicFixture::SetUpTestCase
|
||||
[0;32m[ RUN ] [mBadDynamicFixture1.FixtureBase
|
||||
DynamicFixture()
|
||||
DynamicFixture::SetUp
|
||||
DynamicFixture::TearDown
|
||||
~DynamicFixture()
|
||||
[0;32m[ OK ] [mBadDynamicFixture1.FixtureBase
|
||||
[0;32m[ RUN ] [mBadDynamicFixture1.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()
|
||||
[0;31m[ FAILED ] [mBadDynamicFixture1.TestBase
|
||||
DynamicFixture::TearDownTestCase
|
||||
[0;32m[----------] [m2 tests from BadDynamicFixture2
|
||||
DynamicFixture::SetUpTestCase
|
||||
[0;32m[ RUN ] [mBadDynamicFixture2.FixtureBase
|
||||
DynamicFixture()
|
||||
DynamicFixture::SetUp
|
||||
DynamicFixture::TearDown
|
||||
~DynamicFixture()
|
||||
[0;32m[ OK ] [mBadDynamicFixture2.FixtureBase
|
||||
[0;32m[ RUN ] [mBadDynamicFixture2.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()
|
||||
[0;31m[ FAILED ] [mBadDynamicFixture2.Derived
|
||||
DynamicFixture::TearDownTestCase
|
||||
[0;32m[----------] [m1 test from PrintingFailingParams/FailingParamTest
|
||||
[0;32m[ RUN ] [mPrintingFailingParams/FailingParamTest.Fails/0
|
||||
googletest-output-test_.cc:#: Failure
|
||||
@ -906,9 +984,9 @@ Failed
|
||||
Expected fatal failure.
|
||||
Stack trace: (omitted)
|
||||
|
||||
[0;32m[==========] [m76 tests from 34 test cases ran.
|
||||
[0;32m[ PASSED ] [m26 tests.
|
||||
[0;31m[ FAILED ] [m50 tests, listed below:
|
||||
[0;32m[==========] [m83 tests from 38 test cases ran.
|
||||
[0;32m[ PASSED ] [m30 tests.
|
||||
[0;31m[ FAILED ] [m53 tests, listed below:
|
||||
[0;31m[ FAILED ] [mNonfatalFailureTest.EscapesStringOperands
|
||||
[0;31m[ FAILED ] [mNonfatalFailureTest.DiffForLongStrings
|
||||
[0;31m[ FAILED ] [mFatalFailureTest.FatalFailureInSubroutine
|
||||
@ -957,10 +1035,13 @@ Stack trace: (omitted)
|
||||
[0;31m[ FAILED ] [mExpectFailureWithThreadsTest.ExpectFatalFailure
|
||||
[0;31m[ FAILED ] [mExpectFailureWithThreadsTest.ExpectNonFatalFailure
|
||||
[0;31m[ FAILED ] [mScopedFakeTestPartResultReporterTest.InterceptOnlyCurrentThread
|
||||
[0;31m[ FAILED ] [mDynamicFixture.DynamicTestFail
|
||||
[0;31m[ FAILED ] [mBadDynamicFixture1.TestBase
|
||||
[0;31m[ FAILED ] [mBadDynamicFixture2.Derived
|
||||
[0;31m[ FAILED ] [mPrintingFailingParams/FailingParamTest.Fails/0, where GetParam() = 2
|
||||
[0;31m[ FAILED ] [mPrintingStrings/ParamTest.Failure/a, where GetParam() = "a"
|
||||
|
||||
50 FAILED TESTS
|
||||
53 FAILED TESTS
|
||||
[0;33m YOU HAVE 1 DISABLED TEST
|
||||
|
||||
[mNote: Google Test filter = FatalFailureTest.*:LoggingTest.*
|
||||
|
@ -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().
|
||||
|
||||
|
@ -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!";
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user