5

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:

  1. Create a new project with swift package init --type executable (module name is Hello)
  2. Add the Xcode Project: swift package generate-xcodeproj
  3. In Xcode build settings, ensure Enable Testability is Yes
  4. In swift.main enter this simple test code:
import Foundation

let message = "Hello, world!"
print(message)
  1. In HelloTests.swift:
import XCTest
@testable import Hello

class HelloTests: XCTestCase {
    func testExample() {
        XCTAssert(message == "Hello, world!")
    }

    static var allTests = [
        ("testExample", testExample),
    ]
}
  1. Package.swift and XCTestManifests.swift left as-is.
  2. It builds and runs fine with swift build and swift run Hello (Also, from within in Xcode).
  3. However, when running 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.

Justin Reusch
  • 624
  • 7
  • 14
  • Situation is very similar to the one from https://stackoverflow.com/questions/41322034/swift-test-give-error-undefined-symbols-for-architecture-x86-64/41414836. – Cristik Feb 01 '19 at 07:05
  • So, what I am picking up from that is... You can't test an executable. The way to do it is break your project into one or more (testable) library modules... then one (non-testable) executable that pretty much only contains the main.swift file? – Justin Reusch Feb 01 '19 at 16:30
  • Seems so. Regular Xcode projects allow setting a host app for unit tests, thus allowing directly testing the application code, however not sure if this can be via via SPM. – Cristik Feb 01 '19 at 16:32
  • Cool... I can work with that. I'd guess setting a host app will certainly to SPM in the near future. For now, this will work! Thanks! – Justin Reusch Feb 01 '19 at 16:42
  • It worked! Thanks! – Justin Reusch Feb 02 '19 at 06:25

1 Answers1

26

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.

Justin Reusch
  • 624
  • 7
  • 14