From 711ba914d00dfafee1d00978c3ca330d27a6454c Mon Sep 17 00:00:00 2001 From: Bhaskar Mahajan Date: Wed, 28 Feb 2024 12:53:23 +0530 Subject: [PATCH 1/2] Run all tests for current test class/fixture/suite at once --- Bundle/GoogleTests.mm | 59 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 50 insertions(+), 9 deletions(-) diff --git a/Bundle/GoogleTests.mm b/Bundle/GoogleTests.mm index 069a123..f0357bb 100644 --- a/Bundle/GoogleTests.mm +++ b/Bundle/GoogleTests.mm @@ -51,6 +51,20 @@ */ static NSDictionary *GoogleTestFilterMap; +/** + * This will contain all the tests that should be run for current class. + * Ex - "Class.Test1:Class.Test2:Class.Test3". (format which Gtest understands) + */ +static NSString *finalFilter; +static int filterCount; + +/** + * Run all the selected tests for a given Gtest Class (Fixture / Suite). + * Only selected tests (in run window) will run. + * Name of all the tests will be part of @finalFilter string. + */ +static void RunAllTestsForCurrentClass(id self); + /** * A Google Test listener that reports failures to XCTest. */ @@ -134,30 +148,57 @@ + (NSArray *)testInvocations { return invocations; } +/** + *Run the tests in teardown method. + *By now all the name of all the tests for current class will be part of @finalFilter + */ ++ (void)tearDown{ + RunAllTestsForCurrentClass(self); + // Reset global filter for next run. + finalFilter = @""; + filterCount = 0; +} + @end /** - * Runs a single test. + * Runs all filtered tests for current class */ -static void RunTest(id self, SEL _cmd) { +static void RunAllTestsForCurrentClass(id self){ XCTestListener *listener = new XCTestListener(self); UnitTest *googleTest = UnitTest::GetInstance(); googleTest->listeners().Append(listener); - - NSString *testKey = [NSString stringWithFormat:@"%@.%@", [self class], NSStringFromSelector(_cmd)]; - NSString *testFilter = GoogleTestFilterMap[testKey]; - XCTAssertNotNil(testFilter, @"No test filter found for test %@", testKey); - - testing::GTEST_FLAG(filter) = [testFilter UTF8String]; + + testing::GTEST_FLAG(filter) = [finalFilter UTF8String]; (void)RUN_ALL_TESTS(); delete googleTest->listeners().Release(listener); int totalTestsRun = googleTest->successful_test_count() + googleTest->failed_test_count(); - XCTAssertEqual(totalTestsRun, 1, @"Expected to run a single test for filter \"%@\"", testFilter); + XCTAssertEqual(totalTestsRun, filterCount, @"Expected to run %d test(s) for filters \"%@\"", filterCount, finalFilter); + +} + +/** + * Runs a single test. + */ +static void RunTest(id self, SEL _cmd) { + + NSString *testKey = [NSString stringWithFormat:@"%@.%@", [self class], NSStringFromSelector(_cmd)]; + NSString *testFilter = GoogleTestFilterMap[testKey]; + XCTAssertNotNil(testFilter, @"No test filter found for test %@", testKey); + + // Save all the tests for current class, in format supported by Gtest + if(finalFilter.length){ + finalFilter = [NSString stringWithFormat:@"%s:%s", finalFilter.UTF8String, testFilter.UTF8String]; + }else{ + finalFilter = testFilter; + } + filterCount++; } + @implementation GoogleTestLoader /** From 0a63e5a204540677f546cff424aaaa83b365778e Mon Sep 17 00:00:00 2001 From: mbhaskar98 Date: Wed, 28 Feb 2024 13:00:01 +0530 Subject: [PATCH 2/2] Update GoogleTests.mm --- Bundle/GoogleTests.mm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Bundle/GoogleTests.mm b/Bundle/GoogleTests.mm index f0357bb..a6ec9e4 100644 --- a/Bundle/GoogleTests.mm +++ b/Bundle/GoogleTests.mm @@ -150,7 +150,7 @@ + (NSArray *)testInvocations { /** *Run the tests in teardown method. - *By now all the name of all the tests for current class will be part of @finalFilter + *By now the name of all the tests for current class will be part of @finalFilter */ + (void)tearDown{ RunAllTestsForCurrentClass(self);