OK to call test cases from within other test cases in XCTest

為{幸葍}努か 提交于 2019-12-11 01:59:18

问题


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.

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];
}

回答1:


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.



来源:https://stackoverflow.com/questions/21948887/ok-to-call-test-cases-from-within-other-test-cases-in-xctest

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!