Genki
Genki

Reputation: 3195

How to build and test an iOS Swift Package officially?

I have a iOS Swift Package (no Objective-C, just Swift) that does not have .xcodeproj or .xcworkspace and would like to run unit test on this. Running a simple swift test does not work due to error in third party dependencies. Running different variations of xcodebuild did not work because of my unit test requiring a @testable import and errors out saying "module 'MyFramework' was not compiled for testing". So I tried using swift package generate-xcodeproj thinking I might be able to automatically create xcodeproj and do something clever, but gave me warning "'generate-xcodeproj' is no longer needed and will be deprecated soon.". Obviously I don't want to start using something that is going to be deprecated soon. This official documentation doesn't have any example.

Is there an official way to run and test a pure iOS Swift Package?

Update:

I created a sample SP to replicate this problem here.

When I try swift build, I get error:

✗ swift test error: artifact 'FloatingPanel' does not support the target platform and architecture ('Triple(tripleString: "x86_64-apple-macosx", arch: TSCUtility.Triple.Arch.x86_64, vendor: TSCUtility.Triple.Vendor.apple, os: TSCUtility.Triple.OS.macOS, abi: TSCUtility.Triple.ABI.unknown)')

When I try:

xcodebuild \
    -scheme MyLibrary \          
    -configuration Test \
    -derivedDataPath derivedData \
    -destination "platform=iOS Simulator,name=iPhone 12 Pro,OS=14.5" \
    -sdk iphonesimulator \
    ONLY_ACTIVE_ARCH=YES \
    -enableCodeCoverage YES \
    -resultBundleVersion 3 \
    VALID_ARCHS=x86_64 \
    -scmProvider system \
    -disableAutomaticPackageResolution \
    test | xcpretty -c

I get error:

MyLibraryTests.swift:2:22: module 'MyLibrary' was not compiled for testing

I am using Xcode 12.5.

Upvotes: 2

Views: 1241

Answers (1)

Genki
Genki

Reputation: 3195

I was able to run the unit test successfully by passing in -xcconfig with a file ENABLE_TESTABILITY = true in Config.xcconfig I created. This fixed the issue of module 'MyLibrary' was not compiled for testing.

xcodebuild \
    -scheme MyLibrary \
    -configuration Test \
    -derivedDataPath derivedData \
    -destination "platform=iOS Simulator,name=iPhone 12 Pro,OS=14.5" \
    -sdk iphonesimulator \
    ONLY_ACTIVE_ARCH=YES \
    -enableCodeCoverage YES \
    -resultBundleVersion 3 \
    VALID_ARCHS=x86_64 \
    -scmProvider system \
    -disableAutomaticPackageResolution \
    -xcconfig Config.xcconfig \
    test | xcpretty -c

In the Config.xcconfig:

ENABLE_TESTABILITY = true

The only other issue is sometimes it fails to find the MyLibrary as a scheme.

Upvotes: 2

Related Questions