XCTestCase的setUp方法的目的是什么? [英] What is the purpose of XCTestCase's setUp method?

查看:157
本文介绍了XCTestCase的setUp方法的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 XCTestCase 的默认模板中关于 setUp 的评论:

Per the comment within the default template for XCTestCase regarding setUp :

将设置代码放在此处;它将在第一个测试用例之前运行一次。

但是,在 XCTestCase.h ,上面的注释 setUp 说明不同:

However, in XCTestCase.h, the comment above setUp states differently:

之前调用的安装方法调用类中的每个测试方法。

为了确认实际行为,我放了一个 NSLog setUp 内计算它被调用的次数:

To confirm the actual behavior, I put an NSLog withinsetUp to count how many times it was called:

static int count = 0;

- (void)setUp
{
    [super setUp];
    count++;

    NSLog(@"Call Count = %d", count);
}

这导致 setUp 在每个测试方法之前调用方法(确认 XCTestCase.h 上的注释)。

This resulted in the setUp method being called before every test method (confirming the comment on XCTestCase.h).

我想要使用 setUp 方法创建测试/模拟对象一次(例如,设置Core Data测试堆栈)。一遍又一遍地创建这些将是处理器密集型的,并且可能非常慢。

I wanted to use the setUp method to create test/mock objects once (e.g. to setup a Core Data test stack). Creating these over and over again would be processor intensive and potentially very slow.

所以,

1)什么 setUp 实际上是打算用于?当然开发人员不是一遍又一遍地在其中创建对象?

1) What is setUp actually intended to be used for? Surely developers aren't creating objects in it over and over?

2)如何在<$ c中仅一次创建这些对象$ c> XCTestCase ?

2) How can I create these objects only once within an XCTestCase?

推荐答案

这里有几点需要讨论: setUp 方法和一般最佳测试实践。

There are a couple of points for discussion here: the behaviour of the setUp methods, and general best testing practices.

实际上有两个 setUp 方法:

+ (void)setUp;
- (void)setUp;

类方法( +(void)setUp )仅在整个测试运行期间运行一次。

The class method (+ (void)setUp) is only run once during the entire test run.

实例方法( - (void)setUp )是默认模板中的一个;它在每次测试之前运行。希望在Xcode的假设未来版本中,此注释将更改为 //将设置代码放在此处。在调用类中的每个测试方法之前调用此方法。 WINK WINK

The instance method (- (void)setUp) is the one in the default template; it's run before every single test. Hopefully, in a hypothetical future version of Xcode, this comment will have been changed to // Put setup code here. This method is called before the invocation of each test method in the class. WINK WINK

所以通过这两种方法,你的两个行为描述是可能的。

So through these two methods, both of the behaviours you described are possible.

关于你的评论:


当然开发人员不是'在其中反复创建对象?

"Surely developers aren't creating objects in it over and over?"

我的答案是是的,它们通常都是。 '良好'单元测试的流行首字母缩写是FIRST:

My answer would be "Yes, they usually are". A popular acronym for 'good' unit tests is FIRST:


  • 快速

  • 隔离

  • 可重复

  • 自我验证

  • 及时

  • Fast
  • Isolated
  • Repeatable
  • Self-Verifying
  • Timely

隔离是此讨论的关键:您的测试不应该依赖于其他测试留下的任何先前状态。理想情况下,您应该为每次测试拆除并重新创建内存中的Core Data堆栈,这样您就知道您从一个干净的平板开始。一个很好的例子是 Graham Lee的这篇文章。你想使用内存堆栈,因为a)你可以很容易地扔掉它,而b)它应该非常快,因为它只是在内存中而不是你的磁盘。

Isolated is key to this discussion: your tests shouldn't rely on any previous state left behind by other tests. Ideally, you should tear down and recreate your in-memory Core Data stack for every test, so you know that you're starting from a clean slate. A good example is in this post by Graham Lee. You want to use an in-memory stack because a) you can easily throw it away, and b) it should be very fast because it's just in-memory and not hitting your disk.

如果你发现你的测试运行缓慢(不要过早优化),那么我认为合理的下一步是在你的<$中创建堆栈c $ c> +(void)setUp 方法,但每次在 - (void)setUp 方法中创建一个全新的上下文。

If you find that your tests are running slowly as a result of this (don't optimize prematurely), then I think a reasonable next step would be to create the stack in your + (void)setUp method, but create a brand new context every time in your - (void)setUp method.

这篇关于XCTestCase的setUp方法的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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