Skip to content

Instantly share code, notes, and snippets.

@hoshi-takanori
Last active October 3, 2019 18:17
Show Gist options
  • Save hoshi-takanori/8208342 to your computer and use it in GitHub Desktop.
Save hoshi-takanori/8208342 to your computer and use it in GitHub Desktop.
Testing Objective-C class by XCTest with plain old Makefile in command line.
#import "MyObject.h"
int main()
{
@autoreleasepool {
MyObject *myObject = [[MyObject alloc] init];
NSLog(@"myObject = %@", myObject);
}
}
CLASS_NAME = MyObject
OBJS = main.o $(CLASS_NAME).o $(CLASS_NAME)Tests.o
PROGRAM = a.out
CFLAGS = -Wall -F$(FWPATH)
LIBS = -F$(FWPATH) -framework XCTest -framework Foundation
FWPATH = /Applications/Xcode.app/Contents/Developer/Library/Frameworks
XCTEST = /Applications/Xcode.app/Contents/Developer/usr/bin/xctest
$(PROGRAM): $(OBJS)
$(CC) -o $(PROGRAM) $(OBJS) $(LIBS)
test: $(PROGRAM)
DYLD_FRAMEWORK_PATH=$(FWPATH) $(XCTEST) -XCTest $(CLASS_NAME)Tests $(PROGRAM)
clean:
$(RM) -rf $(PROGRAM) *.o
$(OBJS): $(CLASS_NAME).h
#import <Foundation/Foundation.h>
@interface MyObject : NSObject
@end
#import "MyObject.h"
@implementation MyObject
@end
#import <XCTest/XCTest.h>
#import "MyObject.h"
@interface MyObjectTests : XCTestCase
@end
@implementation MyObjectTests
- (void)setUp
{
[super setUp];
// Put setup code here. This method is called before the invocation of each test method in the class.
}
- (void)tearDown
{
// Put teardown code here. This method is called after the invocation of each test method in the class.
[super tearDown];
}
- (void)testExample
{
MyObject *myObject = [[MyObject alloc] init];
XCTAssert(myObject != nil, @"myObject is successfully created.");
}
@end
@mx4492
Copy link

mx4492 commented May 17, 2014

Thanks. I was looking for this.

The hardcoded paths can be replaced by:

FWPATH=$(xcode-select --print-path)/Library/Frameworks
XCTEST=$(xcrun --find xctest)

@XavierJordaMurria
Copy link

Hello, Could you please explain (for dummies like me), how you run this make file from the Terminal (command line)?
I follow the logic but I can't make it work.

Thank you

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