I'd like to implement XCTest unit-tests, but don't really want to use XCode to do it. It's not obvious how to do this, and I'm wondering if this is even possible?
From what I've found so far, one could get the impression that XCTest is completely dependent on XCode. I've found the xcodebuild test command-line support, but that depends on finding an XCode project or workspace.
Have I any options here, or do I just rip out the existing SenTestingKit code and revert to some home-brew unit test code? I have some such code to hand, but it's not the Right Thing To Do.
Rationale/history:
This is not just me being old-skool. I have an Objective-C program which I last touched two years ago, for which I had developed a reasonable set of unit tests based on SenTestingKit. Now I come back to this code – I may at least have to rebuild the thing, because of intervening library changes – I discover that SenTestingKit has disappeared, to be replaced by XCTest. Oh well....
This code was not developed using XCode, so there isn't a .project file associated with it, and the tests were up to now happily managed using SenTestingKit's main programs, and a Makefile check target (that's partly being old-skool, again, partly a lack of fondness for IDEs, and partly this having been an experiment with Objective-C, so originally sticking with what I know).
Thanks, #stanislaw-pankevich, for a great answer. Here, for completeness, I'm including (more-or-less) the complete test program which I ended up with, which includes a couple of extra details and comments.
(This is a complete program from my point of view, since it tests functions
defined in util.h, which isn't included here)
File UtilTest.h:
#import <XCTest/XCTest.h>
#interface UtilTest : XCTestCase
#end
File UtilTest.m:
#import "UtilTest.h"
#import "../util.h" // the definition of the functions being tested
#implementation UtilTest
// We could add methods setUp and tearDown here.
// Every no-arg method which starts test... is included as a test-case.
- (void)testPathCanonicalization
{
XCTAssertEqualObjects(canonicalisePath("/p1/./p2///p3/..//f3"), #"/p1/p2/f3");
}
#end
Driver program runtests.m (this is the main program, which the makefile actually invokes to run all the tests):
#import "UtilTest.h"
#import <XCTest/XCTestObservationCenter.h>
// Define my Observation object -- I only have to do this in one place
#interface BrownieTestObservation : NSObject<XCTestObservation>
#property (assign, nonatomic) NSUInteger testsFailed;
#property (assign, nonatomic) NSUInteger testsCalled;
#end
#implementation BrownieTestObservation
- (instancetype)init {
self = [super init];
self.testsFailed = 0;
return self;
}
// We can add various other functions here, to be informed about
// various events: see XCTestObservation at
// https://developer.apple.com/reference/xctest?language=objc
- (void)testSuiteWillStart:(XCTestSuite *)testSuite {
NSLog(#"suite %#...", [testSuite name]);
self.testsCalled = 0;
}
- (void)testSuiteDidFinish:(XCTestSuite *)testSuite {
NSLog(#"...suite %# (%tu tests)", [testSuite name], self.testsCalled);
}
- (void)testCaseWillStart:(XCTestSuite *)testCase {
NSLog(#" test case: %#", [testCase name]);
self.testsCalled++;
}
- (void)testCase:(XCTestCase *)testCase didFailWithDescription:(NSString *)description inFile:(NSString *)filePath atLine:(NSUInteger)lineNumber {
NSLog(#" FAILED: %#, %# (%#:%tu)", testCase, description, filePath, lineNumber);
self.testsFailed++;
}
#end
int main(int argc, char** argv) {
XCTestObservationCenter *center = [XCTestObservationCenter sharedTestObservationCenter];
BrownieTestObservation *observer = [BrownieTestObservation new];
[center addTestObserver:observer];
Class classes[] = { [UtilTest class], }; // add other classes here
int nclasses = sizeof(classes)/sizeof(classes[0]);
for (int i=0; i<nclasses; i++) {
XCTestSuite *suite = [XCTestSuite testSuiteForTestCaseClass:classes[i]];
[suite runTest];
}
int rval = 0;
if (observer.testsFailed > 0) {
NSLog(#"runtests: %tu failures", observer.testsFailed);
rval = 1;
}
return rval;
}
Makefile:
FRAMEWORKS=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks
TESTCASES=UtilTest
%.o: %.m
clang -F$(FRAMEWORKS) -c $<
check: runtests
./runtests 2>runtests.stderr
runtests: runtests.o $(TESTCASES:=.o) ../libmylib.a
cc -o $# $< -framework Cocoa -F$(FRAMEWORKS) -rpath $(FRAMEWORKS) \
-framework XCTest $(TESTCASES:=.o) -L.. -lmylib
Notes:
The XCTestObserver class is now deprecated, and replaced by XCTestObservation.
The results of tests are sent to a shared XCTestObservationCenter, which unfortunately chatters distractingly to stderr (which therefore has to be redirected elsewhere) – it doesn't seem possible to avoid that and have them sent only to my observation centre instead. In my actual program, I replaced the NSLog calls in runtests.m with a function which chatters to stdout, which I could therefore distinguish from the chatter going to the default ObservationCenter.
See also the overview documentation
(presumes that you're using XCode),
...the XCTest API documentation,
...and the notes in the headers of the files at (eg) /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks/XCTest.framework/Headers
If you are looking for Xcode-based solutions see this and its linked solutions for examples.
For complete non-Xcode-based solution continue reading.
I used to ask similar answer a few years ago: Is there any non-Xcode-based command line unit testing tool for Objective-C? but things changed since then.
One interesting feature that appeared in XCTest over time is ability to run your custom test suites. I used to implement them successfully for my research needs, here is an example code which is a command line Mac OS application:
#interface FooTest : XCTestCase
#end
#implementation FooTest
- (void)testFoo {
XCTAssert(YES);
}
- (void)testFoo2 {
XCTAssert(NO);
}
#end
#interface TestObserver : NSObject <XCTestObservation>
#property (assign, nonatomic) NSUInteger testsFailed;
#end
#implementation TestObserver
- (instancetype)init {
self = [super init];
self.testsFailed = 0;
return self;
}
- (void)testBundleWillStart:(NSBundle *)testBundle {
NSLog(#"testBundleWillStart: %#", testBundle);
}
- (void)testBundleDidFinish:(NSBundle *)testBundle {
NSLog(#"testBundleDidFinish: %#", testBundle);
}
- (void)testSuiteWillStart:(XCTestSuite *)testSuite {
NSLog(#"testSuiteWillStart: %#", testSuite);
}
- (void)testCaseWillStart:(XCTestCase *)testCase {
NSLog(#"testCaseWillStart: %#", testCase);
}
- (void)testSuiteDidFinish:(XCTestSuite *)testSuite {
NSLog(#"testSuiteDidFinish: %#", testSuite);
}
- (void)testSuite:(XCTestSuite *)testSuite didFailWithDescription:(NSString *)description inFile:(NSString *)filePath atLine:(NSUInteger)lineNumber {
NSLog(#"testSuite:didFailWithDescription:inFile:atLine: %# %# %# %tu",
testSuite, description, filePath, lineNumber);
}
- (void)testCase:(XCTestCase *)testCase didFailWithDescription:(NSString *)description inFile:(NSString *)filePath atLine:(NSUInteger)lineNumber {
NSLog(#"testCase:didFailWithDescription:inFile:atLine: %# %# %# %tu",
testCase, description, filePath, lineNumber);
self.testsFailed++;
}
- (void)testCaseDidFinish:(XCTestCase *)testCase {
NSLog(#"testCaseWillFinish: %#", testCase);
}
#end
int RunXCTests() {
XCTestObserver *testObserver = [XCTestObserver new];
XCTestObservationCenter *center = [XCTestObservationCenter sharedTestObservationCenter];
[center addTestObserver:testObserver];
XCTestSuite *suite = [XCTestSuite defaultTestSuite];
[suite runTest];
NSLog(#"RunXCTests: tests failed: %tu", testObserver.testsFailed);
if (testObserver.testsFailed > 0) {
return 1;
}
return 0;
}
To compile this kind of code you will need to show a path to the folder where XCTest is located something like:
# in your Makefile
clang -F/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks XCTestDriver.m
Don't expect the code to compile but it should give you an idea. Feel free to ask if you have any questions. Also follow the headers of XCTest framework to learn more about its classes and their docs.
This one works ok:
Build your .xctest target as usual. By default it will be added to Plugins of the host app, but the location is irrelevant.
Create a runner command line tool with the code below.
Update: Xcode ships runner that is fairly standalone in /Applications/Xcode.app/Contents/Developer/usr/bin/xctest
You may use this one, if you don't want to create your own simple runner.
Run the tool with the full path to your test suite.
Sample runner code:
#import <Foundation/Foundation.h>
#import <XCTest/XCTest.h>
int main(int argc, const char* argv[]) {
#autoreleasepool {
XCTestSuite *suite = [XCTestSuite testSuiteForBundlePath:
[NSString stringWithUTF8String:argv[1]]];
[suite runTest];
// Note that XCTestSuite is very shy in terms of errors,
// so make sure that it loaded anything indeed:
if (!suite.testRun.testCaseCount) return 1;
return suite.testRun.hasSucceeded;
}
}
Related
Given a declaration of a Swift class like
#objc(NSFoo) public class Foo {
public func bar() -> () {}
}
I would expect, from my reading of the documentation, that on the Objective-C side of things we would be able to refer to this class using the identifier NSFoo. This is not what seems to be happening for me. The generated definition in ProjectName-Swift.h is:
SWIFT_CLASS("NSFoo")
#interface Foo
- (void)bar;
- (instancetype)init OBJC_DESIGNATED_INITIALIZER;
#end
whereas what I would expect is
SWIFT_CLASS("Foo")
#interface NSFoo
...
I am using Xcode 6.0.1.
I missing something, or is this just a Xcode bug?
Note: Things have change since this answer was first written - see updates at the end!
Yeah, this does seam to be a bug... though, controlling Obj-C runtime names of methods does work:
Say we define a pair of minimal Obj-C and Swift classes that interact with each other:
Foo.swift
import Foundation
#objc(SwiftFoo)
class Foo { // inheriting from NSObject makes no difference in this case
#objc(postcardFromSwift)
class func postcard() -> String {
return "Postcard from Swift!"
}
#objc(getMailInSwift)
class func getMail() {
if let hello = NSBar.postcard() { // NSBar is an Obj-C class (see below)
println("Printed in Swift: \(hello)")
}
}
}
NSBar.h
#import <Foundation/Foundation.h>
#interface NSBar : NSObject
+ (NSString *)postcard;
+ (void)getMail;
#end
NSBar.m
#import "NSBar.h"
#import "ObjS-Swift.h"
#implementation NSBar
+ (void)getMail {
// notice that I am not referring to SwiftFoo in spite of #objc(SwiftFoo)
printf("Printed in Objective C: %s", [[Foo postcardFromSwift] UTF8String]);
}
+ (NSString *)postcard {
return #"Postcard from Objective C!";
}
#end
If we now call their class methods, say, from main.m:
#import <Cocoa/Cocoa.h>
#import "NSBar.h"
#import "ObjS-Swift.h"
int main(int argc, const char * argv[]) {
// notice that I am not referring to SwiftFoo in spite of #objc(SwiftFoo)
[Foo getMailInSwift];
[NSBar getMail];
return NSApplicationMain(argc, argv);
}
This prints the following:
// --> Printed in Swift: Postcard from Objective C!
// --> Printed in Objective C: Postcard from Swift!
But it shouldn't have! Foo should only be visible to Obj-C as SwiftFoo since that is what #objc(SwiftFoo) is promising to do. Indeed, using SwiftFoo triggers the Use of undeclared identifier compiler error instead. The fact that this did work for method names, leaves little doubt that this is a bug. I am just amazed that you seem to be the first to ask about it! Plus one for that!
And yes:
// <#ModuleName#>-Swift.h
SWIFT_CLASS("SwiftFoo")
#interface Foo
+ (NSString *)postcardFromSwift;
+ (void)getMailInSwift;
... does seam to be inverted for the class name, yet this is how that macro works – see WWDC video Swift Interoperability In Depth (c. 45 min and c. 48 min into the video). The relevant documentation is Exposing Swift Interfaces in Objective-C.
Xcode 7 beta 4
The issue is now fixed (thanks to #ScottBerrevoets for the comment).
Xcode 7.1
(thanks to #Pang for the comment)
#objc class C { } // error: only classes that inherit from NSObject can be declared #objc
Currently (in XCode8) this seems to have been addressed.
Defined in XYZLogger.h and XYZLogger.m
NS_ASSUME_NONNULL_BEGIN
NS_SWIFT_NAME(Logger)
#interface XYZLogger : NSObject
+ (void)verbose:(NSString *)logString;
+ (void)debug:(NSString *)logString;
+ (void)info:(NSString *)logString;
+ (void)warn:(NSString *)logString;
+ (void)error:(NSString *)logString;
#end
NS_ASSUME_NONNULL_END
Used in objc like this:
[XYZLogger debug:#"Hi from objective C"];
Used in Swift like this:
Logger.debug("Hi from swift");
I'm trying to figure out how to set the default web browser on OS X via command line. I found this forum post, but the solution they are working out there is to open a specific URL with a specific application. I am looking for a way to set the default browser system wide. There's this neat app on GitHub, named Objektiv, which does exactly what I am after, but it's an app. There's a Cocoa method, apparently, to set the NSWorkSpace default browser.
The preference file com.apple.LaunchServices needs to be changed for this, maybe more.
How to set a different default browser via command line?
Thanks for help.
I found this tool. After installing it, you can do this:
defaultbrowser chrome
Here's the entire source-code in case the link 404s in the future. It is licensed under the MIT license with Copyright (c) 2014 Margus Kerma.
#import <Foundation/Foundation.h>
#import <ApplicationServices/ApplicationServices.h>
NSString* app_name_from_bundle_id(NSString *app_bundle_id) {
return [[app_bundle_id componentsSeparatedByString:#"."] lastObject];
}
NSMutableDictionary* get_http_handlers() {
NSArray *handlers =
(__bridge NSArray *) LSCopyAllHandlersForURLScheme(
(__bridge CFStringRef) #"http"
);
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
for (int i = 0; i < [handlers count]; i++) {
NSString *handler = [handlers objectAtIndex:i];
dict[[app_name_from_bundle_id(handler) lowercaseString]] = handler;
}
return dict;
}
NSString* get_current_http_handler() {
NSString *handler =
(__bridge NSString *) LSCopyDefaultHandlerForURLScheme(
(__bridge CFStringRef) #"http"
);
return app_name_from_bundle_id(handler);
}
void set_default_handler(NSString *url_scheme, NSString *handler) {
LSSetDefaultHandlerForURLScheme(
(__bridge CFStringRef) url_scheme,
(__bridge CFStringRef) handler
);
}
int main(int argc, const char *argv[]) {
const char *target = (argc == 1) ? '\0' : argv[1];
#autoreleasepool {
// Get all HTTP handlers
NSMutableDictionary *handlers = get_http_handlers();
// Get current HTTP handler
NSString *current_handler_name = get_current_http_handler();
if (target == '\0') {
// List all HTTP handlers, marking the current one with a star
for (NSString *key in handlers) {
char *mark = [key isEqual:current_handler_name] ? "* " : " ";
printf("%s%s\n", mark, [key UTF8String]);
}
} else {
NSString *target_handler_name = [NSString stringWithUTF8String:target];
if ([target_handler_name isEqual:current_handler_name]) {
printf("%s is already set as the default HTTP handler\n", target);
} else {
NSString *target_handler = handlers[target_handler_name];
if (target_handler != nil) {
// Set new HTTP handler (HTTP and HTTPS separately)
set_default_handler(#"http", target_handler);
set_default_handler(#"https", target_handler);
} else {
printf("%s is not available as an HTTP handler\n", target);
return 1;
}
}
}
}
return 0;
}
Makefile:
BIN ?= defaultbrowser
PREFIX ?= /usr/local
BINDIR ?= $(PREFIX)/bin
CC = gcc
CFLAGS = -O2
.PHONY: all install uninstall clean
all:
gcc -o $(BIN) $(CFLAGS) -framework Foundation -framework ApplicationServices src/main.m
install: all
install -d $(BINDIR)
install -m 755 $(BIN) $(BINDIR)
uninstall:
rm -f $(BINDIR)/$(BIN)
clean:
rm -f $(BIN)
I know this doesn't specifically answer your question, but if you only need to do this for Chrome, it has a flag that let's you set itself as the default browser:
$ open -a "Google Chrome" --args --make-default-browser
For newer versions of macOS (Catalina, Big Sur) I re-implemented the default browser tool in Swift (called it defbro). Main reason was to use a "modern" programming language that gives me an easy way to tweak and improve it. I also added json output defbro --json, in case you want to do other stuff with it.
Installation: brew install jwbargsten/misc/defbro
Repo: https://github.com/jwbargsten/defbro
The only way is to set it in the main Settings:
From the Apple menu, choose System Preferences, then click
General.
Click the “Default web browser” pop-up menu and choose
a web browser, like Chrome.
I would like to read a simple text file in objective-C (command line tool) this is my code
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
#autoreleasepool {
// insert code here...
NSLog(#"Hello, World!");
NSBundle *bundle = [NSBundle mainBundle];
NSString *aPath = [bundle pathForResource:#"data" ofType:#"txt"];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:aPath];
if (fileExists) {
NSLog(#"THERE!");
} else {
NSLog(#"NOT THERE!");
}
but I tried full path like /Users/Me/data or added data.txt into xcode but it just won't load the file, what am I missing? thank you!
Command-line tools don't have a bundle associated with them. You'll have to store the file somewhere else (a directory in /Library/Application Support, for example, or perhaps somewhere in /usr/local/share if you're installing the tool to /usr/local) and read it from there.
When trying to compile the following code for iphone in xcode
void removeGrid(int x,int y) {
//for(id *item in self) {
//if(item.position == ccp(x*32, y*32)) {
// printf("good");
//}
//printf("%#",item);
// }
char rrs[8];
sprintf(rrs,"01%d%d",x/32,y/32);
int aTag = [[[NSString alloc] initWithBytes:rrs length:sizeof(rrs) encoding:NSASCIIStringEncoding] intValue];
//NSAssert( aTag != kCCNodeTagInvalid, #"Invalid tag");
CCNode *child = [self getChildByTag:aTag]; //here it is simply getting a single chil
if (child == nil)
CCLOG(#"cocos2d: removeChildByTag: child not found!");
else
[self removeChild:child cleanup:true];
}
The compiler says "self was not declared in this scope". I'm new to objc and cocos2d, but this seems to be the way most tutorials access objects in the scene. Am I missing something?
Solved. This turned out to be one of Xcode's quirks. Since the function declaration was in c++, it was not able to access objective c self functions for some reason. Changing the declaration to an objective c allowed it to access all the functions. Doesn't make much sense to me, but it now works fine.
For those of you wondering, yes, the file had a .mm extension.
I have got a problem when unit testing a class. When running my test, it compiles without any errors but then it crashes (it does NOT fail in the sense of an assertion not being met), displaying the following error message:
/Developer/Tools/RunPlatformUnitTests.include:451:0 Test rig '/Developer/Platforms
/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.2.sdk/Developer/usr/bin/otest'
exited abnormally with code 134 (it may have crashed).
Here's my code:
The class' interface:
#interface AbstractActionModel : NSObject
{
NSString* mName;
ActionType mType; // enum
float mDuration;
float mRepeatCount;
float mDelay;
NSArray* mTriggerAreas;
}
The implementation:
- (void) dealloc
{
[mTriggerAreas release];
[super dealloc];
}
- (id) initWithConfigData: (NSDictionary*) theConfigData
{
NSAssert(nil != theConfigData, #"theConfigData cannot be nil");
self = [super init];
if (self)
{
self.name = [theConfigData objectForKey:ACTION_NAME];
self.type = [[theConfigData objectForKey:ACTION_TYPE] intValue];
self.duration = [[theConfigData objectForKey:ACTION_DURATION] floatValue];
self.delay = [[theConfigData objectForKey:ACTION_DELAY] floatValue];
self.repeatCount = [[theConfigData objectForKey:ACTION_REPEAT_COUNT] floatValue];
self.triggerAreas = [theConfigData objectForKey:ACTION_TRIGGER_AREAS];
}
return self;
}
Here's the test code:
- (void) testCreateAction
{
SoundActionModel* testSoundAction = (SoundActionModel*)[SoundActionModelFactory createActionModel:self.actionConfig];
STAssertNotNil(testSoundAction, #"returned object must not be nil");
}
The Factory's createActionModel: method:
+ (AbstractActionModel*) createActionModel:(NSDictionary *)config
{
NSAssert(config != nil, #"config must not be nil");
SoundActionModel* retVal = [[[SoundActionModel alloc] initWithConfigData:config] autorelease];
return retVal;
}
As previously mentioned: The code compiles, and it runs when testCreateAction is commented out. The problem does not seem to be the test itself (i.e. its assertion).
Telling from these postings (similar problem 1, similar problem 2) it seems to be a bug in XCode, but these links point to problems which arise when using Core Data (which I don't) or OCMock (which I don't, either - at least not knowingly).
Can someone tell me how to solve this kind of problem? If it turns out to be a bug, a workaround would be very much appreciated.
I also had this problem when starting out with OCUnit. This is caused by attempting to execute as test that is setup in Logic test mode, rather than application test mode. If the code under test has some dependency on Cocoa or Cocoa Touch, this code must be run with a target set up for application test.
The fact that the test runner itself crashes looks like an xcode bug to me as AppCode will continue passed this point.
A good source for setting up these tests is here