How To Link XCTest Dependency To Production / Main Target?

走远了吗. 提交于 2019-11-30 22:41:41

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!

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