XCode7 UITesting - how to add main target classes to be available from UITestCase (Undefined symbols error)?

落爺英雄遲暮 提交于 2019-12-12 05:16:30

问题


I'm doing UI testing with XCode7 and need a way to reset my app to some initial state. I have a method to do so in one of my .m files that are part of the main target.

How can I make classes from the main target to be available from within a UITestCase?

I tried regular import, but get a linker error:

#import <XCTest/XCTest.h>
#import "CertificateManager.h"

@interface UITests : XCTestCase

@end

-(void)testWelcomeScreen
{
//put the app into initial state
    [[CertificateManager sharedInstance] resetApplication];
//... proceed with test
}

Error:

Undefined symbols for architecture i386:
  "_OBJC_CLASS_$_CertificateManager", referenced from:
      objc-class-ref in UITests.o
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)

I have compared this to the Unit test target, but cant see differences beyond that the unit test target has "Allow testing host application APIs" checked, while UITest target does not have this option at all.


回答1:


UI Tests run in a separate process from your production app. This means that you cannot access any production classes from your test suite.

The only interaction UI Tests have, outside of touching the screen, is by setting launch arguments or modifying the launch environment.

You can use either of these to reset your app when it launches. For example, in your UI Testing tests:

@interface UITests : XCTestCase
@property (nonatomic) XCUIApplication *app;
@end

@implementation FooUITests

- (void)setUp {
    [super setUp];

    self.app = [[XCUIApplication alloc] init];
    self.app.launchArguments = @[ @"UI-TESTING" ];
    [self.app launch];
}

- (void)testExample {
    // ... //
}

@end

Then, when your app launches you can check for this argument. If it exists, call your app's "reset" method.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    if ([[[NSProcessInfo processInfo] arguments] containsObject:@"UI-TESTING"]) {
        [[CertificateManager sharedInstance] resetApplication];
    }

    return YES;
}


来源:https://stackoverflow.com/questions/34771278/xcode7-uitesting-how-to-add-main-target-classes-to-be-available-from-uitestcas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!