Skip to content

Instantly share code, notes, and snippets.

@jml
Last active September 10, 2018 15:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jml/c85755830abb0c9eda5d310917a59d4e to your computer and use it in GitHub Desktop.
Save jml/c85755830abb0c9eda5d310917a59d4e to your computer and use it in GitHub Desktop.
swift test on macOS 10.13.6 (17G65)

Current status

Resolved.

What am I trying to do?

Run XCTestCase-using tests in Swift on my Mac with swift test.

When I do so, I get the error error: cannot load underlying module for 'XCTest'

How do I fix it?

Go into Xcode, then Preferences -> Locations -> Command Line Tools, and select a version of command line tools.

Details

What is the full error message?

$ swift test
Compile Swift Module 'HelloTests' (1 sources)
/Users/jml/src/hello/Tests/HelloTests/HelloTests.swift:1:8: error: cannot load underlying module for 'XCTest'
import XCTest
       ^
error: terminated(1): /Library/Developer/Toolchains/swift-4.1.3-RELEASE.xctoolchain/usr/bin/swift-build-tool -f /Users/jml/src/hello/.build/debug.yaml test output:

Why am I trying to do such a thing?

  • I've got a long habit of running things from the command-line
  • documentation for swift-nio implies that it should just work
  • I don't want to learn xCode at the same time as Swift, if I can avoid it

What have I tried?

The above is more or less a summary, but I've tried running swift test with both the system-provided swift binary at /usr/bin/swift, and the one installed by the 4.1.3 release at swift.org.

The first thing I tried was running swift test on swift-nio, but that failed with the same error. Once that happened, I tried a minimal example.

I've asked on #swift-lang on Freenode with minimal success. I've also asked on #swift-linux (on advice from #swift-lang) with no response yet.

What worked

Selecting the location of the command-line tools within the Xcode UI, as per this email thread.

Why I didn't try this earlier

I'm pretty sure I've stumbled across advice that said to be sure that Xcode Command Line Tools were installed, but I had thought they were installed.

I thought so, because whenever I tried to install them I was told they were installed:

$ xcode-select --install
xcode-select: error: command line tools are already installed, use "Software Update" to install updates
modified Package.swift
@@ -15,5 +15,8 @@ let package = Package(
.target(
name: "hello",
dependencies: []),
+ .testTarget(
+ name: "HelloTests",
+ dependencies: []),
]
)
new file Tests/HelloTests/HelloTests.swift
@@ -0,0 +1,7 @@
+import XCTest
+
+class HelloTests : XCTestCase {
+ func testSimple() throws {
+ XCTAssertEqual(2, 1 + 1)
+ }
+}
# as found on https://github.com/exercism/swift/issues/358#issue-350962700
$ swift test -Xcc -F -Xcc /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks -Xlinker -F -Xlinker /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks
Compile Swift Module 'HelloTests' (1 sources)
Compile Swift Module 'hello' (1 sources)
Linking ./.build/x86_64-apple-macosx10.10/debug/hello
Linking ./.build/x86_64-apple-macosx10.10/debug/helloPackageTests.xctest/Contents/MacOS/helloPackageTests
Test Suite 'All tests' started at 2018-09-10 15:04:34.558
Test Suite 'helloPackageTests.xctest' started at 2018-09-10 15:04:34.559
Test Suite 'HelloTests' started at 2018-09-10 15:04:34.559
Test Case '-[HelloTests.HelloTests testSimple]' started.
Test Case '-[HelloTests.HelloTests testSimple]' passed (0.120 seconds).
Test Suite 'HelloTests' passed at 2018-09-10 15:04:34.678.
Executed 1 test, with 0 failures (0 unexpected) in 0.120 (0.120) seconds
Test Suite 'helloPackageTests.xctest' passed at 2018-09-10 15:04:34.679.
Executed 1 test, with 0 failures (0 unexpected) in 0.120 (0.120) seconds
Test Suite 'All tests' passed at 2018-09-10 15:04:34.679.
Executed 1 test, with 0 failures (0 unexpected) in 0.120 (0.120) seconds
import XCTest
class HelloTests : XCTestCase {
func testSimple() throws {
XCTAssertEqual(2, 1 + 1)
}
}
jml@gallantry:~/src/hello (master) (0)
$ swift --version
Apple Swift version 4.1.3 (swift-4.1.3-RELEASE)
Target: x86_64-apple-darwin17.7.0
jml@gallantry:~/src/hello (master) (0)
$ swift test
Compile Swift Module 'HelloTests' (1 sources)
/Users/jml/src/hello/Tests/HelloTests/HelloTests.swift:1:8: error: cannot load underlying module for 'XCTest'
import XCTest
^
error: terminated(1): /Library/Developer/Toolchains/swift-4.1.3-RELEASE.xctoolchain/usr/bin/swift-build-tool -f /Users/jml/src/hello/.build/debug.yaml test output:
// swift-tools-version:4.0
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "hello",
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
.target(
name: "hello",
dependencies: []),
.testTarget(
name: "HelloTests",
dependencies: []),
]
)
jml@gallantry:~/src/hello (master) (0)
$ /usr/bin/swift --version
Apple Swift version 4.1.2 (swiftlang-902.0.54 clang-902.0.39.2)
Target: x86_64-apple-darwin17.7.0
jml@gallantry:~/src/hello (master) (0)
$ /usr/bin/swift test
Compile Swift Module 'HelloTests' (1 sources)
/Users/jml/src/hello/Tests/HelloTests/HelloTests.swift:1:8: error: cannot load underlying module for 'XCTest'
import XCTest
^
error: terminated(1): /Library/Developer/CommandLineTools/usr/bin/swift-build-tool -f /Users/jml/src/hello/.build/debug.yaml test output:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment