24

Recently I have started writing test case for one old static library. I have loaded the library to Xcode 5,Since Static Library is old , I have to manually add TestProject with Test Target.

When I am trying "Product-->Test" , It launches emulator and Console shows following error

The test bundle at /xxx/xxx/xxx/StaticLibTest.xctest could not be
loaded because an unanticipated error occurred: Error
Domain=NSCocoaErrorDomain Code=3587 "The bundle “StaticLibTest.xctest”
couldn’t be loaded because it is damaged or missing necessary
resources."
(dlopen_preflight(/xxx/xxxx/xxx/Debug-iphonesimulator/StaticLibTest.xctest/StaticLibTests):
no suitable image found.  Did find:

I don't have any clue why this error comes.

Any help for pointing out to the right direction will be really appreciated.

Aamir
  • 16,329
  • 10
  • 59
  • 65
Brijesh Thakur
  • 6,768
  • 4
  • 24
  • 29

10 Answers10

31

If other readers are getting this error, and you're using Carthage to add a framework, ensure the Copy Files phase is with Destination: Frameworks.

Adding frameworks from Carthage

If you accidentally add the frameworks to a Copy Files phase with a different target (other than Frameworks), or Copy Bundle Resources, this will also produce the damaged / invalid bundle error message.

odlp
  • 4,984
  • 2
  • 34
  • 45
  • This should be the accepted answer, exactly what was wrong with my setup. Not sure why the accepted answer says to make sure that the archs and Mach-O type are right, as these things are always set as described by default. Anyway thanks @odlp – tecknut Sep 13 '16 at 14:03
25

I had a similar problem. For me, the problem was a unit testing framework that was a dynamic framework but not copied over to the xctest bundle.

The solution was to add a "Copy Files" phase to my unit test target, set its destination to "Frameworks" and add my framework as an input file.

Lukas Spieß
  • 2,478
  • 2
  • 19
  • 24
21

I was able to resolve this issue by setting the host application for the test target: Host Application

Menno
  • 1,232
  • 12
  • 23
  • 1
    It means your application will start and run every single test, which is not what you'd want to do in Unit Testing. – Corbin Miller Jun 12 '18 at 17:28
  • @delta2flat is partially right. The simulator and app will launch which will need some extra time, but this only happens once for all of your tests (not for every single test). In case you set your host application to none, be sure to add all of the targets imported by the host application to the test target. You just might win the time needed for that time back by not having to wait for the app to launch ;) – Menno Jun 13 '18 at 20:21
  • 1
    There may be other precursors though you don't want to exercise - login over network, initial data state, user UI responses, User Settings, etc. When I had the Host Application removed, startup was also much faster as I could test individual methods in a true "black-box" fashion... – Corbin Miller Jun 14 '18 at 18:47
  • This worked for me! Happened after deleting Xcode beta and changing Xcode select over to the regular Xcode directory – Edmund Holderbaum Sep 23 '19 at 18:31
15

I found the answer by myself.

Your Build Settings -> Architectures should be same for Library and Test Project

Architectures -> Architectures = Standrad Architectures (armv7 ,armv7s , arm64)$(ARCHS_STANDARD)

Your Test Project's Linking -> Mach-O Type should be Bundle

Linking -> Mach-O Type = Bundle

Do Build and Run Tests. Thats it

Brijesh Thakur
  • 6,768
  • 4
  • 24
  • 29
12

For me this issue was due to the "iOS Deployment Target" Build Setting being different in my test target than my main app. Once I made them the same the problem went away.

Liron Yahdav
  • 10,152
  • 8
  • 68
  • 104
  • 1
    This is what worked for me. I had lowered the host app's deployment target but forgot to do the same for the tests target, so I was seeing this error. Thanks! – Bruno Koga Nov 30 '18 at 14:15
2

Check if you forgot to add your test target in your Podfile:

target 'MyProject' do

  # Add test target
  target 'MyProjectTests' do
    inherit! :search_paths
  end

  pod 'SomePodLibrary'

end
funct7
  • 3,407
  • 2
  • 27
  • 33
2

The bundle UITests couldn’t be loaded because it is damaged or missing necessary resources. Try reinstalling the bundle

Verify if all your targets are using the same iOS version in: Build Settings -> iOS Deployment Target

Narlei Moreira
  • 256
  • 2
  • 10
1

Turning off the pod configuration for the test target fixed the problem for me, it had something to do with the pod configuration messing with it.

Pellet
  • 2,254
  • 1
  • 28
  • 20
0

For me, we had just turned on CloudKit entitlement. For some reason that broke building.

CommaToast
  • 11,370
  • 7
  • 54
  • 69
0

Had a similar issue:

FrameworkA imports FrameworkB

FrameworkA-Tests also imports FrameworkB

What solved the issue was to remove(comment) all references to FrameworkB in FrameworkA-Tests, then Run Tests once and uncomment the references.

FrameworkA-Tests now imports FrameworkB and runs fine.

vicegax
  • 4,709
  • 28
  • 37