I am having trouble getting unit tests to run in Swift projects created with the Swift Package Manager (that is, any unit tests created by the Package Manager... those I create from within Xcode work fine from within Xcode). I am getting the same error on all projects generated from the Package Manager. To keep it simple, I tried on a very basic test project with as little modification from the default setup as possible, but still getting the error. Here are the steps to reproduce:
swift package init --type executable
(module name is Hello
)swift package generate-xcodeproj
import Foundation
let message = "Hello, world!"
print(message)
import XCTest
@testable import Hello
class HelloTests: XCTestCase {
func testExample() {
XCTAssert(message == "Hello, world!")
}
static var allTests = [
("testExample", testExample),
]
}
swift build
and swift run Hello
(Also, from within in Xcode).swift test
or running any test in Xcode, the build fails with the following error message:Undefined symbols for architecture x86_64:
"Hello.message.unsafeMutableAddressor : Swift.String", referenced from:
implicit closure #1 : @autoclosure () throws -> Swift.Bool in HelloTests.HelloTests.testExample() -> () in HelloTests.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Somehow, it seems like it's failing to link the main module, so the symbols are not recognized. However, I can't tell what's wrong or how to fix it.
I downloaded one of the sample projects from GitHub, and generated the Xcode project. The tests for this project run perfectly in Xcode and the terminal. I've carefully compared the sample project to mine and can't tell what's different. Almost all setup code (Package.swift, file structure, etc.) and project setting are nearly identical. The only meaningful difference I can tell is that the sample project is a library/framework and mine is an executable (but seems like linking should work the same for both types). Otherwise, I can't tell what they are doing right and I am doing wrong.
I figured it out (thanks to Cristik's help). Executable modules are not testable (at least for now), so the solution was to move all definitions to a library module and leave just the main.swift file in the executable module. That way, all unit tests were run with the library as a dependency vs. the executable. The package.swift now looks like this:
let package = Package(
name: "HighestNumberPairing",
products: [
.executable(name: "HighestNumberPairing", targets: ["HighestNumberPairing"]),
.library(name: "NumberPairing", targets: ["NumberPairing"]),
],
dependencies: [],
targets: [
.target(
name: "HighestNumberPairing",
dependencies: ["NumberPairing"]),
.target(
name: "NumberPairing",
dependencies: []),
.testTarget(
name: "NumberPairingTests",
dependencies: ["NumberPairing"]),
]
)
The full program is here on Github.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With