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];
}
有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top