可以从 XCTest 中的其他测试用例中调用测试用例 [英] OK to call test cases from within other test cases in XCTest

查看:58
本文介绍了可以从 XCTest 中的其他测试用例中调用测试用例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一组 XCTestCase 方法来围绕我的核心数据模型练习代码.我计划从其他测试方法调用一些测试方法,这样我就可以看到不同的数据组合,同时将代码保持在最低限度.我无法想象为什么这行不通,但我想知道世界是怎么想的,以及这是否被认为是好的做法.

I'm build a set of XCTestCase methods to exercise the code around my core data model. I am planning on calling some of the test methods from other test methods so I can see different combinations of data while keeping the code to a minimum. I can't imagine why this would not work, but I'm wondering what the world thinks and whether this is considered to be good practice.

这是它的样子:

@interface sessionTests : XCTestCase
@property (strong) Model *model;
@end
- (void)setUp
{
    [super setUp];    
    _model = [[Model alloc]init];
}

- (void) testValue1
{
    _model.value = 1;
    XCTAssertTrue(_model.value == 1, @"model.value is not 1");
}

- (void) testValue2
{
    _model.value = 2;
    XCTAssertTrue(_model.value == 2, @"model.value is not 2");
}

- (void) testStringHello
{
    _model.str = @"Hello";
    XCTAssertTrue([_model.str isEqualToString:@"Hello"], @"model.str is not Hello");
}

- (void) testHello1
{
    [self testValue1];
    [self testStringHello];
}

- (void) testHello2
{
    [self testValue2];
    [self testStringHello];
}

推荐答案

测试相互依赖并不是很好的做法.他们应该能够以自己的方式执行.

Its not considered good practice for tests to depend on each other. They should be able to execute in their own right.

问题在于,如果测试相互依赖,则很难查明哪里出了问题.

The problem is that if tests depend on each other it can be difficult to pinpoint where something has gone wrong.

因此,单元测试应该是自包含的并且尽可能容易理解.事实上,为了实现这个自成一体、易于理解的目标,我们经常会放弃一些我们通常会坚持的做法,例如代码重复.看看这个 DAMP vs DRY 测试答案.

So, unit tests should be self contained and as easy to understand as possible. In fact we often forgo some of the practices we might normally adhere to, such as with code duplication, in order to achieve this self-contained, easy to understand goal. Take a look at this DAMP vs DRY tests answer.

这篇关于可以从 XCTest 中的其他测试用例中调用测试用例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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