OCUnit测试用例配置指南
XCode附带了OCUnit测试框架。下面介绍下如何在项目中使用OCUnit框架以及如何配置构建方案来执行指定用例。
1、使用XCode新建一个项目,如下图:
在项目配置对话框中勾选[IncludeUnit Tests]复选框,如下图:
2、如何执行部分测试用例。
因为OCUnit框架默认将执行所有的测试用例,在开发过程中,随着项目中测试数量的增加,用于编译和运行测试的时间也会随之增加,如果只想看看刚写完的那部分代码是否能够通过测试通过,此时执行那些与当前功能无关且其运行结果也不大会改变的测试用例就是浪费时间,那这时要想办法只执行部分测试用例。可以通过编辑构建方案(buildscheme)来指定哪些测试需要执行,哪些需要跳过。
为了方便演示,先增加几个测试用例,在OCUnitDemoTests.m中添加如下代码,这样就有三个测试用例了:
- (void)testExample
{
// STFail(@"Unit tests arenot implemented yet in OCUnitDemoTests");
}
- (void)testExample1
{
// STFail(@"Unit tests are notimplemented yet in OCUnitDemoTests");
}
- (void)testExample2
{
// STFail(@"Unit tests are notimplemented yet in OCUnitDemoTests");
}
1)在[product]菜单选择[Scheme]—[EditScheme],如下图:
2)在弹出的对话框中可以指定测试用例,如下图,勾选上了的测试用例会被执行:
如果没有被勾选,用例就不会被执行了,而发布之前,我们总是希望测试用例全部被执行的,而开发过程中总希望至迅速执行刚写的用例。
3)为了避免频繁的在部分测试和全部测试之间切换而修改构建方案,最好的办法是将构建方案复制一份,在其中一份构建方案中将测试用例都启用,而另一份中只启用部分当前想要执行的。操作如下,选择下图中的[Duplicate]:
4)命名新的构建方案为[PartTest of OCUnitDemo](名称随意自定)。
5)新的构建方案[PartTest of OCUnitDemo]中只选取自己想要执行的用例:
OK,这样,每次都配置[PartTest of OCUnitDemo]来执行部分用例,日志如下:
Test Suite 'Multiple Selected Tests' started at 2013-06-16 02:29:23 +0000
Test Suite'/var/mobile/Applications/A7916827-1C15-4CE9-BD6E-7BBDC6D8D151/tmp/OCUnitDemoTests.octest(Tests)'started at 2013-06-16 02:29:23 +0000
Test Suite 'DemoTest' started at 2013-06-16 02:29:23 +0000
Test Case '-[DemoTest testExample2]' started.
Test Case '-[DemoTest testExample2]' passed (0.000 seconds).
Test Suite 'DemoTest' finished at 2013-06-16 02:29:23 +0000.
Executed 1 test, with 0 failures (0 unexpected) in 0.000 (0.001) seconds
Test Suite 'OCUnitDemoTests' started at 2013-06-16 02:29:23 +0000
Test Case '-[OCUnitDemoTests testExample2]' started.
Test Case '-[OCUnitDemoTests testExample2]' passed (0.000 seconds).
Test Suite 'OCUnitDemoTests' finished at 2013-06-16 02:29:23 +0000.
Executed 1 test, with 0 failures (0 unexpected) in 0.000 (0.000) seconds
Test Suite'/var/mobile/Applications/A7916827-1C15-4CE9-BD6E-7BBDC6D8D151/tmp/OCUnitDemoTests.octest(Tests)'finished at 2013-06-16 02:29:23 +0000.
Executed 2 tests, with 0 failures (0 unexpected) in 0.000 (0.002) seconds
Test Suite 'Multiple Selected Tests' finished at 2013-06-16 02:29:23+0000.
Executed 2 tests, with 0 failures (0 unexpected) in 0.000 (0.006) seconds
而发布前执行[OCUnitDemo]执行所有用例。日志如下:
Test Suite 'All tests' started at 2013-06-16 02:30:37 +0000
Test Suite'/var/mobile/Applications/A7916827-1C15-4CE9-BD6E-7BBDC6D8D151/tmp/OCUnitDemoTests.octest(Tests)'started at 2013-06-16 02:30:37 +0000
Test Suite 'DemoTest' started at 2013-06-16 02:30:37 +0000
Test Case '-[DemoTest testExample2]' started.
Test Case '-[DemoTest testExample2]' passed (0.000 seconds).
Test Suite 'DemoTest' finished at 2013-06-16 02:30:37 +0000.
Executed 1 test, with 0 failures (0 unexpected) in 0.000 (0.001) seconds
Test Suite 'OCUnitDemoTests' started at 2013-06-16 02:30:37 +0000
Test Case '-[OCUnitDemoTests testExample]' started.
Test Case '-[OCUnitDemoTests testExample]' passed (0.000 seconds).
Test Case '-[OCUnitDemoTests testExample1]' started.
Test Case '-[OCUnitDemoTests testExample1]' passed (0.000 seconds).
Test Case '-[OCUnitDemoTests testExample2]' started.
Test Case '-[OCUnitDemoTests testExample2]' passed (0.000 seconds).
Test Suite 'OCUnitDemoTests' finished at 2013-06-16 02:30:37 +0000.
Executed 3 tests, with 0 failures (0 unexpected) in 0.000 (0.001) seconds
Test Suite'/var/mobile/Applications/A7916827-1C15-4CE9-BD6E-7BBDC6D8D151/tmp/OCUnitDemoTests.octest(Tests)'finished at 2013-06-16 02:30:37 +0000.
Executed 4 tests, with 0 failures (0 unexpected) in 0.001 (0.002) seconds
Test Suite 'All tests' finished at 2013-06-16 02:30:37 +0000.
Executed 4 tests, with 0 failures (0 unexpected) in 0.001 (0.006) seconds