iOS 單元測試

2021-06-29 00:51:36 字數 2252 閱讀 6277

進入測試tab,可以執行工程自帶的測試用例。

我們新增乙個自己的測試用例samplecalctests:

在生成的samplecalctest.m中新增標頭檔案引用和成員變數:

#import "ios_calcviewcontroller.h"

@inte***ce samplecalctests : xctestcase

@end

- (void)setup
新增testaddition方法,測試+操作的正確性:

- (void)testaddition

編譯執行測試用例,通過!

- (void)testaddition

為了測試能夠自動化,接下來我們為單元測試用例新增scheme:

這樣我們就可以通過命令列執行測試用例了:

xcodebuild test -project ios_calc.xcodeproj -scheme samplecalctests -destination 'platform=ios simulator,name=iphone 4s,os=8.1'
命令執行結果如下:

test suite 'all tests' started at 2015-03-09 13:03:31 +0000

test suite 'samplecalctests.xctest' started at 2015-03-09 13:03:31 +0000

executed 2 tests, with 0 failures (0 unexpected) in 0.305 (0.306) seconds

test suite 'samplecalctests' started at 2015-03-09 13:03:32 +0000

test case '-[samplecalctests testaddition]' started.

test case '-[samplecalctests testaddition]' passed (0.002 seconds).

test case '-[samplecalctests testexample]' started.

test case '-[samplecalctests testexample]' passed (0.001 seconds).

test case '-[samplecalctests testperformanceexample]' started.

test case '-[samplecalctests testperformanceexample]' passed (0.254 seconds).

test suite 'samplecalctests' passed at 2015-03-09 13:03:32 +0000.

executed 3 tests, with 0 failures (0 unexpected) in 0.258 (0.259) seconds

test suite 'samplecalctests.xctest' passed at 2015-03-09 13:03:32 +0000.

executed 5 tests, with 0 failures (0 unexpected) in 0.563 (0.566) seconds

test suite 'all tests' passed at 2015-03-09 13:03:32 +0000.

executed 5 tests, with 0 failures (0 unexpected) in 0.563 (0.567) seconds

** test succeeded **

iOS 單元測試

新增測試用例步驟及示例 xctest,它是xcode7中內建的測試框架,使用起來非常簡單 1.新建工程的時候新增測試單元 2.單元測試示例 咱們在這假設乙個使用者向主播送禮物場景,根據使用者送的禮物不同消耗的代金幣不同 user 使用者 present 禮物 anchor 主播 present類 p...

iOS 單元測試

import uikit 工具類 classlftools nsobjectelse import xctest testable import learnt 自己新建的測試類需要testable 引入否則無法訪問 class lftoolstest xctestcase 單元測試結束後呼叫 可以進...

iOS 單元測試

單元測試的好處是可以在其中隨意寫測試 而不會影響到主程式的功能 也許和個人所在公司和專案的原因,在實際開發中單元測試幾乎未用 例 新建乙個工程,在工程中新建乙個類為test。定義乙個計算兩個值和的類方法 test.h 計算兩數之和 nsinteger sumwithpartone nsinteger...