Xcode 4.2,无法运行单元测试

时间:2011-12-26 00:14:09

标签: objective-c xcode

我创建了一个新的Xcode 4.2 Cocoa项目,一个可可库。 默认情况下,单元测试带有以下单元测试:

- (void)testExample
{
    STFail(@"Unit tests are not implemented yet in learning01Tests");
}

如果我按⌘U,我可以看到测试版并按预期失败。 现在,我添加以下新行以测试我创建的外部类:

#import "svc01.h"

- (void)testMyClass
{
    svc01* svc = [[svc01 alloc]init];
    int expected = [svc addTwoNumbers:10 :10];
    STAssertTrue(21 == expected, @"It should fail!");
}

我可以看到两个测试(默认和我的方法)按预期失败。 问题在于,有时候,即使我按下命令Test,它会弹出消息“Build succe”然后“Test successful”,但是如果我看输出控制台就会显示“tests run 0”

我做错了吗?

更新

现在我已经更改了我的测试方法的签名,它不再起作用了。 我有这个:

#import <SenTestingKit/SenTestingKit.h>

@interface learning01Tests : SenTestCase
- (void)sumTwoIntegerShouldPass;
- (void)sumTwoZeroShouldThrow;

@end
- (void)sumTwoIntegerShouldPass
{
    svc01* svc = [[svc01 alloc]init];
    int expected = [svc addTwoNumbers:10 :10];
    STAssertTrue(20 == expected, @"It should fail!");
}

- (void)sumTwoZeroShouldThrow
{
    svc01* svc = [[svc01 alloc]init];
    int expected = [svc addTwoNumbers:0 :0];
    STAssertTrue(21 == expected, @"It should fail!");
}

这是Xcode输出:

-bfxnldmmxzsspnbglmrpwsnweqkd/Build/Products/Debug/learning01Tests.octest(Tests)' started at 2011-12-26 14:45:26 +0000
Test Suite 'learning01Tests' started at 2011-12-26 14:45:26 +0000
Test Suite 'learning01Tests' finished at 2011-12-26 14:45:26 +0000.
Executed 0 tests, with 0 failures (0 unexpected) in 0.000 (0.000) seconds
Test Suite '/Users/raffaeu/Library/Developer/Xcode/DerivedData/learning01-bfxnldmmxzsspnbglmrpwsnweqkd/Build/Products/Debug/learning01Tests.octest(Tests)' finished at 2011-12-26 14:45:26 +0000.
Executed 0 tests, with 0 failures (0 unexpected) in 0.000 (0.002) seconds

2 个答案:

答案 0 :(得分:8)

在Xcode 4.2中,您需要使用前缀 test 标记测试方法,以使其运行。我在这里找到了解决方案: http://www.blog.holsee.com/2011/07/tests-driven-groking-of-cocoa-objective-c/

这将有效:

- (void)testThatsumTwoZeroShouldThrow
{
    svc01* svc = [[svc01 alloc]init];
    int expected = [svc addTwoNumbers:0 :0];
    STAssertTrue(21 == expected, @"It should fail!");
}

虽然这不会:

- (void)TestThatsumTwoZeroShouldThrow
{
    svc01* svc = [[svc01 alloc]init];
    int expected = [svc addTwoNumbers:0 :0];
    STAssertTrue(21 == expected, @"It should fail!");
}

答案 1 :(得分:1)

您是否验证了目标的构建设置?

这是名为iOS Development Target的关键。如果它以粗体显示,则应按Delete键,使其显示为非粗体,然后,您的单元测试才能正常运行。

如果您想更改iOS开发目标,请执行以下操作:

  • 首先,打开项目设置/信息选项卡。然后选择您想要的iOS开发目标
  • 然后,打开目标构建设置。如果关键iOS开发目标以粗体显示,请按Delete键

就是这样。您的单元测试运行正确。