34

I'm using Swift Package Manager on an iOS app on Xcode 11 following the instructions from https://developer.apple.com/videos/play/wwdc2019/408/

Everything looks great, except Unit Tests won't work now.

Navigator panel Project setup Error

betzerra
  • 839
  • 1
  • 8
  • 17
  • 1
    I had almost the exact same issue. It turned out to be due to a custom build configuration I had created. It started working again after I set the Test build configuration back to Debug in the scheme editor. Hope that helps. – Jeffrey Fulton Sep 27 '19 at 18:22
  • 1
    @JeffreyFulton Can you explain why this is needed? I'm facing a similar issue with Firebase even though I don't use Firebase in any of the unit tests. – Kunal Shah Mar 23 '23 at 17:36

4 Answers4

68

This appears to occur because SPM dependencies might not automatically be linked to test targets.

We can manually link it by:

  1. Clicking on your Xcode project file (e.g. Headlines)
  2. Select test target from the sidebar (e.g. HeadlinesTests)
  3. Navigating to Build Phases in the top bar.
  4. In the Link Binary With Libraries phase, add the required library from the SPM dependency (this looks like a white building within the SPM package ).
  5. You may also need to add the library to the Dependencies phase.
Bradley Mackey
  • 6,777
  • 5
  • 31
  • 45
  • 6
    THANK YOU! I think this is a step forward. However, in one of my projects I got now "Class XYZ is implemented in both <> and <>. One of the two will be used. Which one is undefined." and then it crashes on the test. – betzerra Oct 30 '19 at 15:39
  • Thank you, I had a problem with this for a long time and I couldn't understand if it was the PMS or the dependency. – Vinicius Carvalho May 28 '20 at 13:12
  • 3
    I did this as well on Xcode 12 beta, but there seems to be a memory issue ```CLASS: class 'Testing.TestOne' 0x102b2b818 small method list 0x102b26e40 is not in immutable memory``` – 0-1 Jun 23 '20 at 16:56
9

I was facing a similar issue in Xcode 14.1. Getting error Missing required module 'RxCocoaRuntime'.

My fix was:

  1. Go to the project target and select the Unit test target.
  2. Select build phases under the Unit Test target
  3. Select "Link Binary With Libraries"
  4. Added RxSwift and RxCocoa packages.
Dipak
  • 2,263
  • 1
  • 21
  • 27
4

After researching a bit, I got this fixed by adding -Xcc -fmodule-map-file=$(PROJECT_TEMP_ROOT)/GeneratedModuleMaps/macosx/<missing module name>.modulemap to OTHER_SWIFT_FLAGS in the test target.

Source:

PS: Use -Xcc -fmodule-map-file=$(PROJECT_TEMP_ROOT)/GeneratedModuleMaps/iphonesimulator/<module name>.modulemap if your platform is iOS.

UPDATE: Also, it seems that this is fixed on Xcode 11.2 beta2

betzerra
  • 839
  • 1
  • 8
  • 17
0

I saw this in Xcode 13 after I created a multi-platform app & had checked the box to create tests. The issue seems to be that Xcode created UI tests rather than unit tests as I had expected. I just made a new unit test target and everything worked as expected when importing my app in the test file.

Trev14
  • 3,626
  • 2
  • 31
  • 40