Swift Package Tests in Test Plan: “Module was not compiled for testing”

My iOS app consists of one app target and several local SPM packages. Previously, I had a test plan which only included the app target’s tests, while the packages’ tests were launched separately using the packages’ scheme names.

Now, I’m trying to switch to test plans entirely. I included the test targets defined in the packages in the test plan. However, doing so resulted in the following error message, attributed to @testable import statements:

Testing failed:
	Module '<***>' was not compiled for testing
	Command CompileSwiftSources failed with a nonzero exit code
	Testing cancelled because the build failed.

If I understand it correctly, the error occurs because the Enable Testability (ENABLE_TESTABILITY) build setting defaults to NO, preventing the packages’ internal symbols from being exposed with @testable imports. Since Swift packages do not use Xcode project files, I’m confused about where I should define this setting. Besides that, testability is expected to be enabled only for test and not release builds, while Swift packages don’t support build configurations.

Am I missing something? Is there a way to make Swift packages and test plans play nice together?

I am also interested in this!

My use case being: we have internal frameworks that we link to our main app target, and that gets deployed via a CI server. (Github actions and Fastlane). I'd like to include test coverage of these frameworks in the overall test coverage report, since those frameworks are more heavily tested than the app target. It would be nice if it could just be run in one go, without having to do much else with the CI server's configuration.

Otherwise, is there a way to make use of a test coverage report that can be saved in our repo, as these frameworks don't change much, but would have a coverage report we can make use of?

I have a setup similar to yours with an app target and local SPM packages, I'm not sure this applies to your case but I came across a similar situation. Try to run the tests with a Debug configuration if you're not doing so already.

I've also noticed that debugging code inside a SPM package included in the app was very limited with any configuration name other then Debug because the packages would be compiled without debugging information. I have a set of xcconfig files for diferent environments like "Development" and "Staging" assigned to the correspondent configurations in the project. Then I have a Debug configuration which I assign one of those xcconfigs.

Hope this helps.

The critical piece in making this work is ensuring you choose the correct reference to your .xctestplan file within your scheme configuration. I was running into the same errors after wanting to find a way to bundle both my app's unit testing target and multiple .testTargets included in a local SPM package into a single scheme / .xctestplan so that my build machine could run using a single scheme and hit all targets at once.

  1. Within the directory of your Xcode project, your .xctestplan file can live wherever you'd like it to (I like to keep mine within a Test Plans folder within the target folder - just make sure that you add the file as a reference within your project as well).
  2. Then open the .xctestplan file and under the Tests tab, use the + icon to add all of the test targets you want (can be from either the app's target list or the local SPM packages).
  3. Open the Edit Scheme... menu for your scheme and pull up the tests section.
  4. Use the + icon to Add existing Test Plan... and you should see a dialog appear showing two instances of the same .xctestplan file
  5. Choosing the one found under the Packages folder will give that scheme access to the targets it needs to resolve the import error, and if you've previously added the other one, you will encounter this error.
  6. If you have previously chosen the other one, you can remove it from the scheme using the - button and add back the correct one which will also solve the error

Hope this solves the issue for you!

Swift Package Tests in Test Plan: “Module was not compiled for testing”
 
 
Q