如何让应用知道它的运行单元是否在纯Swift项目中测试? [英] How to let the app know if its running Unit tests in a pure Swift project?

查看:89
本文介绍了如何让应用知道它的运行单元是否在纯Swift项目中测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在XCode 6.1中运行测试时,一件烦人的事情是整个应用程序必须运行并启动其storyboard和root viewController。在我的应用程序中,这会运行一些获取API数据的服务器调用。但是,我不希望应用程序在运行测试时执行此操作。

One annoying thing when running tests in XCode 6.1 is that the entire app has to run and launch its storyboard and root viewController. In my app this runs some server calls that fetches API data. However, I don't want the app to do this when running its tests.

随着预处理器宏的消失,对于我的项目来说最好的是要知道它已经启动运行测试而不是普通的发布?我用CMD + U和机器人正常运行它们。

With preprocessor macros gone, whats the best for my project to be aware that it was launched running tests and not an ordinary launch? I run them normally with CMD+U and on a bot.

伪代码将是:

// Appdelegate.swift

if runningTests() {
   return
} else {
   // do ordinary api calls
}


推荐答案

您可以在没有主机的情况下运行测试,而不是检查测试是否正在运行以避免副作用应用本身。转到项目设置 - >选择测试目标 - >常规 - >测试 - >主机应用程序 - >选择无。
只需记住包含运行测试所需的所有文件,以及Host app target通常包含的库。

Instead of checking if the tests are running to avoid side-effects, you could run the tests without the host app itself. Go to Project Settings -> select the test target -> General -> Testing -> Host Application -> select 'None'. Just remember to include all files you need to run the tests, as well as libraries normally included by the Host app target.

这篇关于如何让应用知道它的运行单元是否在纯Swift项目中测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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