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

自闭症网瘾萝莉.ら 提交于 2019-11-29 02:14:40

问题


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?


回答1:


I was able to get your XCTestCase compiling with the following command:

swiftc \
-F/Applications/Xcode6-Beta5.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks \
-Xlinker -rpath -Xlinker /Applications/Xcode6-Beta5.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks \
-lswiftCore LeapTest.swift \
-o LeapTests

Then you can execute the tests with xctest:

xcrun xctest LeapTests

And to break down those swiftc command line options:

  1. -F... adds XCTest.framework to the framework search paths, enabling it to be imported from Swift
  2. -Xlinker -rpath ... makes sure the XCTest shared library can be found at load time
  3. Without explicitly specifying -lswiftCore, I found that xctest would crash when it tried to run the test suite

Hope that helps!




回答2:


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.



来源:https://stackoverflow.com/questions/24248439/how-can-i-run-xctest-for-a-swift-application-from-the-command-line

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!