XCTest, Failed to load test bundle. Crash on test launch - ios

In Xcode 9.1, I added a Unit Test Target to an older project and I'm not able to get tests to run. The target crashes on launch with the following error in the console:
The bundle “XXXTests” couldn’t be loaded because it doesn’t contain a version for the current architecture*
Full output:
2017-11-15 11:52:40.973 XXX[54962:16511010] Failed to load test bundle from file:///Users/XXX/Library/Developer/Xcode/DerivedData/XXX-gaxtmzjadfkxzsdciqnpmyczgntj/Build/Products/DEV-iphonesimulator/XXX.app/PlugIns/XXXTests.xctest/: Error Domain=NSCocoaErrorDomain Code=3585 "dlopen_preflight(/Users/XXX/Library/Developer/Xcode/DerivedData/XXX-gaxtmzjadfkxzsdciqnpmyczgntj/Build/Products/DEV-iphonesimulator/XXX.app/PlugIns/XXXTests.xctest/XXXTests): no suitable image found. Did find:
/Users/XXX/Library/Developer/Xcode/DerivedData/XXX-gaxtmzjadfkxzsdciqnpmyczgntj/Build/Products/DEV-iphonesimulator/XXX.app/PlugIns/XXXTests.xctest/XXXTests: mach-o, but wrong architecture" UserInfo={NSLocalizedRecoverySuggestion=Try installing a universal version of the bundle., NSFilePath=/Users/XXX/Library/Developer/Xcode/DerivedData/XXX-gaxtmzjadfkxzsdciqnpmyczgntj/Build/Products/DEV-iphonesimulator/XXX.app/PlugIns/XXXTests.xctest/XXXTests, NSLocalizedFailureReason=The bundle doesn’t contain a version for the current architecture., NSLocalizedDescription=The bundle “XXXTests” couldn’t be loaded because it doesn’t contain a version for the current architecture., NSDebugDescription=dlopen_preflight(/Users/XXX/Library/Developer/Xcode/DerivedData/XXX-gaxtmzjadfkxzsdciqnpmyczgntj/Build/Products/DEV-iphonesimulator/XXX.app/PlugIns/XXXTests.xctest/XXXTests): no suitable image found. Did find:
/Users/XXX/Library/Developer/Xcode/DerivedData/XXX-gaxtmzjadfkxzsdciqnpmyczgntj/Build/Products/DEV-iphonesimulator/XXX.app/PlugIns/XXXTests.xctest/XXXTests: mach-o, but wrong architecture, NSBundlePath=/Users/XXX/Library/Developer/Xcode/DerivedData/XXX-gaxtmzjadfkxzsdciqnpmyczgntj/Build/Products/DEV-iphonesimulator/XXX.app/PlugIns/XXXTests.xctest}
(And then some diagnostic output that I didn't include)
Stuff I've checked after extensively googling:
Build Active Architecture Only setting is consistent across the app and test targets (With a value of Yes)
Enable Testability is set to Yes for the main target
I've deleted the target and started over, no luck
How do I fix this?

Make sure iOS Deployment target fields of your application target and test target are the same

I had the same issue. Simply building and running the associated application target on an iOS11.1 simulator (which happens to match the "iOS Deployment Target" of Test target) it started working.
It seems starting the test the first time did not trigger a build of the test host. Subsequent runs seem to trigger the builds as needed and all is good.
YMMV

It happened to me when updating the Xcode version from 9.3 to 10. In my case, deleted the DerivedData, then the unit test works.

I had the same error. And for me what it worked was the following:
Select your test on the PROJECT NAVIGATOR
on the right side, in FILE INSPECTOR choose the Target Membership, in this case your test target.

In my case, The Team under Signing & Capabilities were different for both Target Membership
making the Team same for all Target Membership solved the issue for me.

I had a similar error. My tests were passing locally but failing on CI (Xcode Cloud). The fix for me was to set Enable Hardened Runtime to No for Debug.
(from https://www.jessesquires.com/blog/2020/02/23/mac-app-tests-fail-with-hardened-runtime/)

In my case, I had Mac version of OCMock.frameworked in Link Binary with Libraries build phrase for an iOS project.
Removing it solved the issue for me.

run with another ios11 device solve the problem

Related

How to run automated XCUITests in Xcode for iOS via .ipa

I am facing an issue which ultimately results in following error -
The bundle “$(PRODUCT_NAME)” couldn’t be loaded because its executable couldn’t be located. Try reinstalling the bundle.
Error Domain=IDETestOperationsObserverErrorDomain Code=6 "Early unexpected exit, operation never finished bootstrapping - no restart will be attempted" UserInfo=
{NSLocalizedDescription=Early unexpected exit, operation never finished bootstrapping - no restart will be attempted}
I have been working on getting my UITest automated but i am intermittently(though 80% of the time) facing the above issue.
I am using the following command from command line .
xcodebuild -project Commute.xcodeproj -scheme CommuteUITests -configuration Debug test -destination "platform=iOS,name=$DEVICE_NAME" -derivedDataPath $DERIVED_DATA_DIR -only-testing:CommuteUITests/$SUITE .
where everything have there usual meaning but this results in above error, and also i sometime provide multiple destinations.
On the other hand i want to run .ipa instead of building via source code.
So basically there are 2 problems here -
Why do i get these errors, i hovered around the internet but i'm not
able to find the solution?
How to do i start my tests for testing an .ipa?
Someone please help me i'm stuck here big time, any help/pointer is greatly appreciated.
If you are still having this issue please check that your tests are picking up the correct info.plist file. If the tests pick up the incorrect info.plist file it will result in the following error:
The bundle “$(PRODUCT_NAME)” couldn’t be loaded
I know you are not using cocoapods but this guy had the same issue:
https://github.com/CocoaPods/CocoaPods/issues/7178
So make sure your tests are using the correct info.plist. Hope this helps!
As the mentioned answer was not sufficient enough, here are my additional insights:
Yes, there was an Info.plist file loaded, which should not have been loaded. But where did it come from?
My Tests target (in particular my UI Tests target) had an Info.plist file on their own, that was just fine, until I decided to make use of the #testable opportunity given by Xcode nowadays.
Open your file-browser of your project in Xcode:
⌘+0 then ⌘+1
Enter in the search field at the bottom Info.plist
Now I had two referenced Info.plist-files:
Info.plist with Target Membership to the Target Application (which classes are also imported via #testable)
Info.plist with Target Membership was the (UI) Testing Target
Just deselect the 2nd Info.plist Target membership (or delete it, if you don't need it anymore) and should be all fine.

Xcode 7.0 XCTest dyld: could not load inserted library IDEBundleInjection

I'm running unit tests on my iOS project and when it's running, it crashes and spits this out:
dyld: could not load inserted library '/private/var/mobile/Containers/Data/Application/1CAB64C8-D730-427B-8E9E-BD5E152ACFD6/tmp/IDEBundleInjection.framework/IDEBundleInjection' because no suitable image found. Did find:
/private/var/mobile/Containers/Data/Application/1CAB64C8-D730-427B-8E9E-BD5E152ACFD6/tmp/IDEBundleInjection.framework/IDEBundleInjection: mmap() error 1 at address=0x00436000, size=0x00004000 segment=__TEXT in Segment::map() mapping /private/var/mobile/Containers/Data/Application/1CAB64C8-D730-427B-8E9E-BD5E152ACFD6/tmp/IDEBundleInjection.framework/IDEBundleInjection
I'm using XCode 7.0 and testing on an iPod running iOS 8.3. I have deleted the derived data and I've restarted XCode and it still hasn't worked.
I fixed this by just creating a new UnitTest target, copying everything over, and removing the old one.
In General Panel , not leave the Team to "None"
At Same time : select the code sign in Build Setting->Code Signing-> Code Signing Identity
I managed to fix this by specifying my developer certificate in build settings->code signing identity.
I fixed this by selecting the correct target and then selecting the correct Code Signing Identity from Settings. It was set to a non default - changed to 'IOS Developer' for the Debug setting entry.
In my case there was nothing wrong with the provisioning profiles, it was down to the Architectures in the project excluding arm64. Once arm64 was added to both the test target and the host app target the tests ran fine (On Xcode 7.2). The error was:
dyld: could not load inserted library '/private/var/mobile/Containers/Data/Application/A94F5AD3-2A5F-415D-B127-0F45DEC7A659/tmp/IDEBundleInjection.framework/IDEBundleInjection' because no suitable image found. Did find:
/private/var/mobile/Containers/Data/Application/A94F5AD3-2A5F-415D-B127-0F45DEC7A659/tmp/IDEBundleInjection.framework/IDEBundleInjection: mmap() error 22 at address=0x00501000, size=0x00005960 segment=__LINKEDIT in Segment::map() mapping /private/var/mobile/Containers/Data/Application/A94F5AD3-2A5F-415D-B127-0F45DEC7A659/tmp/IDEBundleInjection.framework/IDEBundleInjection
I had this same issue and had to reboot my device. I was using iOS 10 with Xcode 8.0 (developing in swift 3.0)
I needed to set the team under Project->App Target->General. It was set to "None", instead of a valid team.
I had my base SDK in XCode Build Settings set to a more recent version of iOS than the phone had, so I had to upgrade the OS on the phone.
I fixed this issue by rebooting my iOS Device.

Xcode 7 - Code coverage data generation failed

When I run my tests I get an error:
Code coverage data generation failed.
Unable to retrieve the profile data files from 'UIDevice'.
On console was printed warning:
Timed out waiting 120 seconds for simulator to boot, current state is 1.
What is the reason?
If you are integrating your project with a 3rd party dynamic framework, you may need to add a path in your build settings. Look for ->Build Settings->"Runpath search paths" and make sure it includes the path to the framework.
I was just seeing this exact issue myself after setting up my a project to use a framework my team has been working on. After updating this specific setting, the problem went away. In my case, the path was identical to one I already had to set for the "Framework search paths" setting.
I solved this problem, just like I solve most of those XCode Problems:
delete your projects files in your DerivedData (Xcode>Preferences>Locations>DerivedData→ to jump there in finder)
Product>Clean
(keep alt-Button pressed) Product>Clean Build Folder
Quit XCode
Restart XCode
Remove your app from your device / simulator
Try again.
In case it still doesn't work, use another simulator / device for a few runs. Sooner or later it will work again on the original one again.
If you are using cocoa pods, check this thread on the Cocoapods repository:
https://github.com/CocoaPods/CocoaPods/issues/5385#issuecomment-226269847
This fixed my problem:
Copying #dfleming response:
For some reason, it appears that CocoaPods is not adding the "[CP]
Embed Pods Frameworks" build phase to the UI Tests target when
generating the project workspace.
I manually added this in and the UI Tests were runnable again.
This build phase should run the following script: (Replace
{YourProject} with your project name)
"${SRCROOT}/Pods/Target Support
Files/Pods-{YourProject}UITests/Pods-{YourProject}UITests-frameworks.sh"
I experienced exactly the same error and eventually got it working, these are the steps I took.
Tried restarting Xcode and the simulator, after cleaning and deleting the app, that never worked.
Then I restarted the Mac as suggested, but that still didn't work.
Then I chose a new device to test against in the simulator, was previously using 5s and switched to 6, and that worked.
Interestingly, when I switched to the iPhone 6 simulator, it showed the Apple logo with a loading bar, before running the app and working.
When switching back to the 5s simulator it did exactly the same, with the Apple loading bar, which it hadn't done before, and then the 5s worked.
So it looks to be a simulator issue, and switching to a different device worked. Resetting the content and settings may be the solution for a non-working device.
Cocoapods link_with method can cause this issue!
I was getting the exact same error on xcode 7.2 - no number of simulator or device resets seemed to clear it up. After completely rebuilding my UITest targets though things worked fine. After spending a lot of time in a massive git diff of the .pbxproj file I found a solution for my project. Im not sure if it addresses the root cause for everyone who is seeing this error but it definitely clears things up for me.
From the project info below "deployment target", "Configurations" will list all the possible configurations for your application. Expand the configuration you are trying to run and you should see a list of all your targets. In my case cocoapods had automatically added a base configuration for the UITest target:
Set this to none in the dropdown.
Next in the menu to the left select your UITest target then build phases You will need to remove check pods manifest.lock link binary with libraries emebd pods frameworks and copy pods resources.
Finally go to your pod file and check for any mentions of your UITest target or targets. In my case I had been specifying at the top of my podfile:
platform :ios, '8.4'
use_frameworks!
link_with 'My App', 'My UITesting Target'
pod 'A Pod', '~> 1.0'
Instead the podfile should list specific dependencies for each target:
platform :ios, '8.4'
use_frameworks!
target 'My App', :exclusive => true do
pod 'A Pod I want to use in my app', '~> 1.0'
end
Assuming you weren't using any pods in your UITests the target should build again without errors and tests will run!
My understanding of root of this problem is that each UITest target builds two separate bundles, one for the app and one for the UITest controller. Unfortunately cocoapods link_with logic modifies all the specified targets to expect the pods.framework in their bundle. The build phase scripts add the framework to the app bundle but not the UITest controller bundle, so then when you launch your tests that UITest controller bundle appears to be missing frameworks and xcode aborts the installation.
If you were using pods in your UITests you should be able to specify these in the same way:
target 'My UITesting Target', :exclusive => true do
pod 'Another Pod I want only for UITesting', '~> 1.0'
end
And when you run pod install everything should link up correctly.
This problem can also appears when you use Cocoapods with a framework where some dependencies are missing.
For example if you are using Framework A, and this framework depends of Framework B but in the Podspec of Framework A the dependency is not declared.
One of the reasons this can happen is if the host application that the test target is testing is not linked to the right dependencies. For example, if you have are testing a framework, make sure the host app is linked to those frameworks and also embedded.
My problem was caused by incorrect deployment version of unittest target. Tested App deployment version was 7.0, but the unittest's was not set up correctly automatically. It was set to 10.0, while my simulator's version was 8.4. Change the UT target's deployment version to 8.0 in build settings and then all the problems disappeared.
I tried a bunch of these solutions with no luck. I turned off the Code Coverage option in my Scheme's Test section, which suppressed the error but Tests would not run. Then I noticed quite a bit of stuff in NSLog console. Somewhere in there, it mentioned reference to a framework that I was no longer using and trying to load.
I searched the app for it, and Build Settings -> Other Linker Flags was trying to load the framework that wasn't there anymore.
I removed:
-framework 3rd_party_libname
Then tests worked again. This was confusing, but please check for NSLog msgs. I was using XCode 8.3
One or more of your simulators has gotten stuck. The only thing that always works to fix this for me is to reset the simulator content and settings from the Simulator menu.
Note: this will delete all app data from the simulator.
After a long time trying to figure this out, it turned out that I had to create a brand new test target.
Then after rebooting the device the problem no longer resurfaced.
In my case, I had added Swift files to a framework that was (until then) purely Objective-C. The test bundle did not have any Swift code in it.
Once I added a Swift file to the test bundle, Xcode automatically updated some project settings and the error went away.
You should keep the Swift file in the test bundle, even though it may not contain any code. Either Xcode or Cocoapods evidently use the existence of Swift files in the test bundle to determine whether to run the tests in "swift mode".
The root cause is probably the simulator failure. Issues with the simulator are common, especially on first launch.
If the problem occurs even after successful simulator launch and connect, please post the details of the error.
To get the simulator to launch, I often have to cancel the first run (after launching Xcode cleanly), and try re-running a few times.
If this is repeatable, happens on multiple projects, and persists after relaunch and cleaning of the projects, consider submitting a defect to Apple if the Stack Overflow community cannot help.
I had this same problem but it was due to having a test target and renaming the application's target. Make sure you have a valid Host Application set.
Choose the test target on the project page
Under the General tab there should be a Host Application select box
Choose the target you want to test against
I also faced same issue :
Tried following things for different projects :
1.For project where i had some third party frameworks, The problem was that test bundle can't find the framework at runtime. For this update test target's Runpath Search Paths build settings by adding $(PROJECT_DIR)/Frameworks (assuming you keep your frameworks at that location)., It fixed my problem for this project
You can look for it at below location :
Project file -> Test target -> Build Settings -> Runpath Search Paths
In other workspace, When I changed my testing device to some other simulator or change the device itself. it helped.
Restart the xcode or your mac system.
hope it will help u
Carthage users:
This happened to me after I added a new framework to my Cartfile.
I ran carthage update but forgot to drag the .framework file from Finder into the Embedded Binaries section of my app target!
Once I did that, the problem went away.
(Note this is a specific case of the general problem mentioned by #Mustafa above.)
I was having this problem on Xcode 8.3 on Sierra 10.12.4
I opened the Keychain Access application on my Mac
After a few seconds, I got several system pop-ups asking for my password, to grant permissions to Keychain items.
I entered my password into the pop-ups, and then my tests starting working.
Steps that worked in my case
Delete the scheme and clicking Manage Scheme->Auto generate scheme resolved the issue in my case.
Issue was caused by installing the certificate in the simulator and in the keychain
Using Xcode 9.1 with multiple build targets and standard compiler we were working on UI tests and I started seeing this problem. So, you will probably hate me for this answer, and I fully expect it to be downvoted, BUT this actually worked for me: I had to completely remove Xcode and my project from my machine to make this error disappear.
I tried every single suggestion in this thread, some multiple times, tried clearing cache files, deleting derived data, tinkering with build settings, updating podfiles, cleaning, rebuilding after each attempt, cleaning the simulator, manually recreating the simulators in 'manage devices'. After hours and hours of frustration it was still a complete mystery why this machine was failing to build our UI test. It seemed to work fine on other machines and on our CI. In addition to the items in this thread I manually modified the pbxproj file to set all options related to code coverage to "NO".
Finally, I was 1 step away from reformatting the entire machine. I decided to completely remove Xcode following the suggestions here: Stackoverflow: How to completely uninstall Xcode.
Specifically, I trashed our workspace, quit all programs, deleted all of the files listed, cleared content and settings from the simulator, deleted the simulator, turned the machine off for 20 minutes, came back, reinstalled Xcode, cloned the repository, and voila! No error.
Hope that solves the issue for someone. It's the 'nuclear' option, and you should never ever have to do this, but like I said, this is the only thing that worked for me.

Unable to run app in simulator

Today, when I try to run my app in simulator, it simply tells me
Unable to run app in Simulator
An error was encountered while running
(Domain = NSPOSIXErrorDomain, Code = 22)
And the error dialog looks like this:
I've tried many things:
Restart Xcode
Restart Simulator
Reboot
Reset content and settings of simulator
None of above works. And I looked into log, I found these are those records has something to do with this issue
2014/10/15 9:09:40.964 com.apple.CoreSimulator.CoreSimulatorService[979]: Error Domain=com.apple.CoreSimulator.SimError Code=146 "Unable to lookup in current state: Shutdown" UserInfo=0x7f96c861d000 {NSLocalizedDescription=Unable to lookup in current state: Shutdown}
2014/10/15 9:09:40.964 Xcode[1983]: [MT] iPhoneSimulator: Unable to connect to "com.apple.instruments.deviceservice.lockdown" (Error Domain=com.apple.CoreSimulator.SimError Code=146 "Unable to lookup in current state: Shutdown" UserInfo=0x7fcf6fc24190 {NSLocalizedDescription=Unable to lookup in current state: Shutdown})
2014/10/15 9:09:40.980 com.apple.CoreSimulator.CoreSimulatorService[979]: Error Domain=com.apple.CoreSimulator.SimError Code=146 "Unable to lookup in current state: Shutdown" UserInfo=0x7f96c861d000 {NSLocalizedDescription=Unable to lookup in current state: Shutdown}
2014/10/15 9:09:42.351 launchd_sim[2047]: assertion failed: 13F34: libxpc.dylib + 29453 [75E30F22-514B-3A20-B82C-EDA43AF5C35C]: 0x8d
2014/10/15 9:09:42.362 com.apple.CoreSimulator.CoreSimulatorService[979]: Could not register service com.apple.coreservices.lsuseractivity.simulatorsupport: Failed to lookup com.apple.coreservices.lsuseractivity.simulatorsupport: 0x44e
I also found some other people had the same issue here : https://gist.github.com/bdeshong/2dd90add09b7287a9a85
No luck to find solution after Googling around.
Here is my environment information
OSX 10.9.5
Xcode 6.0.1
iOS Simulator 8.0 (550.1)
Any ideas?
I found the root causes, and I also found the solution. First thing I've done is to ensure this has something to do with Xcode rather than a problem relative to the project itself. I tried to create a new project, and it works perfectly fine. So I assume that is caused by something in my project.
I actually added a resources reference folder to my project. So I think the problem has something to do with it. However, even I removed the folder from project, the problem is still there. In the end, it turns out, the resources folder could live in
Built App folder
App folder in simulator
Since Xcode may simply copy and overwrite target App bundle folder, so the resources folder may still there in the App bundle folder. So after removing the resources folder, you need to
Ensure the build is cleaned
Ensure the app in simulator is deleted
And for the reason the App cannot be launched correctly, that's because it appears resources is actually a reserved folder name in the bundle structure. By adding a folder named resources into the bundle, somehow ruined the normal structure. To solve the problem, I changed the name from resources to app_data, or whatever it is, that all work.
I got the same. I just did force quit xcode and simulator then worked for me.
I got the exact same error. When browsing in my project info I inadvertently deleted the bundle identifier; hence the "ErrorDomain".
to correct, goto project data; info; set bundle identifier to whatever it was. it worked for me.
You are doing everything correct but Xcode and Swift are still in their early stages, means they are a little buggy right now. Maybe try restarting your Xcode and iOS Simulator once more.
This can happen for a couple of reasons:
You quit the iOS Simulator.app while Instruments was trying to use it.
You changed the booted device in the iOS Simulator.app while Instruments was trying to use another one.
If neither of those is the case, take a look at ~/Library/Logs/CoreSimulator/*.log for more information that might help debugging and include it in your question.
Root cause in my case was slightly different. In the project file, the iOS deployment target was set to 8.0 for the test target and 8.1 for the main target. We were running Xcode 6.0.1 (6A317) with 8.0 simulators on our build servers, which all started failing after the main build target was set to 8.1.
I'm not sure whether it was the fact that the target and tests were using different iOS deployment targets, or whether the 8.1 set in the project file conflicted with the 8.0 simulators, but it was this change which broke the simulators across several machines.
Unfortunately after setting the iOS Deployment Target back to 8.0 in the project, it still required a reinstall of Xcode (I also removed the ~/Library/Developer/Xcode directory) to return simulators to a functional state.
Check if you have proper permissions on your /private/tmp folder, should be writable and executable for all users.
Clear all files in
/Users/XXXXX/Library/Developer/CoreSimulator/Devices
Restart Xcode and Simulator.
Now it will work.

Strange errors when Archiving iPhone app

So I am trying to archive my iPhone app for ad-hoc testing and I am getting some weird circumstances. When I archive it, at the top where it says archive type, for some reason it says "Mac app archive" instead of the usual "iOS App Archive"
I do not know why this is happening, because this is not a mac App.
I have the following three warnings when I archive the app that may help, but I'm not sure.
The first is this one
iPhone/iPod Touch: application executable is missing a required architecture.
At least one of the following architecture(s) must be present: armv6 (-19033)
I am not sure why this is happening, because I have both armv6 and armv7 listed in valid architectures
The second warning I get is this
The CodeResources file is missing and it must be a symbolic link to
_CodeSignature/CodeResources. Make certain that the bundle is on a locally-mounted volume
(not a remote SMB volume), and be certain to use the Mac OS X Finder to compress it
(-19062)
Again, not sure why this is happening, or even what it means.
The last error is this one
Warning: The Copy Bundle Resources build phase contains this target's Info.plist file 'Catch It/Resources/Info.plist'.
I am really lost on that one, I don't know what it is trying to tell me.
Please help me, because I cannot distribute this ipa because it thinks it is a mac app.
Thanks
I guess you are using Xcode 4.5.
I've just run into the same problem, and managed to solve the problem. My app is waiting for review now.
This is what I changed: In the target, I selected "Build phases", then in "Copy bundle resources" I removed the plist file.
If this doesn't fix your problem, also make sure you set the iOS deployment target to iOS 4.3 at least.
Edit: I also removed armv6 from both Architecturesand Valid Archictectures in the build settings.
I get message on Xcode 4.6.3
The CodeResources file is missing and it must be a symbolic link to
_CodeSignature/CodeResources. Make certain that the bundle is on a locally-mounted volume
(not a remote SMB volume), and be certain to use the Mac OS X Finder to compress it
(-19062)
when try Build app, after running it on Simulator. Change target to iOSDevise solve this problem.

Resources