简体   繁体   English

单击确定可以从XCTest中的其他测试用例中调用测试用例

[英]OK to call test cases from within other test cases in XCTest

I'm build a set of XCTestCase methods to exercise the code around my core data model. 我正在构建一组XCTestCase方法,以围绕核心数据模型执行代码。 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. 我无法想象为什么这行不通,但是我想知道世界在想什么,这是否被认为是好的做法。

Here's what it would look like: 如下所示:

@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. 问题是,如果测试相互依赖,可能很难查明哪里出了问题。

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 . 看看这个DAMP vs DRY测试答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM