为什么我应该使用单独的测试目标来运行 XCTests,我应该怎么做? [英] Why should I use a separate test target for running XCTests and how should I do that?

查看:29
本文介绍了为什么我应该使用单独的测试目标来运行 XCTests,我应该怎么做?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾经问了一个与 XCTests 相关的问题.在其中一个答案中,我被告知在运行单元测试(至少在 iOS 开发中)时使用单独的测试目标(主应用程序除外)是一种常见做法.我试图找到一些关于它的来源,但我找不到

我明白,这可能是最佳实践,所以我真的很想了解它.有人可以向我解释为什么它很重要,我从中得到什么好处以及我应该如何去做?非常感谢提供一些解释该问题的文章的链接.

附言我知道我需要特殊的测试环境(假内存数据库、模拟网络层等),但到目前为止,我设法在没有单独测试主机的情况下实现了它.但我相信可能有更好的方法.

解决方案

回答你的问题:

  1. 为什么使用单独的测试目标很重要?

关注点分离.单元测试目标与常规应用目标的结构不同——它包含一个测试包,它引用被测应用目标,以及测试类和任何所需的帮助程序类.它不是添加了测试代码的应用程序目标的副本——事实上,它甚至不应该包含被测代码.这意味着您不必做任何特别的努力来使测试目标与主应用程序目标保持同步 - 事实上,测试包加载主应用程序会为您处理所有这些.(您对 Richard Ross 对您之前的问题的回答发表了评论,暗示您已经遇到了重复导致的困难).

  1. 我该怎么做?(在 Xcode 7 上检查).

假设您要向没有任何测试的现有项目添加目标,请选择主项目,然后单击项目中目标列表下方的+".您也可以使用 File->New->Target 菜单选项.您将看到一个菜单,要求您为新目标选择模板.选择测试"并在测试中选择iOS 单元测试包".检查下一个屏幕中的字段 - 默认情况下它们应该是正确的 - 但如果项目/工作区中有很多目标,您可能需要仔细检查要测试的目标"字段值是否正确.单击确定",您应该有一个正常运行的单元测试包,以及一个示例测试,您应该能够使用 Apple-U 或 Product->Test 运行该测试您仍然需要 #import应用类(如果您使用的是 ObjC).

如果您要创建新项目,只需在创建项目时勾选包括单元测试"框 - 无需其他步骤.

  • Apple 文档(带有相关 WWDC 演示文稿的链接)

  • 教程.周围的大多数教程都有些过时了.但并没有太大变化,所以只需查看文档并弄清楚.下面的两个可能有用,否则就谷歌.乍一看,他们中的大多数人似乎认为该项目在开始时设置了单元测试

http://www.raywenderlich.com/22590/beginning-automated-testing-with-xcode-part-12(2012/iOS 6,但过程仍然大致相同.还涉及 Jenkins、Git 和从 CLI 运行测试).>

OSX 中的单元测试 - 最近的帖子 - 我知道不是 iOS,但比大多数最新的iOS 教程(2015 年 10 月),并提供分步说明(包括在构建方案中设置单元测试目标,在您的情况下可能不需要).无论如何可能值得一看.

I once asked a question related to XCTests. And in one of the answers I was told that it is a common practice to use a separate test target (other than the main app) when running unit tests (at least, in iOS development). I tried to find some sources about it, but I couldn't

I understand, that it is probably a best practice, so I would really like to understand it. Could someone explain to me why is it important, what benefits do I get from it and how should I go about doing it? Links to some articles explaining the issue will be much appreciated.

P.S. I understand that I need special environment for tests (fake in-memory database, mocked networking layer, etc.), but up until now I managed to achieve it without a separate test host. But I believe that there might be a better way.

解决方案

To answer your points:

  1. Why is using a separate test target important?

Separation of concerns. A unit test target does not have the same kind of structure as a regular app target - it contains a test bundle, which references the app target under test, as well as the test classes and any helper classes required. It is not a duplicate of the app target with test code added to it - in fact it should not even contain the code under test. This means that you don't have to make any special effort to keep the test target in sync with the main app target - the fact that the test bundle loads the main app handles all that for you. (You made a comment on Richard Ross's answer to your previous q suggesting you've run into the difficulties duplication causes already).

  1. How should I go about doing this? (checked on Xcode 7).

Assuming you are wanting to add a target to an existing project that doesn't have any tests, select the main project, and then click on the '+' beneath the list of targets in the project. You can also use the File->New->Target menu option. You'll see a menu asking you to choose a template for your new target. Select 'Test' and within test select 'iOS Unit Testing Bundle'. Check the fields in the next screen - they should be correct by default - but you might want to double check the 'Target to be Tested' field value is correct if you have a lot of targets in the project/workspace. Click 'OK' and you should have a functioning unit test bundle, with an example test that you should be able to run using Apple-U or Product->Test You'll still need to #import the app classes if you're using ObjC.

If you are creating a new project, all you need to do is tick the 'Include Unit Test' box when creating the project - no other steps are required.

  • Apple Docs (with links to relevant WWDC presentations)

  • Tutorials. Most of the tutorials around are a bit out of date. But not that much has changed, so just look at the docs and figure it out. The two below might be useful, otherwise just google. From a quick glance, most of them seem to assume that the project had unit tests set up at the beginning

http://www.raywenderlich.com/22590/beginning-automated-testing-with-xcode-part-12 (2012/iOS 6, but the process is still broadly the same. Also deals with Jenkins, Git and running tests from the CLI).

Unit testing in OSX - most recent post - not iOS, I know, but more up to date than most of the iOS tutorials (Oct 2015), and gives step by step instructions (including setting the unit test target in the build schemes, which probably won't be necessary in your case). Probably worth a look anyway.

这篇关于为什么我应该使用单独的测试目标来运行 XCTests,我应该怎么做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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