当前位置:编程学习 > wap >>

iOS开发那些事--编写OCUnit测试方法-逻辑测试方法

应用测试和逻辑测试
 
添加OCUnit到工程时候,我们提到过,应用测试(Application Testing)和逻辑测试(Logic Testing)两个概念,它们并非是OCUnit中的概念,而是单元测试中概念。应用测试是对整个应用程序进行的测试,设计测试用例时候要考虑到运行环境等因素,例如在测试JavaEE时候需要考虑Web容器和EJB容器等环境问题。而逻辑测试则是轻量级的,只测试某个业务逻辑对象的方法或算法正确性。
 
编写OCUnit测试方法
 
每一个单元测试用例对应于测试类中的一个方法,因此测试类分为:逻辑测试类和应用测试类,在设计测试用例时候,逻辑测试和应用测试也是不同的。编写OCUnit测试方法也是要分逻辑测试和应用测试。下面我们还是通过计算个人所得税应用介绍,它们的编写过程,被测试类ViewController编写过程不再介绍。
 
1、逻辑测试方法
 
逻辑测试应该测试计算个人所得税的业务逻辑,即测试ViewController类中的calculate:方法
 
LogicTest.h的代码如下:
 
[cpp] 
#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];  
  
}  
  
//测试月应纳税额不超过1500元 用例1   
  
- (void)testCalculateLevel1  
  
{  
  
double dbRevenue = 5000;  
  
NSString *strRevenue = [NSString stringWithFormat:@"%f",dbRevenue];  
  
NSString* strTax =[self.viewController calculate:strRevenue];  
  
STAssertTrue([strTax doubleValue] == 45, @”期望值是:45 实际值是:%@”, strTax);  
  
}  
  
//测试月应纳税额超过1500元至4500元 用例2   
  
- (void)testCalculateLevel2  
  
{  
  
double dbRevenue = 8000;  
  
NSString *strRevenue = [NSString stringWithFormat:@"%f",dbRevenue];  
  
NSString* strTax =[self.viewController calculate:strRevenue];  
  
STAssertTrue([strTax doubleValue] == 345, @”期望值是:345 实际值是:%@”, strTax);  
  
}  
  
//测试月应纳税额超过4500元至9000元 用例3   
  
- (void)testCalculateLevel3  
  
{  
  
double dbRevenue = 12500;  
  
NSString *strRevenue = [NSString stringWithFormat:@"%f",dbRevenue];  
  
NSString* strTax =[self.viewController calculate:strRevenue];  
  
STAssertTrue([strTax doubleValue] == 1245, @”期望值是:1245 实际值是:%@”, strTax);  
  
}  
  
//测试月应纳税额超过9000元至35000元 用例4   
  
- (void)testCalculateLevel4  
  
{  
  
double dbRevenue = 38500;  
  
NSString *strRevenue = [NSString stringWithFormat:@"%f",dbRevenue];  
  
NSString* strTax =[self.viewController calculate:strRevenue];  
  
STAssertTrue([strTax doubleValue] == 7745, @”期望值是:7745 实际值是:%@”, strTax);  
  
}  
  
//测试月应纳税额超过35000元至55000元 用例5   
  
- (void)testCalculateLevel5  
  
{  
  
double dbRevenue = 58500;  
  
NSString *strRevenue = [NSString stringWithFormat:@"%f",dbRevenue];  
  
NSString* strTax =[self.viewController calculate:strRevenue];  
  
STAssertTrue([strTax doubleValue] == 13745, @”期望值是:13745 实际值是:%@”, strTax);  
  
}  
  
//测试月应纳税额超过55000元至80000元 用例6   
  
- (void)testCalculateLevel6  
  
{  
  
double dbRevenue = 83500;  
  
NSString *strRevenue = [NSString stringWithFormat:@"%f",dbRevenue];  
  
NSString* strTax =[self.viewController calculate:strRevenue];  
  
STAssertTrue([strTax doubleValue] == 22495, @”期望值是:22495 实际值是:%@”, strTax);  
  
}  
  
//测试月应纳税额超过80000元 用例7   
  
- (void)testCalculateLevel7  
  
{  
  
double dbRevenue = 103500;  
  
NSString *strRevenue = [NSString stringWithFormat:@"%f",dbRevenue];  
  
NSString* strTax =[self.viewController calculate:strRevenue];  
  
STAssertTrue([strTax doubleValue] == 31495, @”期望值是:31495 实际值是:%@”, strTax);  
  
}  
  
@end  
 
#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];
补充:移动开发 , IOS ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,