ios 运行单元测试时不要启动模拟器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18134670/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:39:21  来源:igfitidea点击:

Don't launch simulator when running unittests

iosxcodejenkins

提问by Victor Ronin

Some background:

一些背景:

I have iOS application with a target configured to run unitTests. And I am running build automation tool jenkins on my MacBook which automatically builds this application and run all tests (using command line xcodebuild tool).

我有一个目标配置为运行 unitTests 的 iOS 应用程序。我正在我的 MacBook 上运行构建自动化工具 jenkins,它会自动构建这个应用程序并运行所有测试(使用命令行 xcodebuild 工具)。

Everything worked fine with Xcode 4. This build automation tool was running under different user and was running all these tests.

Xcode 4 一切正常。这个构建自动化工具在不同的用户下运行,并运行所有这些测试。

I switched to Xcode 5 recently and it started to fail, because it can't launch Simulator.

我最近切换到 Xcode 5,它开始失败,因为它无法启动模拟器。

The problem

问题

I have a scheme UnitTests which is configured to run tests (logic tests). A I run these test using one of two methods:

我有一个计划 UnitTests,它被配置为运行测试(逻辑测试)。AI 使用以下两种方法之一运行这些测试:

  • Command U in Xcode

  • Or command line "/usr/bin/xcodebuild -scheme UnitTests -sdk iphonesimulator -configuration Release clean build TEST_AFTER_BUILD=YES "

  • Xcode 中的命令 U

  • 或者命令行“/usr/bin/xcodebuild -scheme UnitTests -sdk iphonesimulator -configuration Release clean build TEST_AFTER_BUILD=YES”

In both cases, it tries to start simulator. However, per my understand it doesn't need it. Anyway it runs on top x86 and it doesn't look like any apps are installed on Simulator.

在这两种情况下,它都会尝试启动模拟器。但是,根据我的理解,它不需要它。无论如何,它运行在顶级 x86 上,看起来模拟器上没有安装任何应用程序。

Is there a way to get rid of this pesky simulator start (because it breaks my build automation)?

有没有办法摆脱这个讨厌的模拟器启动(因为它破坏了我的构建自动化)?

Update 1

更新 1

Seems to find very similar question, but can't get it working: Run logic tests in Xcode 4 without launching the simulator

似乎找到了非常相似的问题,但无法使其正常工作: 在 Xcode 4 中运行逻辑测试而不启动模拟器

Update 2

更新 2

I found VERY relevant and interesting question/answer: Apple CI / Xcode Service and Jenkins

我发现了非常相关和有趣的问题/答案:Apple CI / Xcode Service and Jenkins

回答by rustylepord

Using xCode 7 and xCtool.

使用 xCode 7 和 xCtool。

xctoolis capable of executing unit tests without the simulator.

xctool能够在没有模拟器的情况下执行单元测试。

To get this working,

为了让这个工作,

1 . Update target settings to run without a host app.

1 . 更新目标设置以在没有主机应用程序的情况下运行。

Select your project --> then test target --> Set the host application to none.

选择您的项目 --> 然后测试目标 --> 将宿主应用程序设置为无。

enter image description here

在此处输入图片说明

2. Install xctool , if you don't have it.

2.安装 xctool ,如果你没有它。

brew install xctool

3. Run the tests using terminal with xctool.

3. 使用带有 xctool 的终端运行测试。

xctool -workspace yourWorkspace.xcworkspace -scheme yourScheme run-tests -sdk iphonesimulator

回答by Saltymule

You can create a Mac OSX Unit Test instead of an iOS unit test. This requires that you not include any iOS specific libraries in the unit tests though. You can do this via the following:

您可以创建 Mac OSX 单元测试而不是 iOS 单元测试。不过,这要求您不要在单元测试中包含任何特定于 iOS 的库。您可以通过以下方式执行此操作:

  1. Select the project -> the target drop down -> "Add Target..."
  2. Select "Mac OSX" -> "Other" -> "Cocoa Unit Testing Bundle"
  3. Create the testing bundle as you would a normal project
  1. 选择项目 -> 目标下拉菜单 -> “添加目标...”
  2. 选择“Mac OSX”->“其他”->“Cocoa 单元测试包”
  3. 像创建普通项目一样创建测试包

You can now add sources to the unit test and run it like an iOS test without launching the simulator.

