Tests stop working under xcode 8 TEST_HOST error
Asked Answered
A

13

45

I want to start tests under Xcode 8 and it fail in the beginning. My error is:

Could not determine bundle identifier for MyProjectTest's TEST_HOST: "/Users/jakubmazur/Library/Developer/Xcode/DerivedData/MyProject-ejeepybggxvekxajlyngopeahiex/Build/Intermediates/CodeCoverage/Products/Testing-iphonesimulator/MyProject.app"

Any idea what's wrong here? I try to clean project - nothing. In the Build Settings in my target in Packaging i change Product Bundle Identifier depends on configuration in my schema. It can have something to do with that, but not sure.

-- EDIT

Ok figure how to narrow the problem. When i change settings in scheme to use Build Configuration Debug instead of Testing seems to work, but I need separate configuration for testing.

Ammunition answered 14/9, 2016 at 14:27 Comment(4)
Did you try to delete everything under /Users/jakubmazur/Library/Developer/Xcode/DerivedData?Vagina
yep, that was my first move - didn't workAmmunition
Did you try to recreate your Testing scheme? It's not fun, but it should help.Vagina
Yep, as I said in question, as long as it in the same configuration is fine, when I change configuration I'm getting the same errorAmmunition
A
11

Ok figure it out. There is a problem with naming packages. Unfortunatelly there is some issue with Xcode. I was using different Module Names for different Configurations. So go to Project -> Build Settings -> Product Module Name Change name in all configurations for same name without spaces.

Ammunition answered 23/9, 2016 at 8:11 Comment(3)
I had a Product Module Name defined at the project level, which was inherited by my application / TEST_HOST and overridden by the test target. I deleted the Product Module Name defined on the project and set that field on the app and the test target, then everything started working again.Dunaj
@Kuba This solved my problem even in Xcode 9 beta 4 ... Thank you :)Lindsy
Hi, wouldn't doing this mess with the other apps though? when I changed my Product Module Name my app could no longer unarchive some archived objects, because the of the change.Surmullet
D
62

For some reason the "Host Application" setting in the picture below was the problem for me. Selecting the proper target fixed this.

This ended up modifying the following values in my xcodeproj:

  • BUNDLE_LOADER = "$(TEST_HOST)";
  • TEST_HOST = "$(BUILT_PRODUCTS_DIR)/myappname.app/myappname";
Demetri answered 5/12, 2016 at 22:21 Comment(0)
K
19

There is one more case you might encounter. If you need different product names for main target(for example Debug, Staging, Production) - and try to use Xcode Host Application selector, it will write incorrect values to TEST_HOST build setting.

And while error message you see is about bundle identifier in Debug configuration, Xcode actually complains about TEST_HOST in Release configuration.

I fixed it by manually changing TEST_HOST build setting. For example, if you have ProductName on main target set to AppDebug in Debug and AppRelease in Release configuration, your TEST_HOST should be following:

What Xcode sets:

$(BUILT_PRODUCTS_DIR)/AppDebug.app/AppDebug

What you need to set:

Debug configuration: $(BUILT_PRODUCTS_DIR)/AppDebug.app/AppDebug
Release configuration: $(BUILT_PRODUCTS_DIR)/AppRelease.app/AppRelease
Kinship answered 10/2, 2017 at 15:29 Comment(1)
Beautiful man! How to make @testable import generic?Abroad
H
15

My project has many targets so I had to set the Tests Target's Host Application to 'None'. (Xcode 9.2)

enter image description here

Heelpiece answered 24/4, 2018 at 17:29 Comment(1)
That's my case =) Thank you!Stuccowork
A
11

Ok figure it out. There is a problem with naming packages. Unfortunatelly there is some issue with Xcode. I was using different Module Names for different Configurations. So go to Project -> Build Settings -> Product Module Name Change name in all configurations for same name without spaces.

Ammunition answered 23/9, 2016 at 8:11 Comment(3)
I had a Product Module Name defined at the project level, which was inherited by my application / TEST_HOST and overridden by the test target. I deleted the Product Module Name defined on the project and set that field on the app and the test target, then everything started working again.Dunaj
@Kuba This solved my problem even in Xcode 9 beta 4 ... Thank you :)Lindsy
Hi, wouldn't doing this mess with the other apps though? when I changed my Product Module Name my app could no longer unarchive some archived objects, because the of the change.Surmullet
M
7

I too have a project with several schemes, and each of which builds the app using a separate configuration (Debug, AdHoc, etc.) and a separate product name (MyApp, MyApp_Dev, MyApp_Staging, etc.).

Thankfully, they all have the same Module name so my unit test source files can simply use @testable import MyApp, regardless of active scheme).

To overcome the error, I just had to make sure that for each scheme of the app target that I wanted to test, the following two matched:

  1. The App Target's Build Settings > Product Name for that build configuration (.e.g, "Debug"),
  2. The Test Target's Build Settings > Test Host for the same build configuration (the part after $(BUILT_PRODUCTS_DIR)...)

