简体   繁体   中英

How to determine whether code is running during a unit test run

I need to have some kind of if statement to ignore certain bits of code eg pop dialog if the coding is being run as part of a unit test.

Does anyone have any idea have to do this - similar to debug?

I prefer runtime solution, not a one based on preprocessor:

int main(int argc, char* argv[]) {
    @autoreleasepool {
        BOOL tests = NO;

        for (int i = 0; i < argc; i++) {
            NSString* argument = [NSString stringWithCString:argv[i] encoding:NSASCIIStringEncoding];

            if ([argument isEqualToString:@"-SenTest"]) {
                tests = YES;
                break;
            }
        }

       if (tests) {
           //save YES to a global variable and use it whenewer you want
       }

       UIApplicationMain(...)

    }
}

I am actually using this to have a different UIApplicationDelegate when unit tests are run, so no UI code (DB opening, notifications started etc) collides with my test cases.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM