XCTest did not finish in Xcode 5
Asked Answered
A

3

10

I have one test class and one test case. When I run my unit test, Xcode tells me all of my tests passed, but I have a warning that my tests didn't finish.

Do I have to do something special with XCTest to let it know I'm done with a particular test case?

Update: It may be a timing issue/bug. I put an [NSThread sleepForTimeInterval:.1]; in the tearDown method and now it works every time. Xcode also finished the tests fine periodically on its own without the sleep.

Update 2: Looks like some odd Xcode bug, I added a third test that is just doing text formatting validation and the warning shows up again.

Test Failure Image

Alexandretta answered 24/9, 2013 at 20:17 Comment(4)
Can you show your code? Test methods are expected to run to completion without any special handling.Merilyn
Same error here, though I'm getting it w/o the tests ever starting :-/Dissogeny
I had the same problem, and your sleepForTimeInterval trick, while unfortunate, solved it for me as well. I assume this is a bug in XCTest or Xcode, so that seems like a reasonable workaround for now.Margaux
I have this issue in an Xcode 5-created project. Adding [NSThread sleepForTimeInterval:.1]; (ickkk) to the tearDown method of my test fixed it. Seems like an Xcode bug, though.Preadamite
D
13

I never got the warning you mention, but after converting to Xcode 5 XCTest (with refactoring menu and swapping the framework myself) my tests ran forever (for some value of forever...) but never actually began executing (setUp and test methods not called at all).

I changed my Test target's, Build Settings > Packaging > Wrapper Extension from "octest" to "xctest" and the problem was immediately resolved.

Wrapper Extension setting existing and new value

To answer your question, a test should finish as soon as its execution finishes and its tearDown method completes. A test with no code will pass successfully. Try setting a breakpoint or log message inside your test to verify it is actually being called and make sure your tearDown is not hanging for any reason.

Donettedoney answered 24/9, 2013 at 22:16 Comment(4)
I first let Xcode convert to XCTest for me, but when I had this problem, I created the test target manually and it has correct wrapper extension now but I still have the same issue.Alexandretta
Thanks - I have a standard xcconfig file for unit tests, and this was a vital step I'd missed in doing the conversion.Natashianatassia
Yeah, seems like an oversight that the auto-conversion doesn't change this too.Donettedoney
Worked! Thank you! This is a classic XCode'ish annoyance.Morbidezza
D
0

Your update alludes to sleeping in the tearDown thread solving it, if you are doing something like network or background processing a handy trick is to set a flag and make sure it gets cleared (and throw an error if not). Clear the flag in your response code.

The teardown and test below set a timer for two seconds (well, it waits for 250ms eight times).

- (void)tearDown {
    [super tearDown];
    while (isProcessing > 0) {
        LogTrace(@"Polling...");
        [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:.25]];
        isProcessing--;
        if(isProcessing == 0) {
            XCTFail(@"Network loop expired while awaiting response");
        }
    }
    LogTrace(@"Finished Polling");
}

-(void)testARandomRequest {
    isProcessing = 8;

    NSDictionary *parameters = @{@"code": @"54321"};
    [manager doAFNetworkPatch:@"restApi/1" andParameters:parameters andPredicate:^(NSURLResponse *response, NSError *error) {
        NSLog(@"Response received");
        isProcessing = 0;
    }];
}
Donettedoney answered 26/9, 2013 at 4:49 Comment(1)
My test is just creating UILocalNotification objects and making sure all of the dates are set correctly in them. No background or network processing is happening that I'm aware of. Great tip though!Alexandretta
T
0

I had same issue as you when tried to run on the iOS6 simulator. Make sure you've running your tests on the iOS7 simulator, not the iOS6. XCTest available only since iOS7.

Tomika answered 13/5, 2014 at 6:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.