如何从命令行运行快速应用程序的XCTest ?

时间:2021-09-11 21:08:24

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

如果可能的话,我想从命令行使用XCTest测试一些快速驱动示例。

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:

我已经设置了Xcode来使用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?

可以直接从CLI中运行它吗?还是我必须创建一个Xcode项目?

2 个解决方案

#1


11  

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

我可以用以下命令编译您的XCTestCase:

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:

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

xcrun xctest LeapTests

And to break down those swiftc command line options:

并打破这些swiftc命令行选项:

  1. -F... adds XCTest.framework to the framework search paths, enabling it to be imported from Swift
  2. - f……将XCTest.framework添加到框架搜索路径中,使其能够从Swift导入
  3. -Xlinker -rpath ... makes sure the XCTest shared library can be found at load time
  4. -Xlinker rpath……确保在加载时可以找到XCTest共享库
  5. Without explicitly specifying -lswiftCore, I found that xctest would crash when it tried to run the test suite
  6. 没有显式地指定-lswiftCore,我发现xctest在试图运行测试套件时将会崩溃

Hope that helps!

希望会有帮助!

#2


47  

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.

我认为问题是你有你的测试。swift文件下的主要项目的目标成员。确保您的swift测试文件只属于测试目标。

#1


11  

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

我可以用以下命令编译您的XCTestCase:

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:

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

xcrun xctest LeapTests

And to break down those swiftc command line options:

并打破这些swiftc命令行选项:

  1. -F... adds XCTest.framework to the framework search paths, enabling it to be imported from Swift
  2. - f……将XCTest.framework添加到框架搜索路径中,使其能够从Swift导入
  3. -Xlinker -rpath ... makes sure the XCTest shared library can be found at load time
  4. -Xlinker rpath……确保在加载时可以找到XCTest共享库
  5. Without explicitly specifying -lswiftCore, I found that xctest would crash when it tried to run the test suite
  6. 没有显式地指定-lswiftCore,我发现xctest在试图运行测试套件时将会崩溃

Hope that helps!

希望会有帮助!

#2


47  

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.

我认为问题是你有你的测试。swift文件下的主要项目的目标成员。确保您的swift测试文件只属于测试目标。