Xcode unit test "Creating more than one Application"

When I try to make a unit test target for my macOS app that uses Objective-C, running tests fails, and the debugging log shows a message "Creating more than one Application". If, on the other hand, when I create the unit test target, I select "None" as the target to be tested, and then add all my sources to that target, my tests work. So, I can get my testing done, but I think I must be doing something wrong.

By the way, along with the error message, there is a stack crawl:

	0   CoreFoundation                      0x00007ff8140410aa __exceptionPreprocess + 242
	1   libobjc.A.dylib                     0x00007ff813b660b7 objc_exception_throw + 48
	2   Foundation                          0x00007ff814f19bd8 -[NSCalendarDate initWithCoder:] + 0
	3   AppKit                              0x00007ff817030062 -[NSApplication init] + 1718
	4   XCTestCore                          0x000000010774b0df -[XCTestDriver _createTestBundlePrincipalClassInstance] + 82
	5   XCTestCore                          0x0000000107749338 -[XCTestDriver _runTests] + 111
	6   XCTestCore                          0x000000010770f194 _XCTestMain + 126
	7   libXCTestBundleInject.dylib         0x0000000106e8982d __copy_helper_block_e8_32s + 0
	8   CoreFoundation                      0x00007ff813fc9a91 __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
	9   CoreFoundation                      0x00007ff813fc99ca __CFRunLoopDoBlocks + 398
	10  CoreFoundation                      0x00007ff813fc883d __CFRunLoopRun + 898
	11  CoreFoundation                      0x00007ff813fc7e51 CFRunLoopRunSpecific + 560
	12  HIToolbox                           0x00007ff81da52f3d RunCurrentEventLoopInMode + 292
	13  HIToolbox                           0x00007ff81da52b84 ReceiveNextEventCommon + 199
	14  HIToolbox                           0x00007ff81da52aa8 _BlockUntilNextEventMatchingListInModeWithFilter + 64
	15  AppKit                              0x00007ff8170689d8 _DPSNextEvent + 858
	16  AppKit                              0x00007ff817067882 -[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 1214
	17  AppKit                              0x00007ff817059ef7 -[NSApplication run] + 586
	18  AppKit                              0x00007ff81702e111 NSApplicationMain + 817
	19  PlainCalc                           0x00000001057d0a91 main + 65
	20  dyld                                0x00007ff813b93418 start + 1896
Answered by JWWalker in 855607022

OK, I seem to have solved it. I had a .xcconfig file at the project level that specified

INFOPLIST_KEY_NSPrincipalClass = NSApplication

Apparently the test target is not supposed to specify a principal class.

So, thanks for suggesting that I try a newly created project, which I should have thought of.

What version of Xcode is this? What version of macOS are you testing on?

If you create a new app from the macOS > App template, does that reproduce the problem.

If you set an exception breakpoint and reproduce the problem, can you catch the exception at the point that it’s thrown.

By the way, along with the error message, there is a stack crawl:

Something is wonky with the symbolication there. The exception is thrown directly by NSApplication, so NSCalendarDate is definitely a red herring.

It’s easy to understand what this exception is about. Consider this tiny test program:

import Cocoa

func main() {
    let app1 = NSApplication()
    print("app1: \(app1)")
    let app2 = NSApplication()
    print("app2: \(app2)")
}

main()

It prints:

app1: <NSApplication: 0x15c904f50>
*** Assertion failure in -[NSApplication init], NSApplication.m:1590
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Creating more than one Application'
…

So, the program can create a single instance of NSApplication but hits this trap when it tries to create a second one.

If you set a symbolic breakpoint on -[NSApplication init], can you see the first instance of NSApplication being created?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

I'm using Xcode 15.2 on macOS 13.7.7.

A new app project does not reproduce the problem, but I'm not yet seeing the relevant difference. At first I thought it might be because I was using a nonstandard build location, but changing that back to the default didn't fix it.

With a breakpoint on -[NSApplication init], the first break has this backtrace

frame #0: 0x00007ff81702f9ac AppKit`-[NSApplication init]
frame #1: 0x00007ff81702f78e AppKit`+[NSApplication sharedApplication] + 120
frame #2: 0x00007ff81702df79 AppKit`NSApplicationMain + 409
frame #3: 0x000000010e442a91 PlainCalc`main(argc=5, argv=0x00007ff7b1ae7e68) at main.m:14:9
frame #4: 0x00007ff813b93418 dyld`start + 1896

And the second is

frame #0: 0x00007ff81702f9ac AppKit`-[NSApplication init]
frame #1: 0x00000001103bd0df XCTestCore`-[XCTestDriver _createTestBundlePrincipalClassInstance] + 82
frame #2: 0x00000001103bb338 XCTestCore`-[XCTestDriver _runTests] + 111
frame #3: 0x0000000110381194 XCTestCore`_XCTestMain + 126
frame #4: 0x000000010fafb82d libXCTestBundleInject.dylib`__RunTests_block_invoke_2 + 13
frame #5: 0x00007ff813fc9a91 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
frame #6: 0x00007ff813fc99ca CoreFoundation`__CFRunLoopDoBlocks + 398
frame #7: 0x00007ff813fc883d CoreFoundation`__CFRunLoopRun + 898
frame #8: 0x00007ff813fc7e51 CoreFoundation`CFRunLoopRunSpecific + 560
frame #9: 0x00007ff81da52f3d HIToolbox`RunCurrentEventLoopInMode + 292
frame #10: 0x00007ff81da52b84 HIToolbox`ReceiveNextEventCommon + 199
frame #11: 0x00007ff81da52aa8 HIToolbox`_BlockUntilNextEventMatchingListInModeWithFilter + 64
frame #12: 0x00007ff8170689d8 AppKit`_DPSNextEvent + 858
frame #13: 0x00007ff817067882 AppKit`-[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 1214
frame #14: 0x00007ff817059ef7 AppKit`-[NSApplication run] + 586
frame #15: 0x00007ff81702e111 AppKit`NSApplicationMain + 817
frame #16: 0x000000010e442a91 PlainCalc`main(argc=5, argv=0x00007ff7b1ae7e68) at main.m:14:9
frame #17: 0x00007ff813b93418 dyld`start + 1896

The exception is thrown from the second invocation of -[NSApplication init].

Accepted Answer

OK, I seem to have solved it. I had a .xcconfig file at the project level that specified

INFOPLIST_KEY_NSPrincipalClass = NSApplication

Apparently the test target is not supposed to specify a principal class.

So, thanks for suggesting that I try a newly created project, which I should have thought of.

Xcode unit test "Creating more than one Application"
 
 
Q