您现在可以将源添加到单元测试并像 iOS 测试一样运行它,而无需启动模拟器。

回答by rfrittelli

I've asked the same question to apple engineers. Unfortunately it doesn't seem you can accomplish this and stay with iOS at the same time. There are some tricks you can do to check if testing. You could put this code snippet in your AppDelegate.h or some other global class to say not load a root viewcontroller and prevent any wierdo ui stuff from corrupting your unit tests:

我问过苹果工程师同样的问题。不幸的是,您似乎无法做到这一点并同时使用 iOS。您可以使用一些技巧来检查是否正在测试。您可以将此代码片段放在 AppDelegate.h 或其他一些全局类中,以说明不加载根视图控制器并防止任何 wierdo ui 内容破坏您的单元测试:

static BOOL isTesting() {
    BOOL isTesting = !isEmpty([[[NSProcessInfo processInfo] environment] objectForKey:@"XCInjectBundle"]);
    return isTesting;
}

I've also had an apple engineer verify this is a legitimate check. And to give credit where credit is due, this is from: Programmatically determine current target (run or test) in iOS project

我还让一位苹果工程师验证这是一次合法的检查。并在信用到期时给予信用,这来自:以编程方式确定 iOS 项目中的当前目标(运行或测试)

EDIT:I've also had success with this and it's a little more straight forward:

编辑:我在这方面也取得了成功,而且更加直接:

static BOOL isTesting() {
    return [[[NSProcessInfo processInfo] processName] isEqualToString:@"xctest"];
}

回答by LimeRed

A osx test target can become a huge hassle because you have to manage yourself which source file to include. Putting @testable import YourAppNameon top of your XCTest files is way more convenient. So just prevent your app from launching in case of a XCTest run.

osx 测试目标可能会变得非常麻烦,因为您必须自己管理要包含的源文件。放在@testable import YourAppNameXCTest 文件之上更方便。因此,在 XCTest 运行的情况下,只需阻止您的应用程序启动即可。

In your AppDelegate put: (Swift 3 solution)

在您的 AppDelegate 中放置:(Swift 3 解决方案)

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
{
    if ProcessInfo.processInfo.environment["XCInjectBundleInto"] != nil {
        return false
    } 

...

This wont prevent the simulator from launching, but will save you a lot of time.

这不会阻止模拟器启动,但会为您节省大量时间。

回答by user1046037

Workaround:

解决方法:

App will still launch but you can #ifto define what you don't want to run.

应用程序仍将启动,但您可以#if定义不想运行的内容。

Approach:

方法:

  1. Create a custom build configuration called Testby duplicating Debug(Project > Info > create new configuration)
  2. In Build Settings> Active Compilation Conditionsfor Testadd TESTING
  3. Edit Scheme > Info > Build Configuration, set build configuration as Test
  4. Use #if !TESTING#endifaround the code you don't want to execute when testing.
  1. 创建一个Test通过复制调用的自定义构建配置Debug(Project > Info > create new configuration)
  2. Build Settings>Active Compilation ConditionsTest添加TESTING
  3. 编辑 Scheme > Info > Build Configuration,设置 build configuration 为 Test
  4. #if !TESTING#endif在测试时不要执行的代码周围使用。

Frameworks:

构架:

If you have embedded frameworks, create the same build configuration in the framework, so that the framework binary is properly linked.

如果您有嵌入式框架,请在框架中创建相同的构建配置,以便正确链接框架二进制文件。

回答by abdullahselek

You can run unit and ui tests in headless mode with Xcode 9and a command from your terminal. For reference sample commands:

您可以使用Xcode 9和终端命令以无头模式运行单元和 ui 测试。对于参考示例命令:

Building and testing a workspace

构建和测试工作区

xcrun xcodebuild -workspace "YOUR_WORKSPACE_NAME.xcworkspace" -scheme "YOUR_SCHEME" -sdk "iphonesimulator12.0" -destination "OS=12.0,name=iPhone X" -configuration Debug -enableCodeCoverage YES clean build test

For project

对于项目

xcrun xcodebuild -project "YOUR_PROJECT_NAME.xcodeproj" -scheme "YOUR_SCHEME" -sdk "iphonesimulator12.0" -destination "OS=12.0,name=iPhone X" -configuration Debug -enableCodeCoverage YES clean build test