生活随笔
收集整理的這篇文章主要介紹了
iOS开发那些事--编写OCUnit测试方法-逻辑测试方法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
應用測試和邏輯測試
添加OCUnit到工程時候,我們提到過,應用測試(Application Testing)和邏輯測試(Logic Testing)兩個概念,它們并非是OCUnit中的概念,而是單元測試中概念。應用測試是對整個應用程序進行的測試,設計測試用例時候要考慮到運行環 境等因素,例如在測試JavaEE時候需要考慮Web容器和EJB容器等環境問題。而邏輯測試則是輕量級的,只測試某個業務邏輯對象的方法或算法正確性。
編寫OCUnit測試方法
每一個單元測試用例對應于測試類中的一個方法,因此測試類分為:邏輯測試類和應用測試類,在設計測試用例時候,邏輯測試和應用測試也是不同的。編寫 OCUnit測試方法也是要分邏輯測試和應用測試。下面我們還是通過計算個人所得稅應用介紹,它們的編寫過程,被測試類ViewController編寫 過程不再介紹。
1、邏輯測試方法
邏輯測試應該測試計算個人所得稅的業務邏輯,即測試ViewController類中的calculate:方法
LogicTest.h的代碼如下:
?
#import?<SenTestingKit/SenTestingKit.h>??#import?“ViewController.h”??@interface?LogicTest?:?SenTestCase??@property?(nonatomic,strong)?ViewController?*viewController;??@end??在h文件中定義viewController屬性,注意定義為屬性參數設置為strong。LogicTest.m的代碼如下:??#import?“LogicTest.h”??@implementation?LogicTest??-?(void)setUp??{??[super?setUp];??self.viewController?=?[[ViewController?alloc]?init];??}??-?(void)tearDown??{??self.viewController?=?nil;??[super?tearDown];??}????-?(void)testCalculateLevel1??{??double?dbRevenue?=?5000;??NSString?*strRevenue?=?[NSString?stringWithFormat:@"%f",dbRevenue];??NSString*?strTax?=[self.viewController?calculate:strRevenue];??STAssertTrue([strTax?doubleValue]?==?45,?@”期望值是:45?實際值是:%@”,?strTax);??}????-?(void)testCalculateLevel2??{??double?dbRevenue?=?8000;??NSString?*strRevenue?=?[NSString?stringWithFormat:@"%f",dbRevenue];??NSString*?strTax?=[self.viewController?calculate:strRevenue];??STAssertTrue([strTax?doubleValue]?==?345,?@”期望值是:345?實際值是:%@”,?strTax);??}????-?(void)testCalculateLevel3??{??double?dbRevenue?=?12500;??NSString?*strRevenue?=?[NSString?stringWithFormat:@"%f",dbRevenue];??NSString*?strTax?=[self.viewController?calculate:strRevenue];??STAssertTrue([strTax?doubleValue]?==?1245,?@”期望值是:1245?實際值是:%@”,?strTax);??}????-?(void)testCalculateLevel4??{??double?dbRevenue?=?38500;??NSString?*strRevenue?=?[NSString?stringWithFormat:@"%f",dbRevenue];??NSString*?strTax?=[self.viewController?calculate:strRevenue];??STAssertTrue([strTax?doubleValue]?==?7745,?@”期望值是:7745?實際值是:%@”,?strTax);??}????-?(void)testCalculateLevel5??{??double?dbRevenue?=?58500;??NSString?*strRevenue?=?[NSString?stringWithFormat:@"%f",dbRevenue];??NSString*?strTax?=[self.viewController?calculate:strRevenue];??STAssertTrue([strTax?doubleValue]?==?13745,?@”期望值是:13745?實際值是:%@”,?strTax);??}????-?(void)testCalculateLevel6??{??double?dbRevenue?=?83500;??NSString?*strRevenue?=?[NSString?stringWithFormat:@"%f",dbRevenue];??NSString*?strTax?=[self.viewController?calculate:strRevenue];??STAssertTrue([strTax?doubleValue]?==?22495,?@”期望值是:22495?實際值是:%@”,?strTax);??}????-?(void)testCalculateLevel7??{??double?dbRevenue?=?103500;??NSString?*strRevenue?=?[NSString?stringWithFormat:@"%f",dbRevenue];??NSString*?strTax?=[self.viewController?calculate:strRevenue];??STAssertTrue([strTax?doubleValue]?==?31495,?@”期望值是:31495?實際值是:%@”,?strTax);??}??@end? 在setUp方法中初始化viewController,在tearDown方法中釋放viewController屬性。測試方法 testCalculateLevel1~ testCalculateLevel7是對應測試用例1~7,測試方法中STAssertTrue是OCUnit框架宏斷言,這些與斷言有關的宏。
OCUnit框架斷言宏
| 框架 | 說明 |
| STAssertEqualObjects | 當兩個對象不相等,或者是其中一個對象為nil時候斷言失敗; |
| STAssertEquals | 當參數1不等于參數2時候斷言失敗,用于C中基本數據測試; |
| STAssertNil | 當參數不是nil時候斷言失敗; |
| STAssertNotNil | 當參數是nil時候斷言失敗; |
| STAssertTrue | 當表達式為false時候斷言失敗; |
| STAssertFalse | 當表達式為ture時候斷言失敗; |
| STAssertThrows | 如果表達式沒有拋出異常,則斷言失敗; |
| STAssertNoThrow | 如果表達式拋出異常,則斷言失敗; |
總結
以上是生活随笔為你收集整理的iOS开发那些事--编写OCUnit测试方法-逻辑测试方法的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。