0

Is it possible to call a testfunction from another Testcase with working XCTest Assertions ?

-(void)testExample {

    XCUIApplication *app = [[XCUIApplication alloc] init];

    TestLoginMethod *loginModule = [[TestLoginMethod alloc] init:app];
    [loginModule testExampleLogin];
}

I want to write test modules for easy reuse or to call them in different order. But my XCTAssert in the called function is not aborting the test if it is false.

- (void)testExampleLogin
{
XCUIElement *passwordSecureTextField = app.secureTextFields[@"Passwordaadsfs"];
XCTAssertTrue([passwordSecureTextField exists]);
XCTAssertTrue([passwordSecureTextField isHittable]);

In the called function, the XCTAssertTrue is just skipped although this textfield doesn't exist.

JackyCoke
  • 1
  • 1
  • testcases should be independent. In your case id made:--- `-(void)testExample{ [loginModuleHelper login]; } -(void) testExampleLogin{ [loginModuleHelper login]; } ... -(void) login{ XCUIElement *passwordSecureTextField = app.secureTextFields[@"Passwordaadsfs"]; XCTAssertTrue([passwordSecureTextField exists]); XCTAssertTrue([passwordSecureTextField isHittable]); }` – Che Feb 01 '16 at 07:32
  • You may find discussion on thread [link](http://stackoverflow.com/questions/21948887/ok-to-call-test-cases-from-within-other-test-cases-in-xctest) useful. – Yogesh Khatri Feb 04 '16 at 12:55

1 Answers1

0

Rather than putting shared logic into tests, factor out that logic into non-test functions, ie:

- (void) login {
    // Do login
    XCTAssert(YES);
}

- (void) testLogin {
    [self login];
}

- (void) testExample {
   [self login];
   // do some other stuff
   XCTAssert(YES);
}

Xcode runs into issues when you call functions prepended with test from other tests, but doesn't care if you have common test logic in non-test functions.

Chase Holland
  • 2,178
  • 19
  • 23