Exposing testing code from SPM package

On the project that I am working on we have recently transitioned from Cocoapods to SPM for providing our internal SDK. For the most part everything seems to be working well, expect one thing...

We have two projects - ProductSDK and ProductDemoApp. The SDK is an internal SDK used by different projects, and the Demo app is an iOS app using pretty much every aspect of the SDK for demo purposes.

ProductSDK is a Swift package. It contains around 20 different modules that can then be included in the projects using the SDK based on their needs. All of the provided modules are defined as library products in the Package.swift. The structure is similar to this:

  • ProductSDK - core target, depends on three external packages ("external dependency 1, 2, 3")
  • ProductSDKMocks - target exposed for external and internal testing, depends on ProductSDK
  • ProductSDKTests - test target, depends on ProductSDK, ProductSDKMocks, ProductFeature1Mocks
  • ProductFeature1 - Feature module, depends on ProductSDK
  • ProductFeature1Mocks - external testing module, depends on ProductFeature1, ProductSDK, ProductSDKMocks
  • ProductFeature1Tests - Test target, depends on ProductFeature1, ProductSDK, ProductSDKMocks
  • ...

On the implementation side of things we have ProductDemoApp - which is a regular iOS Xcode project using the ProductSDK as a package dependency. Project includes a main target DemoApp and a test target DemoAppTests. Demo App uses ProductSDK and ProductFeature1, while tests use ProductSDK, ProductFeature1, ProductSDKMocks and ProductFeature1Mocks. And here is the problem...

When adding the packages for the DemoApp target we add ProductSDK and ProductFeature1 under "Frameworks, Libraries and embeded content" section. This works fine when building the DemoApp. But not surprisingly test target fails to build since it cannot find Mocks that are required in the test code. How can we add the test dependencies without adding them to the DemoApp target?

We tried adding the Mocks to the DemoAppTests "build phases -> Link binary with libraries" section. And while that fixes the issue with missing Mocks, the build still fails. This time in one of the files that is part of ProductSDK -> No Such Module "External dependency 1". To me this error makes no sense since the thing builds fine during a regular build. I've also checked and only tests actually import Mock files.

Any ideas on how to solve this issue? To repeat the question - how can we use the mocks without needing to include them in the main app?

1 Like