Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I run XCTest for a swift application from the command line?

Tags:

swift

xctest

I want to test drive some Swift examples using XCTest from the command line if possible.

import XCTest  class LeapTest : XCTestCase {      func testVanillaLeapYear() {       let year = Year(calendarYear: 1996)       XCTAssertTrue(year.isLeapYear);     } } 

I'd love to run it from the command line.

I already set Xcode to use the developer tools in the beta:

sudo xcode-select -s /Applications/Xcode6-Beta.app/Contents/Developer/ 

If I naively try and run it it goes like this

$ xcrun swift LeapTest.swift LeapTest.swift:1:8: error: cannot load underlying module for 'XCTest' import XCTest        ^ 

Any way to run it directly from the CLI? Or do I have to create a Xcode project?

like image 411
John Paul Ashenfelter Avatar asked Jun 16 '14 16:06

John Paul Ashenfelter


People also ask

How do I run unit tests in Xcode?

⌘U will build and run all your test cases. It is the most commonly used shortcut when creating unit test cases. It is equivalent to ⌘R (build & run) while doing app development. You can use this shortcut to build your test target and run all the test cases in your test target.

How do I run a test in swift package?

To run the tests from the command line, first cd to the exercise directory (for example, ~/exercism/swift/exercises/hello-world/), then execute swift test . This will compile the files in the Sources directory and execute the tests in the Tests directory. Alternatively, open the Xcode project and press Command-U.


1 Answers

I think the issue is you have your test.swift file under the main project's target membership. Make sure your swift test files belong to the Test target only.

like image 200
chourobin Avatar answered Sep 28 '22 05:09

chourobin