When I run my tests in XCode 5, the main window of my OS X app appears on the screen for a couple of seconds while running the tests. Why? Even if I uncomment all my tests it still opens my main window.
-
The up-voted solutions here are about statically changing what you build to exclude the Application running bits. This might be what you want, but a very simple and very effective alternative solution is to launch your unit tests **without an applications delegate**. Unfortunately, the [answer on this question](http://stackoverflow.com/a/39142986/2547229) that suggests this approach only has one up vote at the moment. [Another question](http://stackoverflow.com/q/15714697/2547229) on this topic has [higher voted answers](http://stackoverflow.com/a/15725328/2547229) using the delegate approach. – Benjohn Feb 06 '17 at 09:59
-
I use the dynamic delegate, and I've put a refinement for detecting the test run in [my answer](http://stackoverflow.com/a/42065078/2547229). – Benjohn Feb 06 '17 at 10:16
7 Answers
You are running application test, not logic test. This means an instance of your app will be started and then run the unit tests. This allow you to perform some integration test that require your app is running.
Here is the guide to setup application test and logic test.
If you want to change it to logic test (so it run faster and don't need to start your app first):
- go to build settings for your unit test target
- search
Bundle
- remove Bundle Loader and Test Host

- 45,816
- 18
- 112
- 143
-
9When I do this, the test target fails to link properly. The test target was created by XCode (with the New test target option in the test navigator), and I haven't touched it apart from that. – Daniel Bruce Oct 09 '13 at 11:59
-
-
This used to be the case with Xcode 4, can the OP verify that this worked? (Must be a registered developer to see the link) https://devforums.apple.com/message/867874#867874 – qnoid Dec 18 '13 at 08:23
-
4This isn't possible in Xcode 5. See the above openradar link: http://openradar.appspot.com/15859153 – cbowns Feb 07 '14 at 00:07
-
7
-
2Works with Xcode7 and you can just set `Host Application` to `None` in the `General` Tab within the test Target – Daniel Jun 09 '16 at 07:01
-
It does work in XCode 7 when choosing None for the host app, however, you will get linker errors if using classes in your test from the host app. I'm seeing the same thing as @DanielBruce – stonedauwg Jun 27 '16 at 20:43
-
@stonedauwg I believe you forgot to add your class to unit-test target https://www.dropbox.com/s/jb7m4d62at58yr9/Screenshot%202016-11-22%2020.03.45.png?dl=0 (see on the right hand side it has two checkboxes) – Alex Bush Nov 23 '16 at 04:05
-
@AlexBush But you don't have to do that for "normal" unit tests when it's set to launch the real app, so is that only if you have the Host App set to None? – stonedauwg Nov 28 '16 at 15:02
-
Thats right, you have to delete the "Bundle Loader" and "Test Host" from your build settings.
But you have to add the necessary implementation files to your unit test target. The necessary files are what you want to use in your unit test cases. You need to do this because in logic tests XCode wont compile the whole application. So some of your files will be missing.
This is en error message if you have left out a file:
Undefined symbols for architecture i386:
"_OBJC_CLASS_$_Module", referenced from:
objc-class-ref in Lobic Network.o
objc-class-ref in Logic_Unit.o
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)
You can add the missing files by selecting the implementation file and bringing up the file inspector. There will be a section named "Target Membership" and there you can set the files target membership to your unit test also.
With XCTest, application files DO NOT need to be included within XCTest targets. The XCTest bundle is linked against the application which makes those files available during runtime.
To make this work, ensure the compiler option "Symbols hidden by default" is set to NO Within the Application target.
Here is a blog post with screenshots for clarity: http://zmcartor.github.io/code/2014/02/24/slim-xctest-targets
The advantage of this approach is test target builds much much faster.

- 409
- 3
- 15
-
1I think it's a bit of a shame that Apple requires you to have a compiling application before any of your tests can run... I'd rather have my tests have the same dependencies as my application, but compiled independently of the application target (i.e. Host Application should be empty). Is this possible? – fatuhoku Jan 21 '15 at 00:38
-
-
In XCode 7, removing Host Application
does not work for me. Indeed I use the following to avoid app runs.
in
main.m
static bool isRunningTests() { NSDictionary* environment = [[NSProcessInfo processInfo] environment]; NSString* testEnabled = environment[@"TEST_ENABLED"]; return [testEnabled isEqualToString:@"YES"]; }
modify main()
int main(int argc, char * argv[]) { @autoreleasepool { if (isRunningTests()) { return UIApplicationMain(argc, argv, nil, nil); } else { return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); } } }

- 1,428
- 18
- 23
If the tests are for code that can run on desktop and mobile, you can run them without a simulator or hosting them within your app.
The trouble is that you cannot use the scheme chooser for your normal target (desktop or iOS) to run the test.
The following worked for me in Xcode6.
File > New Target...
Select Cocoa Testing Bundle from the OS X category.
Take care to select None
from the target drop-down.
Click Finish. Add the relevant files to the new target as described above.
Now create a scheme to run the test.
Click the schemes chooser top-right and choose New Scheme...
, click the drop-down and navigate down the list to the new target. Now you can choose the scheme from the schemes chooser, and use ⌘U to run the tests.

- 1,275
- 11
- 26
On XCode5, the app does start. This answer shows how to change its delegate when running unit tests so that it exits right away: https://stackoverflow.com/a/20588035/239408
-
This is, for me at least, a much better solution. It's a shame your answer here hasn't been voted up, and that that other question isn't the one my googling hits! – Benjohn Feb 06 '17 at 09:53
I just wasted a morning on this.
Project was created in XCode 4 and used SenTesting.
Tried migrating tests on XCode 5/XCTTest
Had same issue - app ran in simulator and test never started after trying everything (change from app to logic tests, change to XCTest, remove SenTesting)
gave up created a clean XCode 5 project.
Added all my files in and tests ran ok.
May still have issues with Storyboard as these were built with XCode 4.
Drastic but it works so keep it as last resort.

- 5,277
- 2
- 41
- 62