本文介绍了如何从命令行为 swift 应用程序运行 XCTest?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果可能,我想从命令行使用 XCTest 测试驱动一些 Swift 示例.

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);
    }
}

我想从命令行运行它.

我已经将 Xcode 设置为使用测试版中的开发人员工具:

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
       ^

有没有办法直接从 CLI 运行它?还是我必须创建一个 Xcode 项目?

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

推荐答案

我能够使用以下命令编译您的 XCTestCase:

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

然后您可以使用 xctest 执行测试:

Then you can execute the tests with xctest:

xcrun xctest LeapTests

并分解那些 swiftc 命令行选项:

And to break down those swiftc command line options:

  1. -F... 将 XCTest.framework 添加到框架搜索路径,使其能够从 Swift 导入
  2. -Xlinker -rpath ... 确保在加载时可以找到 XCTest 共享库
  3. 没有明确指定-lswiftCore,我发现xctest 在尝试运行测试套件时会崩溃
  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

希望有帮助!

这篇关于如何从命令行为 swift 应用程序运行 XCTest?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 05:39