iOS 單元測試

2021-07-24 16:45:53 字數 3011 閱讀 3086

新增測試用例步驟及示例

xctest,它是xcode7中內建的測試框架,使用起來非常簡單

1.新建工程的時候新增測試單元

2.單元測試示例

咱們在這假設乙個使用者向主播送禮物場景,根據使用者送的禮物不同消耗的代金幣不同

user(使用者)->present(禮物)->anchor(主播)

present類

//  present.h

// unittestdemo

//// created by kong.com on 16/11/17.

//#import

typedef ns_enum(nsuinteger, presentenum) ;

@inte***ce

present : nsobject

@property (nonatomic ,assign)presentenum present;

@property (nonatomic ,assign)int goldcoins;//禮物需要的代金幣

@property (nonatomic ,copy) nsstring*presentname;

- (instancetype)initwithpresent:(presentenum)present;

@end

#import "present.h"

#define presentnamearr @[@"愛心",@"玫瑰花",@"跑車"]

@implementation

present

- (instancetype)initwithpresent:(presentenum)present

return

self;

}@end

anchor類

//  anchor.h

// unittestdemo//

// created by kong.com on 16/

11/17.//

#import foundation.h>

@inte***ce

anchor

:nsobject

@property (nonatomic ,copy) nsstring *anchorname;

- (instancetype)initwithname:(nsstring *)name;

@end

- (instancetype)initwithname:(nsstring *)name 

return

self;

}

user 類

//  user.h

// unittestdemo

//// created by kong.com on 16/11/17.

//#import

#import "present.h"

#import "anchor.h"

@inte***ce

user : nsobject

@property (nonatomic ,copy) nsstring *username;

- (instancetype)initwithname:(nsstring *)name;

- (void)sendpresent:(present *)present toanchor:(anchor *)anchor;

- (nsstring *)test;

@end

#import "user.h"

@inte***ce

user ()

@property (nonatomic ,strong)present *present;

@property (nonatomic ,strong)anchor*anchor;

@end

@implementation

user

- (instancetype)initwithname:(nsstring *)name

return

self;

}- (void)sendpresent:(present *)present toanchor:(anchor *)anchor

- (nsstring *)test

@end

在unittestdemotests.m中新增測試方法- (void)testsendpresent

- (void)testsendpresent
在這個測試用例中大白*送玫瑰花給kong***花費了1代金幣使用快捷鍵⌘u執行測試

發現測試報錯了檢查發現xctasserttrue([[user test] isequaltostring:@"大白*送玫瑰花給kong***花費了1金幣"],@"測試送禮物");文字裡面少寫了乙個代xctasserttrue([[user test] isequaltostring:@"大白*送玫瑰花給kong***花費了1代金幣"]加上乙個⌘u再次執行,測試通過。

在單元測試中,綠色表示測試通過,紅色表示測試失敗

這是乙個簡單的測試用例,希望能給你帶來一些幫助

iOS 單元測試

進入測試tab,可以執行工程自帶的測試用例。我們新增乙個自己的測試用例samplecalctests 在生成的samplecalctest.m中新增標頭檔案引用和成員變數 import ios calcviewcontroller.h inte ce samplecalctests xctestca...

iOS 單元測試

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

iOS 單元測試

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