Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests and swift test not working because 'Cannot load underlying module for XCTest' #358

Closed
funky-monkey opened this issue Aug 15, 2018 · 10 comments

Comments

@funky-monkey
Copy link

Hi,

I've successfully installed the exercism CLI app and checked out the first two challenges for the Swift track. I'm really eager to get started, but I've run into some problems...

The problem i'm facing is the following:
First thing I've noticed is that the Package.swift does not conform to the latest version and does not contain the tool directive in comments (no idea what the official name of this is): // swift-tools-version:4.0. Apparently this is mandatory. I've found that here
If i omit this directive it compiles via swift build. However when I add this directive, I get different error messages. When I then run either swift build or swift test (like it says in your guide) the SPM(Swift Package Manager) returns with a warning: no targets to build in package to me.

What i've tried so far:

  • I've generated a new project via the spm with --type executable. This generates a more complete Package.swift file. I wanted to see if this gives me the same results when using swift build/test. It did. I've also added the testTarget to the targets. It should look something like this:
// 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: "Testing",
    dependencies: [],
    targets: [
        .target(
            name: "Testing",
            dependencies: []),
        .testTarget(
            name: "TestingTests",
            dependencies: ["Testing"]
        ),
    ]
)

One thing that is different from your project than from the project that i've generated via spm, is that the spm project also generates another folder in the Sources folder with the name of the project. In my case that was Testing. In there is a .swift file called Testing. If you try to run swift test without this change it throws an error like this error: could not find target(s): Testing; use the 'path' property in the Swift 4 manifest to set a custom target path.

When I run the tests now it gives me an error: "xcrun: error: unable to find utility "xctest", not a developer tool or in PATH". Running xctest can be done via xcrun xctest in the Terminal. This give a bunch of output and there is a path in the Arguments node.

Arguments: (
    "/Applications/Xcode.app/Contents/Developer/usr/bin/xctest"
)

So following the the hint from the error I've added the "/Applications/Xcode.app/Contents/Developer/usr/bin/" to my path via export PATH="$PATH:/Applications/Xcode.app/Contents/Developer/usr/bin/". To confirm that it works check it with an echo $PATH.

Now when calling swift test I get to see the correct output, tests pass and it all works!

Compile Swift Module 'Testing' (1 sources)
Compile Swift Module 'TestingTests' (1 sources)
Linking ./.build/x86_64-apple-macosx10.10/debug/TestingPackageTests.xctest/Contents/MacOS/TestingPackageTests
Test Suite 'All tests' started at 2018-08-15 21:52:56.705
Test Suite 'TestingPackageTests.xctest' started at 2018-08-15 21:52:56.706
Test Suite 'TestingTests' started at 2018-08-15 21:52:56.706
Test Case '-[TestingTests.TestingTests testANameGiven]' started.
Test Case '-[TestingTests.TestingTests testANameGiven]' passed (0.204 seconds).
Test Case '-[TestingTests.TestingTests testAnotherNameGiven]' started.
Test Case '-[TestingTests.TestingTests testAnotherNameGiven]' passed (0.000 seconds).
Test Case '-[TestingTests.TestingTests testNoNameGiven]' started.
Test Case '-[TestingTests.TestingTests testNoNameGiven]' passed (0.000 seconds).
Test Suite 'TestingTests' passed at 2018-08-15 21:52:56.911.
	 Executed 3 tests, with 0 failures (0 unexpected) in 0.205 (0.205) seconds
Test Suite 'TestingPackageTests.xctest' passed at 2018-08-15 21:52:56.911.
	 Executed 3 tests, with 0 failures (0 unexpected) in 0.205 (0.205) seconds
Test Suite 'All tests' passed at 2018-08-15 21:52:56.911.
	 Executed 3 tests, with 0 failures (0 unexpected) in 0.205 (0.206) seconds

If that still does not work; After searching github and SO I've found this which talks about Why isn't swift build able to link XCTest?.

They say this:

The XCTest framework on Darwin isn't a proper SDK framework, and is located at a different location within the Platform directory structure than frameworks like Foundation.

The solution they propose is adding the search paths to swift build like so

swift build \
-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

When doing the same but for swift test I get to see the correct output, tests pass and it all works!

What I've also done (and later removed because swift test worked regardless of adding the search paths) is to create an alias which includes the search paths so you only have to type swift test and be done with it.

alias 'swift test'='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'

Hope this helps anyone who has run into the same problems. Or maybe add this to your docs if people have the same problem.

Kind regards,
Sidney de Koning

@masters3d
Copy link
Contributor

What version of Swift are you using with SPM?

@masters3d
Copy link
Contributor

We have a ticket tracking package update. #297
The current package should work up to 4.1

I am not sure about 4.2. Follow this thread if you are interested:
https://forums.swift.org/t/removing-packagedescription-api-v3-master-branch/14109/12

@funky-monkey
Copy link
Author

Hi @masters3d, I'm running the following:

Apple Swift version 4.1.2 (swiftlang-902.0.54 clang-902.0.39.2)
Target: x86_64-apple-darwin17.7.0

Cheers,
Sidney

@masters3d
Copy link
Contributor

That should work. Try reinstalling Xcode and the command line tools for Xcode. Also you could instead just use swift package to generate an Xcode project

@funky-monkey
Copy link
Author

It does work now - just originally swift test does not work out of the box when doing commandline only Swift (not Xcode)

@masters3d
Copy link
Contributor

swift test is how we do all of our tests on travis CI.

@masters3d
Copy link
Contributor

masters3d commented Aug 19, 2018

We do the same with Circle CI with macOS. The only target do not test is windows because there is no Swift windows yet.

swift test -C $d

@funky-monkey
Copy link
Author

It does work now - just originally swift test does not work out of the box when doing commandline only Swift (not Xcode).

The reason for this issue was the incompleteness in your guide and wanted to chip in to make them more complete when people run into problems.

Hope this help and it gets added to the guide ;-)

Kind regards,
Sidney

@masters3d
Copy link
Contributor

Sounds good. Any improvement PR to our are documentation is more than welcome. Feel free to open up a new issue specifically about it or submit a PR.

@masters3d
Copy link
Contributor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants