iOS/Test

테스트 기초 - UNIT TEST

@서비 2022. 4. 18. 22:40

1. 설정

 

가지고 있는 프로젝트의 좌측 상단 내비게이션 바에서 체크된 다이아몬드 모양을 클릭하고 좌측 하단의+ 를 누르면 unit test를 추가할 수 있다. New unit test target 선택하고 현재 프로젝트 선택

 

 

 

 

2. 기본 코드 생성

아래와 같이 기본 소스 코드가 생성된다.

import XCTest

class ActiveLabelTestTests: XCTestCase {

    override func setUpWithError() throws {
        // Put setup code here. This method is called before the invocation of each test method in the class.
    }

    override func tearDownWithError() throws {
        // Put teardown code here. This method is called after the invocation of each test method in the class.
    }

    func testExample() throws {
        // This is an example of a functional test case.
        // Use XCTAssert and related functions to verify your tests produce the correct results.
        // Any test you write for XCTest can be annotated as throws and async.
        // Mark your test throws to produce an unexpected failure when your test encounters an uncaught error.
        // Mark your test async to allow awaiting for asynchronous code to complete. Check the results with assertions afterwards.
    }

    func testPerformanceExample() throws {
        // This is an example of a performance test case.
        measure {
            // Put the code you want to measure the time of here.
        }
    }

}

 

 

2. 테스트 코드 작성

적절한 테스트 함수가 없어서 다른 블로그에서 가져왔다.

작업 과정을 보면

1) @testable로 테스트할 프로젝트를 import 한다.

2) 테스트할 swift file을 sut라는 이름의 object로 setUpWithError에서 할당한다.

3) 테스트가 끝날 때, tearDownError에서 해제를 한다.

4) 테스트 함수를 작성한다. 변수를 설정해서 sut 오브젝트의 isValidEmail 함수에 인자로 넣어주고, 에러가 발생할 경우 assert 가 되도록 한다. test 함수는 test로 시작해야 한다.

import XCTest
@testable import ActiveLabelTest	//테스트할 프로젝트 

class ActiveLabelTestTests: XCTestCase {
    
    var sut:ViewController!		//테스트할 swift file 을 object 로 가져오기 오기 위한 변수.
    struct User {
        let email:String
        let password:String
        
        init(email:String,password:String) {
            self.email = email
            self.password = password
        }
    }
    override func setUpWithError() throws {
        // Put setup code here. This method is called before the invocation of each test method in the class.
        sut = ViewController()		//테스트할 swift file 을 object 로 가져 온다.
        
    }

    override func tearDownWithError() throws {
        // Put teardown code here. This method is called after the invocation of each test method in the class.
        sut = nil					//테스트할 swift file 을 해제 한다.
    }
    
    								//실제 test할 파일 작성
    func testLoginValidator_WhenValidEmailProvided_ShouldReturnTrue(){
        //arange			변수 설정
        let user = User(email: "Fomagran6@naver.com", password: "1234")
        
        //act				실행
        let isValidEmail = sut.isValidEmail(email: user.email)
                
        //assert			문제 발생 시 assert
        XCTAssertTrue(isValidEmail,"isValidEmail은 True를 반환해야되는데 False를 반환했어 @를 포함시켜야 해!")
        
    }
}

 

 

 

참고

https://fomaios.tistory.com/entry/iOS-Unit-Test%EC%97%90-%EB%8C%80%ED%95%B4%EC%84%9C-%EA%B0%84%EB%8B%A8%ED%9E%88-%EC%95%8C%EC%95%84%EB%B3%B4%EA%B8%B0

 

 

 

2022.04.20 - [iOS] - iOS - UI TEST

 

iOS - UI TEST

1. 설정 내비게이션 바에서 6번 째로 이동해서 UI Test target 추가해 준다. 2. 튜토리얼 확인 UIView 상의 Object에 접근하는 방법이 Xcode 와는 다르기 때문에 그 부분을 먼저 확인해야 한다. 그리고 사용

iosdevhistory.tistory.com