[iOS] 养成TDD好习惯(1):create first project with unit test
2013-04-19 16:39
441 查看
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 = [[Calculator
alloc] 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
* 生成的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 = [[Calculator
alloc] 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
相关文章推荐
- Create a simple unit test project with Visual Studio Unit Testing Framework
- create android project with github, what you should do first?
- To create my first app in iOS with Xcode(在Xcode创建我的第一个iOS app )
- TDD Best Practices: Unit testing in iOS with GHUnit (Part 1)
- [Python + Unit Testing] Write Your First Python Unit Test with pytest
- How to create unit test folder separated from src folder in eclipse project?
- ios xcode Could not load the "MyImage.png" image referenced from a nib in the bundle with identifier "com.mytest.MyProject"
- Create a unit test project
- How to create iOS 8 Today extension and share data with containing app – tutorial
- Unit Test Bundle' in SDK 'iOS 8.1 解决方法
- No mapping found for HTTP request with URI [/test/first.jsp] in DispatcherServlet
- Quick and easy Watir test suites with Test::Unit
- DotNet笔记--Unit Test With Fake
- iOS下的unit test——写在测试框架之后
- Your First Concordion.Net Project (Part 5)-Running Specs with Gallio
- Tutorial: Creating an HTML5 app from a native iOS* project with Intel® HTML5 App Porter Tool – BETA
- Code signing is required for product type Unit Test Bundle in SDK iOS 8.0
- CodeSign error: code signing is required for product type 'Unit Test Bundle' in SDK 'iOS 8.0'
- Create your first HTML-based AIR application with the AIR SDK
- Getting start with OCMock in you unit test