티스토리 뷰

Develop/Mac OS

Unit test Swift

Nebori 2019. 1. 28. 17:03
UnitTest

Unit test Swift

Xcode에서 Unit test 코드 작성 및 실행하는 방법에 대해서 기록한 문서입니다.

 

버전 정보

현재 이 문서를 작성하는 기준 버전들을 기재합니다.

  • macOS Mojave 10.14

  • Xcode 10.1 (10B61)

    • Swift 4.2

 

직접 해보기

프로젝트 생성하기


 

Unit test 파일 들여다보기

//
//  UnitTestTests.swift
//  UnitTestTests
//
//  Created by 김인중 on 23/01/2019.
//  Copyright © 2019 nebori92. All rights reserved.
//

import XCTest
// 해당 프로젝트를 테스트 하기 위해 반드시 임포트
// @testable import [ProjectName]
@testable import UnitTest

// XCTestCase 클래스 상속
class UnitTestTests: XCTestCase {

    override func setUp() {
        // Put setup code here. This method is called before the invocation of each test method in the class.
        // 해당 Unit test 클래스의 테스트 메서드가 동작할 때
        // 동작 바로 전 준비하는 내용을 작성
    }

    override func tearDown() {
        // Put teardown code here. This method is called after the invocation of each test method in the class.
        // 해당 Unit test 클래스의 테스트 메서드가 동작할 때
        // 동작을 마치고 정리하는 내용을 작성
    }

    // 모든 테스트 메서드는 'test'로 시작해야 테스트 메서드로 작동됨. 중요!
    func testExample() {
        // This is an example of a functional test case.
        // Use XCTAssert and related functions to verify your tests produce the correct results
		
    }

    func testPerformanceExample() {
        // This is an example of a performance test case.
        // 퍼포먼스를 확인하는 기능이 있는 클로저
        self.measure {
            // Put the code you want to measure the time of here.
            // 성능을 테스트하고 싶은 코드를 작성하면
            // 해당 코드가 작동하는데 걸리는 시간을 이 곳에서 측정해준다.
            // 결과는 Xcode에서 확인 가능
        }
    }

}

 

TDD 흉내내보기

  • 테스트 메서드 작성
func testSuccessGetOne() {
    let expectationNumber = 1

    XCTAssertEqual(expectationNumber, numberGetter.getOne())
}

func testFailGetTwo() {
    let expectationNumber = 3

    XCTAssertEqual(expectationNumber, numberGetter.getFiv())
}
  • 테스트 하려는 클래스 작성
import Cocoa

class NumberGetter: NSObject {
    func getOne() -> Int {
        return 1
    }
    func getTwo() -> Int {
        return 2
    }
    func getThr() -> Int {
        return 3
    }
    func getFou() -> Int {
        return 4
    }
    func getFiv() -> Int {
        return 5
    }
    func getSix() -> Int {
        return 6
    }
}

 

테스트 실행


 

테스트 로그

2019-01-24 16:45:29.032626+0900 UnitTest[11804:6007802] [default] Unable to load Info.plist exceptions (eGPUOverrides)
2019-01-24 16:45:29.047874+0900 UnitTest[11804:6007637] ApplePersistenceIgnoreState: Existing state will not be touched. New state will be written to (null)
Test Suite 'All tests' started at 2019-01-24 16:45:29.405
Test Suite 'UnitTestTests.xctest' started at 2019-01-24 16:45:29.406
Test Suite 'UnitTestTests' started at 2019-01-24 16:45:29.406
Test Case '-[UnitTestTests.UnitTestTests testExample]' started.
작동 전
작동 후
Test Case '-[UnitTestTests.UnitTestTests testExample]' passed (0.215 seconds).
Test Case '-[UnitTestTests.UnitTestTests testFailGetTwo]' started.
작동 전
/Users/injungkim/Desktop/UnitTest/UnitTestTests/UnitTestTests.swift:40: error: -[UnitTestTests.UnitTestTests testFailGetTwo] : XCTAssertEqual failed: ("3") is not equal to ("5") - 
작동 후
Test Case '-[UnitTestTests.UnitTestTests testFailGetTwo]' failed (0.005 seconds).
Test Case '-[UnitTestTests.UnitTestTests testPerformanceExample]' started.
작동 전
2019-01-24 16:45:29.880129+0900 UnitTest[11804:6007863] Invalid connection: com.apple.coresymbolicationd
Invalid connection: com.apple.coresymbolicationd
/Users/injungkim/Desktop/UnitTest/UnitTestTests/UnitTestTests.swift:45: Test Case '-[UnitTestTests.UnitTestTests testPerformanceExample]' measured [Time, seconds] average: 0.000, relative standard deviation: 146.950%, values: [0.000003, 0.000001, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000], performanceMetricID:com.apple.XCTPerformanceMetric_WallClockTime, baselineName: "Local Baseline", baselineAverage: 0.000, maxPercentRegression: 10.000%, maxPercentRelativeStandardDeviation: 10.000%, maxRegression: 0.100, maxStandardDeviation: 0.100
작동 후
Test Case '-[UnitTestTests.UnitTestTests testPerformanceExample]' passed (1.774 seconds).
Test Case '-[UnitTestTests.UnitTestTests testSuccessGetOne]' started.
작동 전
작동 후
Test Case '-[UnitTestTests.UnitTestTests testSuccessGetOne]' passed (0.000 seconds).
Test Suite 'UnitTestTests' failed at 2019-01-24 16:45:31.401.
	 Executed 4 tests, with 1 failure (0 unexpected) in 1.994 (1.995) seconds
