[iOS] 养成TDD好习惯(1):create first project with unit test
来源:互联网 发布:淘宝精仿 编辑:程序博客网 时间:2024/06/15 15:37
1. Create a projectwith ticking “include unit test” option inxcode.
* 生成的project会创建 2target, one for product app, one for unit test.
* For existingproject, to add unit test feature, just add a “unit testing bundle” type target
* By default, thereare 2 groups, one for product app, one for unit test. Unit test group里的.mfiles的property panel都是指向 unit test target。 而在product app group里的.mfiles都是指向product app target。不过2个group里的.hfiles都不指向任何target。有文章说对要被unit test的需要同时指向2个target,但我试了一下,好像不需要。
2. 先说requirement,要创建一个class叫Calculator, 会包含一个加法的method。
3. TDD,先写unittest:
* right click the unit testgroup, select “New File > Cocoa touch > objective c test case class”,click “next”
* set “Class” as “CalculatorTests”,click “Next”
* make sure unit test target is ticked, group is unit test, andfile path in unit test folder
* 代码如下
//CalculatorTests.h
#import<SenTestingKit/SenTestingKit.h>
@class Calculator;
@interface CalculatorTests : SenTestCase{
Calculator* calculator;
}
@end
//CalculatorTests.m
#import"CalculatorTests.h"
#import"Calculator.h"
@implementation CalculatorTests
-(void) setUp{
calculator = [[Calculatoralloc] init];
}
-(void) tearDown{
calculator = nil;
}
@end
4. 上面代码编译错误,因为没有Calculatorclass存在。现在创建一个classnamed “Calculator”,记住target指向product app target。
5. 继续TDD, 添加下面测试加法method的代码 in CalculatorTest.m
-(void) testAdd {
STAssertEquals([calculator add: 2to:3], 5,@"2 + 3 result should be5");
}
6. 编译错误,因为Calculator里没有add:to: method. 添加下列代码
-(int) add: (int)a to: (int)b{
return a + b;
}
7. 这时运行”Test”,如果failed, error list就会出现在左边的panel
- [iOS] 养成TDD好习惯(1):create first project with unit test
- Create a simple unit test project with Visual Studio Unit Testing Framework
- 如何写Unit Test, TDD 入门1
- 养成好的习惯
- 好习惯要养成
- 养成好习惯
- 养成好的习惯
- 养成sql好习惯
- 养成好的习惯
- 养成const好习惯
- 养成好习惯
- 如何才能养成好习惯
- 好习惯的养成
- 养成科研好习惯
- 养成好习惯!
- 养成好习惯
- 养成编码好习惯
- 养成好习惯
- ubuntu备份与恢复
- 自己感兴趣的网站
- java编程之计算3000绳子每天剪一半,绳子短于5米需要时间
- hdu 1398 Square Coins
- Android 滑动切换页面 以及屏幕手势
- [iOS] 养成TDD好习惯(1):create first project with unit test
- Qt 两个lineEdit 判断焦点问题
- Apache Shiro 集成-Ehcache
- 谈谈我处理异常的一般方法
- java编程之do…while计算1!+2!+…+100!的和
- NYOJ 16 矩形嵌套
- 程序在内存中的分布
- MySQL ODBC 配置
- memset与memcpy用法