[XCode] 유닛 테스트(Asynchronous) & UI테스트

XCode 유닛 테스트(asynchronous)

결과를 받을수 있는 코드 블록이 있는 asynchronous함수에 대한 테스트

XCTestExpectation *expectation = [self expectationWithDescription:@"asynchronous unit test"];
[test asyncHandler:^(BOOL success) {
    XCTAssertTrue(success);
    [expectation fulfill]; // 함수 진행 완료 후 호출
}];
[self waitForExpectationsWithTimeout:3 handler:nil];  // 3초 대기

코드 블록이 없는 asynchronous함수에 대한 테스트

NSPredicate *predicate = [NSPredicate predicateWithBlock:^BOOL(id  _Nullable evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) {
    // 10초 대기 시간 동안 성공할때 까지 연속으로 호출 된다.
    Test* test = (Test*)evaluatedObject;
    BOOL isValid = [test isValid];
    return isValid;
}];
Test *test = [Test new];
XCTNSPredicateExpectation *predicateTestExcectation = [[XCTNSPredicateExpectation alloc]initWithPredicate:predicate object:test];

[test asyncHandler];  // 코드 블록이 없는 asynchronous함수

XCTWaiterResult result = [XCTWaiter waitForExpectations:@[predicateTestExcectation] timeout:10];  // 10초 대기
if(result == XCTWaiterResultCompleted){
    XCTAssertTrue(TRUE, @"Pass");
}else{
    XCTAssertTrue(FALSE, @"Fail");
}

XCode UI테스트



일단 XCode UI테스트 타겟을 추가합니다. 화살표 부분에 커서를 위치 시킵니다.



비활성화 되어있던 Rec버튼(빨간색 버튼)이 활성화 됩니다.



Rec를 시작하고 시뮬레이터 UI에서 테스트를 합니다. 테스트를 끝내면 UI에서 테스트 했던 내용이 자동으로 코드로 생성 됩니다.

기타

@testable import

swift는 유닛테스트시 접근자가 있어서 저 키워드가 필요함..
Objective-C에는 해당 키워드가 없기 때문에 타겟을 추가해서 테스트 해야함.

시뮬레이터 생체 테스트

시뮬레이터에서 생체 테스트시 터미널로도 생체 인증을 할수 있음.

xcrun simctl spawn 'iPhone X' notifyutil -p com.apple.BiometricKit_Sim.fingerTouch.match
xcrun simctl spawn 'iPhone X' notifyutil -p com.apple.BiometricKit_Sim.fingerTouch.nomatch

참고 사이트


태그: ,

카테고리:

업데이트:

댓글남기기