Test Suite 'UnitTestTests.xctest' failed at 2019-01-24 16:45:31.401.
	 Executed 4 tests, with 1 failure (0 unexpected) in 1.994 (1.995) seconds
Test Suite 'All tests' failed at 2019-01-24 16:45:31.402.
	 Executed 4 tests, with 1 failure (0 unexpected) in 1.994 (1.996) seconds

 

테스트 결과 화면


 

Terminal 로 테스트 돌려보기

# xcodebuild -project [project_name] -scheme [scheme_name] test
$ cd ~/Desktop/UnitTest
$ xcodebuild -project UnitTest.xcodeproj -scheme UnitTest test

...
2019-01-28 15:19:15.287257+0900 UnitTest[28445:8693178] [default] Unable to load Info.plist exceptions (eGPUOverrides)
2019-01-28 15:19:15.318540+0900 UnitTest[28445:8693163] ApplePersistenceIgnoreState: Existing state will not be touched. New state will be written to (null)
Test Suite 'All tests' started at 2019-01-28 15:19:15.437
Test Suite 'UnitTestTests.xctest' started at 2019-01-28 15:19:15.438
Test Suite 'UnitTestTests' started at 2019-01-28 15:19:15.438
Test Case '-[UnitTestTests.UnitTestTests testExample]' started.
작동 전
작동 후
Test Case '-[UnitTestTests.UnitTestTests testExample]' passed (0.095 seconds).
Test Case '-[UnitTestTests.UnitTestTests testFailGetTwo]' started.
작동 전
/Users/injungkim/Desktop/UnitTest/UnitTestTests/UnitTestTests.swift:41: error: -[UnitTestTests.UnitTestTests testFailGetTwo] : XCTAssertEqual failed: ("3") is not equal to ("5") -
작동 후
Test Case '-[UnitTestTests.UnitTestTests testFailGetTwo]' failed (0.002 seconds).
Test Case '-[UnitTestTests.UnitTestTests testPerformanceExample]' started.
작동 전
2019-01-28 15:19:15.789143+0900 UnitTest[28445:8693178] Invalid connection: com.apple.coresymbolicationd
Invalid connection: com.apple.coresymbolicationd
/Users/injungkim/Desktop/UnitTest/UnitTestTests/UnitTestTests.swift:46: Test Case '-[UnitTestTests.UnitTestTests testPerformanceExample]' measured [Time, seconds] average: 0.000, relative standard deviation: 138.116%, values: [0.000002, 0.000001, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000], performanceMetricID:com.apple.XCTPerformanceMetric_WallClockTime, baselineName: "", baselineAverage: , maxPercentRegression: 10.000%, maxPercentRelativeStandardDeviation: 10.000%, maxRegression: 0.100, maxStandardDeviation: 0.100
작동 후
Test Case '-[UnitTestTests.UnitTestTests testPerformanceExample]' passed (1.483 seconds).
Test Case '-[UnitTestTests.UnitTestTests testSuccessGetOne]' started.
작동 전
작동 후
Test Case '-[UnitTestTests.UnitTestTests testSuccessGetOne]' passed (0.000 seconds).
Test Suite 'UnitTestTests' failed at 2019-01-28 15:19:17.019.
	 Executed 4 tests, with 1 failure (0 unexpected) in 1.580 (1.581) seconds
Test Suite 'UnitTestTests.xctest' failed at 2019-01-28 15:19:17.019.
	 Executed 4 tests, with 1 failure (0 unexpected) in 1.580 (1.582) seconds
Test Suite 'All tests' failed at 2019-01-28 15:19:17.020.
	 Executed 4 tests, with 1 failure (0 unexpected) in 1.580 (1.583) seconds


Test session results and logs:
	/Users/injungkim/Library/Developer/Xcode/DerivedData/UnitTest-gtxfesydlfioavfrhanjinblngfd/Logs/Test/Test-UnitTest-2019.01.28_15-19-14-+0900.xcresult

2019-01-28 15:19:17.283 xcodebuild[28440:8692993] [MT] IDETestOperationsObserverDebug: 2.274 elapsed -- Testing started completed.
2019-01-28 15:19:17.283 xcodebuild[28440:8692993] [MT] IDETestOperationsObserverDebug: 0.000 sec, +0.000 sec -- start
2019-01-28 15:19:17.283 xcodebuild[28440:8692993] [MT] IDETestOperationsObserverDebug: 2.274 sec, +2.274 sec -- end
Failing tests:
	UnitTestTests.testFailGetTwo()
** TEST FAILED **

 

기록

2019.01.23 첫 작성

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31