So, if #1 is (for example):

MyApp-Debug

, then #2 must be:

$(BUILT_PRODUCTS_DIR)/MyApp-Debug.app/MyApp-Debug

In my case, my scheme was building the AdHoc configuration with a product name ending with the -DEV (development) suffix, but the test target was looking for a host named ending with a suffix of -STG (staging).

Mossgrown answered 29/5, 2019 at 8:4 Comment(0)
H
5

Under Test Target >> Build Setting >> Testing >> Test Host you can see the .app file which should be used for testing.

For me **.app file name was wrong. And I replaced it with **/Build/Intermediates/CodeCoverage/Products/Debug-iphonesimulator/**.app (check for the .app file present under this directory).

Hadji answered 22/11, 2016 at 13:15 Comment(2)
I just removed Test Host altogether and it worked for my test target and it worked. I did this for a framework, I'm not sure if that changes things.Forecourse
Removing Test Host altogether worked for me too when all other answers across the web did not.Pincer
J
4

It seems there are several bugs in Xcode8 that can cause this. However I have found a solution for the case where Xcode tries to find the TEST_HOST app within the Intermediates/CodeCoverage/ folder. (I tried all other solutions with the module names etc and it didn't work for me.)

The issue itself seems to be that Xcode does not even try to build the app before running the tests. However once Xcode is able to find the TEST_HOST in the folder it will rebuild when launching the tests. That's how we can find our way around it.

Two possible solutions:

If you don't have to have code coverage: Go to your unit test target's Build Settings and set Enable Code Coverage Support to No and deactivate code coverage in your build scheme's test setting. (Edit scheme, select Test on the left). If you want to run your unit tests and still get that TEST_HOST error message, try building (CMD+B) or running your app. Then you can run your tests without that error. Voilá.

If you need code coverage: You can follow a certain workaround to get that Intermediates/CodeCoverage/.. folder filled. Once the app is inside Xcode will rebuild for the unit tests as it should, but you need to get it filled once. This is how you do it:

  • In the app and the unit test target select No for "Enable Code Coverage Support"
  • Then in the "General" tab of the unit test target set the test host application to "None"
  • Uncheck "Allow testing Host API"
  • Try to start unit tests. Xcode will now at least try to build. If Xcode gives a build error (sometimes Xcode complains about Cocoapods here in my experience), try building once again.
  • Re-check "Allow testing Host API" again in the Unit Test target
  • Try running the tests again as above. Xcode should complain.
  • Now set the test host to the app.
  • Now the tests should run through. (Until you run a clean, then you'll have to re-do these steps..)

I know, it's bothersome. But it's the only solution to the problem so far.

Jounce answered 22/3, 2017 at 9:1 Comment(0)
F
3

I ran into this error a lot and eventually discovered that it was caused by varying PRODUCT_NAME between Debug and Release build configurations.

We were doing this so that the Debug builds had a different app name when installed on the phone (as well as a different bundle id so we could install them side-by-side with the App Store builds).

We reverted that change and added a new APP_DISPLAY_NAME setting to the build configuration, varied that between build types, and changed the Info.plist to use $(APP_DISPLAY_NAME) instead of $(PRODUCT_NAME).

After doing that, it's important to go into the test target in the project and make sure the 'Host application' setting in General is actually set to your application.

Fouquet answered 2/10, 2017 at 12:50 Comment(1)
Thank you for the answer, I have solved my problem using this solutionMonatomic
H
0

What I have discovered is that the tests will not run unless you have built and run the same target you are running the tests against on the device/sim you are testing on.

For example, if I run the tests against target Production, then I would have to build and run the target Production on the device/sim before I can run the tests. If I did not do that, it will give me the same error.

Hindi answered 7/9, 2017 at 11:18 Comment(0)
R
0

I have been using Bitrise for CI/CD (for years) and in this new project was failing like the question. I was able to resolve the issue my normalizing the Bitrise machine version with my own.

So, I'm using Xcode 14.1 in macOS 12.5 and the failure happened with a machine at Xcode 13 macOS 12.5

Matching them to the same version solved the issue. It makes sense as the older versions have some bug when looking for the (newer) TEST_HOST reference

Rameau answered 1/12, 2022 at 4:9 Comment(0)
L
-1

go to xcode Preferences -> location > Derived Data Advance -> select build location Unique

Lowminded answered 25/11, 2017 at 3:49 Comment(0)
L
-2

This is a bug in Xcode right now. See http://www.openradar.me/28095069.

Lingwood answered 17/9, 2016 at 4:45 Comment(2)
No, thats not a case here. I try to run on 9.3Ammunition
This link does not seem to reference the TEST_HOST error the @Kuba is describing.Osmunda
B
-5

Quitting Xcode and removing DerivedData folder at fixed the problem for me.

$ sudo rm -Rf Library/Developer/Xcode/DerivedData

Shame on you, Apple!

Blinding answered 14/11, 2017 at 11:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.