如何将 XCTest 依赖与生产/主要目标联系起来? [英] How To Link XCTest Dependency To Production / Main Target?

查看:22
本文介绍了如何将 XCTest 依赖与生产/主要目标联系起来?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用 Swift 为 XCTest 框架编写扩展.为此,我创建了一个包含两个目标的项目:主/生产目标和测试目标.

当我正在为 XCTest 编写扩展时,我还需要在我的主要/生产目标中导入 XCTest.但是,我很难这样做.当我在 Xcode 中单击我的项目,然后选择主要目标,转到 Build Phases, Link Binary With Libraries 并在那里添加 XCTest 时,出现编译错误:

ld:未找到框架 XCTestclang:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)

我还尝试了

  1. 新 Swift 中用于启动测试的 ViewController 代码略有不同,以下是我使用的代码:

<预><代码>导入 UIKit导入 XCTest类视图控制器:UIViewController {覆盖 func touchesBegan(_ touches: Set, with event: UIEvent?) {打印(运行测试!")让套件 = XCTestSuite.default;在suite.tests中进行测试{测试运行()}}}

应该是这样!当我运行上面的应用程序,然后触摸屏幕时,来自我的 UITesting 目标的所有测试都完美地运行并通过了!

I am trying to write an extension for the XCTest framework in Swift. In order to do so, I created a project with two targets: the main/production target and a test target.

As I am writing an extension for XCTest, I need to import XCTest within my main/production target as well. However, I am having trouble to do so. When in Xcode and I click on my project, then select the main target, go to Build Phases, Link Binary With Libraries and add XCTest there, I get a compile error:

ld: framework not found XCTest clang: error: linker command failed with exit code 1 (use -v to see invocation)

I also tried the solution provided here which unfortunately doesn't work either.

解决方案

Auxiliary information on XCTest itself is sparse and hard to find, I was also chasing down the same functionality and finally managed to get it working.

I am on XCode 10.1 and running on a real iPhone with iOS 11. I am certain the general technique will work for other versions, but probably will require a few tweaks.

The general steps are described in this stackoverflow answer, but required several additional steps and tweaks to work for me on a real iPhone:

Is it possible to run XCTest tests in an iOS app?

Follow the steps in the above link. The below steps are deviations from those instructions that were required for me.

  1. Copy in the XCTest framework as described in the above link. NOTE: Use the framework for the iPhone.OS platform and not the simulator as it describes. You can find this framework file inside the actual XCode Application package on your mac. (Right click, "Show Package Contents", then look in ./Contents/Developer/Platforms/iPhoneOS.platform

  2. Disable bitcode in your app target. This solves a linker error. Here is an example of enabling it: how to ENABLE_BITCODE in xcode 7?

  3. When dragging the XCTest.framework file to the linked binaries in your target, ensure that you also drag it to the "Embedded Binaries" which is directly above the "Linked Frameworks and Libraries" option. If you don't do this you will get a runtime error.

  1. The ViewController code to start the tests is slightly different in new Swift, here is what I am using:


import UIKit
import XCTest

class ViewController: UIViewController {
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        print("running tests!")
        let suite = XCTestSuite.default;
        for test in suite.tests {
            test.run()
        }
    }

}

That should be it! When I run the above app, then touch the screen, all of the tests from my UITesting target run flawlessly and pass!

这篇关于如何将 XCTest 依赖与生产/主要目标联系起来?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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