是否可以在iOS应用程序中运行XCTest测试? [英] Is it possible to run XCTest tests in an iOS app?

查看:1162
本文介绍了是否可以在iOS应用程序中运行XCTest测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个用XCTest编写的后端测试套件。该套件在Xcode中运行良好,但由于各种原因,如果我们也可以在iOS应用程序中运行该套件,那将对我们很好。那可能吗?我不介意为它编写一些胶水代码,但因为它甚至无法在非测试目标中导入XCTest框架:

We have a backend test suite written with XCTest. The suite runs great in Xcode, but for various reasons it would be nice for us if we could also run the suite in an iOS app. Is that possible? I don’t mind writing some glue code for it, but as it is I can’t even import the XCTest framework in a non-testing target:

SomeController.swift:2:8: Cannot load underlying module for 'XCTest'


推荐答案

有可能!关键是获取XCTest框架链接的副本,然后使用公共XCTest API来运行测试。这将为您提供在通过Xcode运行测试时看到的相同控制台输出。 (使用公共API连接自己的自定义记者看起来可行,但是询问如何这样做本身就会产生一个好问题 - 没有多少人使用XCTest API,因为Xcode为我们做了肮脏的工作。)

It is possible! The key is to get a copy of the XCTest framework linking and then use the public XCTest APIs to run your tests. This will give you the same console output you see when running the tests via Xcode. (Wiring up your own, custom reporter looks doable using the public APIs, but asking how to do so would make a good question in itself - not many people use the XCTest APIs, because Xcode does the dirty work for us.)

您可以复制XCTest,而不是解开任何破坏模块加载的内容。 .framework捆绑到您的项目来自您的目标平台的平台框架:

Rather than unravel whatever is breaking the module loading, you can copy the XCTest.framework bundle into your project from the platform frameworks for your target platform:

mkdir Frameworks && mkdir Frameworks/iphonesimulator
PLATFORMS=/Applications/Xcode.app/Contents/Developer/Platforms
FRAMEWORKS=Developer/Library/Frameworks
cp -Rp \
    "$PLATFORMS/iPhoneSimulator.platform/$FRAMEWORKS/XCTest.framework" \
    Frameworks/iphonesimulator/



链接自己的XCTest



然后,弹出打开主应用目标的目标编辑器,然后将您的副本从Finder拖放到链接的框架和库列表中。

Link Against Your Own XCTest

Then, pop open the target editor for your main app target, and drag and drop your copy from Finder to the list of "Linked Frameworks and Libraries".

现在,转到测试文件,弹出文件检查器,并勾选主应用程序目标旁边的框以查找这些文件。现在,您将构建测试文件作为主应用程序的一部分,将所有 XCTestCase 子类放入二进制文件中。

Now, go to your test files, pop open the File Inspector, and tick the box next to your main app target for those files. Now you're building the test files as part of your main app, which puts all your XCTestCase subclasses into the binary.

最后,将运行测试按钮连接到这样的操作:

Lastly, wire up a button to "Run Tests" to an action like this:

import UIKit
import XCTest

class ViewController: UIViewController {
    @IBAction func runTestsAction() {
        print("running tests!")
        let suite = XCTestSuite.default()
        for test in suite.tests {
            test.run()
        }
    }
}

当你点击按钮时,你会在控制台中看到这个:

When you tap the button, you'll see this in the console:

running tests!
Test Suite 'RunTestsInApp.app' started at 2017-05-15 11:42:57.823
Test Suite 'RunTestsInAppTests' started at 2017-05-15 11:42:57.825
Test Case '-[RunTestsInApp.RunTestsInAppTests testExample]' started.
2017-05-15 11:42:57.825 RunTestsInApp[2956:8530580] testExample()
Test Case '-[RunTestsInApp.RunTestsInAppTests testExample]' passed (0.001 seconds).
Test Case '-[RunTestsInApp.RunTestsInAppTests testPerformanceExample]' started.
/Users/jeremy/Workpad/RunTestsInApp/RunTestsInAppTests/RunTestsInAppTests.swift:34: Test Case '-[RunTestsInApp.RunTestsInAppTests testPerformanceExample]' measured [Time, seconds] average: 0.000, relative standard deviation: 122.966%, values: [0.000002, 0.000001, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000], performanceMetricID:com.apple.XCTPerformanceMetric_WallClockTime, baselineName: "", baselineAverage: , maxPercentRegression: 10.000%, maxPercentRelativeStandardDeviation: 10.000%, maxRegression: 0.100, maxStandardDeviation: 0.100
Test Case '-[RunTestsInApp.RunTestsInAppTests testPerformanceExample]' passed (0.255 seconds).
Test Suite 'RunTestsInAppTests' passed at 2017-05-15 11:42:58.081.
     Executed 2 tests, with 0 failures (0 unexpected) in 0.256 (0.257) seconds
Test Suite 'RunTestsInApp.app' passed at 2017-05-15 11:42:58.081.
     Executed 2 tests, with 0 failures (0 unexpected) in 0.256 (0.258) seconds

这篇关于是否可以在iOS应用程序中运行XCTest测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