Is there any way to share code among several OCUnit test cases? Maybe I'm missing something obvious, but I haven't been able to do it...
I have tried to put the common code in another class, but it seems you can only use STAssertxxx macros inside instance methods of classes inheriting from SenTestCase. Taking this into account, I put this common code in a singleton inheriting from SentTestCase with no test methods but this seems to break some internal assumption of OCUnit, as I'm not getting any errors from failed tests in the shared code.
My current code (not working) goes like this:
@interface TestHelper : SenTestCase
+ (TestHelper *)sharedHelper;
- (void)assertSomething:(id)object;
@end
@implementation TestHelper
+ (TestHelper *)sharedHelper
{
// Typical singleton magic
}
- (void)assertSomething:(id)object
{
STAssertWhateverOnObject(object, ...);
}
@end
@interface RealTestCase : SenTestCase
- (void)testWhatever;
- (void)testAnotherThing;
@end
@implementation RealTestCase
- (void)testWhatever
{
[[TestHelper sharedHelper] assertSomething:someObject];
STAssertOtherThings(someObject, ...);
}
- (void)testAnotherThing
{
[[TestHelper sharedHelper] assertSomething:someSimilarObject];
STAssertSomeOtherThings(someSimilarObject, ...);
}
@end
Please, note that this is a simple example. In this case I could put all the code in the RealTestCase class, but I would like a generic solution to share code among several test cases, not only among methods in the same test case.
By the way, I have a similar problem using Kiwi.