Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode 4.2, can't run unit test

I have created a new Xcode 4.2 Cocoa project, a cocoa library. By default, the unit tests come with the following unit test:

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

If I press ⌘U I can see the test building and failing as expected. Now, I add the new following lines in order to test an external class I have created:

#import "svc01.h"

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

I can see both tests (the default and my method) failing as expected. The issue is that sometimes, even if I press the command Test, it popups the message "Build succeed" and then "Test succeed" but if I look at the output console it says "tests run 0"

Is there anything wrong am I doing?

UPDATE

Right now I have changed the signature of my test methods and it does not work anymore. I have this:

#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!");
}

And this is the Xcode output:

-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
like image 613
Raffaeu Avatar asked Jan 17 '23 23:01

Raffaeu


2 Answers

In Xcode 4.2 you need to mark your test method with the prefix test in order to get them running. I found the solution here: http://www.blog.holsee.com/2011/07/tests-driven-groking-of-cocoa-objective-c/

This will work:

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

while this will not:

- (void)TestThatsumTwoZeroShouldThrow
{
    svc01* svc = [[svc01 alloc]init];
    int expected = [svc addTwoNumbers:0 :0];
    STAssertTrue(21 == expected, @"It should fail!");
}
like image 173
Raffaeu Avatar answered Jan 28 '23 22:01

Raffaeu


Do you verify Build Setting of your target?

It's the key named iOS Development Target. If it appears in bold, you should press the Delete key, so it appears in not bold, then, your unit test runs correctly.

If you want to change your iOS Development Target, do as follow:

  • First, open Project setting / Info tab. Then choose the iOS Development Target you want
  • Then, open your target build setting. If the key iOS Development Target appears in bold, press the Delete key

That's it. Your unit test run correctly.

like image 38
vietstone Avatar answered Jan 28 '23 22:01

vietstone