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

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

问题描述

在Xcode 6.1中运行测试时,一件令人讨厌的事情是,整个应用程序必须运行并启动其情节提要和根视图控制器.在我的应用程序中,这会运行一些获取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 view controller. 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.

在没有预处理器宏的情况下,让我的项目知道它是运行测试而不是普通启动的最佳方式是什么?我通常使用 command + U 在机器人上运行它们.

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

伪代码:

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

推荐答案

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

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天全站免登陆