iOS UI测试在一个被隔绝的看法 [英] iOS UI Testing On an Isolated View

查看:95
本文介绍了iOS UI测试在一个被隔绝的看法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将UI测试合并到我的iOS项目中,但有一件事情继续让我感到高兴的是,您编写的所有测试似乎都必须从应用程序的开头开始,然后才能完成。例如,如果我想测试登录屏幕后面的视图,我的测试必须首先在登录屏幕上运行,输入用户名/密码,单击登录,然后转到我想要测试的视图。理想情况下,登录视图和下一个视图的测试将完全隔离。有没有办法做到这一点,还是我完全错过了UI测试背后的哲学?

I'm trying to incorporate UI tests in my iOS project, but one thing that continues to hold me up is the fact that it seems all of the tests you write must start from the beginning of the app and work their way through. For example, if I want to test a view that is behind a login screen, my tests must first run on the login screen, enter a username/password, click login, then go to the view I want to test. Ideally, the tests for the login view and the next one would be completely isolated. Is there a way to do this, or am I missing the philosophy behind UI tests completely?

推荐答案

绝对是!

您需要的是一个干净的应用程序环境,您可以在其中运行测试 - 一个空白的平板。

What you need is a clean application environment in which you can run your tests - a blank slate.

所有应用程序都有一个应用程序委托,它设置应用程序的初始状态,并在启动时提供根视图控制器。出于测试目的,您不希望发生这种情况 - 您需要能够独立测试,而不会发生所有这些事情。理想情况下,您希望能够拥有屏幕并且仅加载该屏幕,并且不会发生其他状态更改。

All applications have an application delegate which sets up the initial state of the application and provides a root view controller on launch. For the purposes of testing you don't want that to happen - you need to be able to test in isolation, without all of those things happening. Ideally you want to be able to have the screen undertest and only that screen loaded, and no other state changes happen.

为此,您可以创建一个仅用于测试的对象实现 UIApplicationDelegate 。您可以告诉应用程序以测试模式运行,并使用启动参数使用特定于测试的应用程序委托。

To do so you can create an object just for testing that implements UIApplicationDelegate. You can tell the application to run in "testing mode" and use the testing-specific application delegate using a launch argument.

Objective-C:
main。 m:

Objective-C: main.m:

int main(int argc, char * argv[]) {
NSString * const kUITestingLaunchArgument   = @"org.quellish.UITestingEnabled";

    @autoreleasepool {
        if ([[NSUserDefaults standardUserDefaults] valueForKey:kUITestingLaunchArgument] != nil){
            return UIApplicationMain(argc, argv, nil, NSStringFromClass([TestingApplicationDelegate class]));
        } else {
            return UIApplicationMain(argc, argv, nil, NSStringFromClass([ProductionApplicationDelegate class]));
        }
    }
}

Swift:
main.swift:

Swift: main.swift:

let kUITestingLaunchArgument = "org.quellish.UITestingEnabled"

if (NSUserDefaults.standardUserDefaults().valueForKey(kUITestingLaunchArgument) != nil){
    UIApplicationMain(Process.argc, Process.unsafeArgv, NSStringFromClass(UIApplication), NSStringFromClass(TestingApplicationDelegate))

} else {
    UIApplicationMain(Process.argc, Process.unsafeArgv, NSStringFromClass(UIApplication), NSStringFromClass(AppDelegate))
}

您必须从Swift类中删除任何 @UIApplicationMain 注释。

You will have to remove any @UIApplicationMain annotation from your Swift classes.

For应用程序测试确保在Xcode中设置方案的测试操作以提供启动参数:

For "application tests" be sure to set the "Test" action of the scheme in Xcode to provide the launch argument:

佛r UI测试你可以将启动参数设置为测试的一部分:

For UI tests you can set the launch arguments as part of the test:

Objective-C:

Objective-C:

XCUIApplication *app = [[XCUIApplication alloc] init];
[app setLaunchArguments:@[@"org.quellish.UITestingEnabled"] ];
[app launch];

Swift:

let app = XCUIApplication()
app.launchArguments = [ "org.quellish.UITestingEnabled" ]
app.launch()

这允许测试特定地使用应用程序委托进行测试。这使您获得了很多控制权 - 现在您可以使用空白板进行测试。测试应用程序委托可以加载特定的故事板或放置一个空的 UIViewController 。作为UI测试的一部分,您可以实例化测试中的视图控制器并将其设置为 keyWindow 的根视图控制器或以模态方式呈现它。一旦添加或呈现,您的测试就可以执行,并在完成删除或解除时。

This allows the tests to use an application delegate specificly for testing. This empowers you with a lot of control - you now have a blank slate to work with for testing. The testing application delegate can load a specific storyboard or put in place an empty UIViewController. As part of your UI tests you might instantiate the view controller under test and set it as the keyWindow's root view controller or present it modally. Once it has been added or presented your tests can execute, and when complete remove or dismiss it.

这篇关于iOS UI测试在一个被隔绝的看法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