How to add tests to an existing Mac app

I have an app that's starting to grow beyond my abilities to test manually every time I make changes to it (yes, I should probably have added tests from the

beginning, but at the time I was new to Mac/Xcode/Swift and you can't learn everything at once; for a c# developer it's quite a culture shock... it still is!). So, I've added a test class and tried to call one of the methods that my UI uses but that method makes the following call so that it can get access to a table view controller (for example), so I need to load the UI somehow:


let del = NSApplication.shared().delegate as! AppDelegate


I tried adding the following to my setUp() method (which I just copied from main.swift):


_ = NSApplicationMain(CommandLine.argc, CommandLine.unsafeArgv)


But the UI just loads and then sits there; nothing happens. So when I quit the UI everything finishes and the test that I ran in the first place seemingly never gets called.


My question is: how do I go about loading the UI properly for my test cases to use?


Thanks.

Accepted Reply

Are you creating an app test or a library test? (Testing with Xcode explains that terminology.) If you’re creating a library test you should refactor your library code that it’s not dependent on the app delegate. If you’re creating an app test, the app delegate should be up and running within your app at the time that your test code runs.

ps One of the reasons why there’s not a lot of Mac-specific stuff in this space is that the way that tests run is very similar on both platform. For example, the distinction between app and library tests is the same on both platforms.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Replies

I get the impression these days that writing macOS apps is pretty much a dead end unless you're a huge software company. There is practically no information available for testing that isn't related to iOS. Try searching google on the topic with a "-iOS" switch and you'll get nothing.


😠

Are you creating an app test or a library test? (Testing with Xcode explains that terminology.) If you’re creating a library test you should refactor your library code that it’s not dependent on the app delegate. If you’re creating an app test, the app delegate should be up and running within your app at the time that your test code runs.

ps One of the reasons why there’s not a lot of Mac-specific stuff in this space is that the way that tests run is very similar on both platform. For example, the distinction between app and library tests is the same on both platforms.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thanks eskimo. I was really trying to avoid coming to that conclusion, but I can see that it's the right way to go. I was, indeed, more interested in a library test, so I've refactored my code to remove direct references to any GUI controllers. One day, time permitting (LOL!) I'll do some GUI tests, but I think it's more important to make sure the library is properly tested.