iT邦幫忙

2023 iThome 鐵人賽

DAY 2
0
Mobile Development

在 iOS 專案上加上 Unit testing - 因為 You need testing系列 第 2

D2 - 在 iOS 專案加上測試-You need testing {開 Unit Testing Target 的方法}

  • 分享至 

  • xImage
  •  

https://ithelp.ithome.com.tw/upload/images/20230912/20140622Tin4GByD7J.png

開專案時加上 Tests Target

在開專案的時候要勾 Include Tests,這樣就會附上 Tests, UITests Targets。

https://ithelp.ithome.com.tw/upload/images/20230912/20140622FbvfnlaDQv.png

https://ithelp.ithome.com.tw/upload/images/20230912/20140622SE635zWS8K.png

如果是已經存在的專案,一開始並沒有勾 include tests,你還是可以在之後加上 test targets

在已經存在的專案,加上 Tests Target

step 1: 在專案下方的 Add Target 按 +

https://ithelp.ithome.com.tw/upload/images/20230912/20140622LCG2izXX3W.png

step 2: 選擇 Unit Testing Bundle

https://ithelp.ithome.com.tw/upload/images/20230912/20140622Ateqh7yws5.png

step 3: 在命名上,預設是你專案名後面加上 Tests,當然你也可以改成你想要的名字

https://ithelp.ithome.com.tw/upload/images/20230912/20140622DMWjfWmfjH.png

step 4: 開始運行測試,確定一切正常

在增加 Target 後,專案會出現預設的 test code

import XCTest

final class YouNeedTestingTests: 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.
        }
    }

}

試著選擇 Test Navigator 並按下 Test,確定 Unit Testing 能運作。這樣,就完成了在專案中加入 Unit Testing 步驟。

https://ithelp.ithome.com.tw/upload/images/20230912/20140622SQrSp5W3SL.png

You need testing, and this is how you start testing.


上一篇
D1 - 在 iOS 專案加上測試-You need testing {Why you need testing}
下一篇
D3 - 在 iOS 專案加上測試-You need testing {可測試的程式碼的通常長什麼樣子}
系列文
在 iOS 專案上加上 Unit testing - 因為 You need testing32
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言