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

查看:14
本文介绍了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:

在调用类中的每个测试方法之前调用Setup方法.

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

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) 如何在 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 的假设未来版本中,此注释已更改为 //Put setup code here.该方法在类中每个测试方法调用之前调用. 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:

  • 快速
  • 隔离
  • 可重复
  • 自我验证
  • 及时

隔离是本次讨论的关键:您的测试不应该依赖于其他测试留下的任何先前状态.理想情况下,您应该为每个测试拆除并重新创建内存中的 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.

如果您发现您的测试因此而运行缓慢(不要过早优化),那么我认为合理的下一步是在您的 中创建堆栈+ (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天全站免登陆