Skip to content

Instantly share code, notes, and snippets.

@NxSoftware
Created January 17, 2015 11:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NxSoftware/e7eff738181ca4b702d5 to your computer and use it in GitHub Desktop.
Save NxSoftware/e7eff738181ca4b702d5 to your computer and use it in GitHub Desktop.
A somewhat meta test case for testing whether the specified prefixed classes have matching unit test case classes.
#import <UIKit/UIKit.h>
#import <XCTest/XCTest.h>
#import <objc/runtime.h>
@interface UnitTestCoverageTests : XCTestCase
@end
@implementation UnitTestCoverageTests
- (void)testClassCoverage {
int numberOfClasses = objc_getClassList(NULL, 0);
if (numberOfClasses > 0)
{
NSString *const prefixForClassThatShouldHaveTest = @"SW";
NSString *const testClassSuffix = @"Tests";
Class *classes = (__unsafe_unretained Class *)malloc(sizeof(Class) * numberOfClasses);
numberOfClasses = objc_getClassList(classes, numberOfClasses);
NSMutableArray *classesThatShouldHaveTests = [NSMutableArray array];
NSMutableArray *testClasses = [NSMutableArray array];
for (unsigned int i = 0; i < numberOfClasses; ++i)
{
Class cls = classes[i];
NSString *className = [NSString stringWithFormat:@"%s", class_getName(cls)];
if ([className hasPrefix:prefixForClassThatShouldHaveTest])
{
// Is this a test case? (Naively based on class name suffix)
if ([className hasSuffix:testClassSuffix])
{
// Remove the suffix, assuming that the test class
// name matches the name of a "real" class
NSString *testClassNameWithoutSuffix = [className substringToIndex:className.length - testClassSuffix.length];
[testClasses addObject:testClassNameWithoutSuffix];
}
else
{
[classesThatShouldHaveTests addObject:className];
}
}
}
free(classes);
// Remove the classes where a test case was found
NSMutableArray *classesThatDontHaveTests = [classesThatShouldHaveTests mutableCopy];
[classesThatDontHaveTests removeObjectsInArray:testClasses];
for (NSString *nameOfClassThatDoesNotHaveTest in classesThatDontHaveTests)
{
XCTAssert(0, @"%@ does not have a test case", nameOfClassThatDoesNotHaveTest);
}
}
}
@end
@NxSoftware
Copy link
Author

Modify the following line to suit your project's class prefix:
NSString *const prefixForClassThatShouldHaveTest = @"SW";

Modify the following line if your test case classes have a different suffix:
NSString *const testClassSuffix = @"Tests";

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment