使用Xcode 7 UI Testing进行每次测试后,如何重设应用程序数据? [英] How do I reset the application data after each test with Xcode 7 UI Testing?

查看:81
本文介绍了使用Xcode 7 UI Testing进行每次测试后,如何重设应用程序数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Apple在Xcode 7中引入了新的UI测试,但是每当测试启动该应用程序时,我都会感到挣扎,它始于该应用程序之前的数据.这意味着测试不能独立进行,而会受到其他测试的影响.

Apple introduced in Xcode 7 new UI Testing but I have a struggle whenever the tests launches the app, it starts with data that the application had before. It means tests cannot be independent and can be influenced by other tests.

无法访问用户默认值和其他数据,因为正在运行测试的应用程序无法访问已测试应用程序的捆绑软件.脚本也毫无疑问,因为它们可以在测试之前或之后运行.而且没有办法在每个测试套件之前在iOS上执行NSTask来运行脚本.

It is not possible to access user defaults and other data because the application that is running tests has no access to the bundle of the tested application. Scripts are also out of question because they can be run before or after testing. And there is no way how to execute NSTask on iOS to run a script before each test suite.

有没有办法在每个测试套件之前重置应用程序数据?

Is there a way how do reset the application data before each test suite?

推荐答案

不是直接的方法.但是有一些解决方法.

Not in a straight forward manner. But there are some workarounds.

XCUIApplication可以设置命令行参数和环境变量,以改变应用程序的行为.

The XCUIApplication can set command line arguments and environment variables that can alter your application’s behavior.

main.m文件的简单示例:

int main(int argc, char * argv[]) {
#if DEBUG
    // Reset all data for UI Testing
    @autoreleasepool {
        for (int i = 1; i < argc; ++i) {
            if (0 == strcmp("--reset-container", argv[i])) {
                NSArray *folders = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
                NSFileManager *fm = [[NSFileManager alloc] init];
                for (NSString *path in folders) {
                    [fm removeItemAtPath:path error:nil];
                }
                // Also remove documents folder if necessary...
            }
        }
    }
#endif
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil,
                                 NSStringFromClass([AppDelegate class]));
    }
}

然后在-[XCTestCase setUp]中添加:

XCUIApplication *app = [[XCUIApplication alloc] init];
app.launchArguments = @[@"--reset-container"];
[app launch];

这篇关于使用Xcode 7 UI Testing进行每次测试后,如何重设应用程序数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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