微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ios – Xcode 7.3中的UITesting中的launchArguments无效

我一直在 Xcode 7.3中编写UI测试,最近想添加一个启动参数来在应用程序中启用一些测试代码.我最初尝试设置XCUIApplication().launchArguments就像几个人在各种帖子中所做的那样,但它们不起作用.

挖掘它似乎无法在UI测试中设置launchArguments和launchEnvironment,即使api文档说它们可以.

此外,当我尝试在UI测试方案中设置启动参数和环境变量时,它们也没有传递到应用程序,在单元测试或运行应用程序时,它们是.

这是我为证明这一点所做的快速测试的副本,所有这些测试都失败了.

import XCTest

class launchdebugUITests: XCTestCase {

    func testLaunchArgumentsSetting() {
        XCUIApplication().launchArguments = ["abc"]
        print("Arguments \(XCUIApplication().launchArguments)")
        XCTAssertTrue(XCUIApplication().launchArguments.contains("abc"))
    }

    func testLaunchArgumentsAppending() {
        XCUIApplication().launchArguments.append("abc")
        print("Arguments \(XCUIApplication().launchArguments)")
        XCTAssertTrue(XCUIApplication().launchArguments.contains("abc"))
    }

    func testLaunchEnvironmentSetting() {
        XCUIApplication().launchEnvironment = ["abc":"def"]
        print("Environment \(XCUIApplication().launchEnvironment)")
        XCTAssertEqual("def",XCUIApplication().launchEnvironment["abc"])
    }

    func testLaunchEnvironmentAppending() {
        XCUIApplication().launchEnvironment["abc"] = "def"
        print("Environment \(XCUIApplication().launchEnvironment)")
        XCTAssertEqual("def",XCUIApplication().launchEnvironment["abc"])
    }

}

有人遇到过这种情况么?你有工作吗?

解决方法

然后,您还需要启动应用程序并在应用程序中查看参数.我是这样做的……

func testFooBar() {
    // given
    app.launchArguments = ["shoulddobar","shouldDoFoo"]

    // when
    app.launch()

    // then
}

然后在你的应用程序

int main(int argc,char *argv[]) {
    NSArray *arguments = [[nsprocessInfo processInfo] arguments];

    if ([arguments containsObject:@"shoulddobar"]) {
       dobar();
    }

    if ([arguments containsObject:@"shouldDoFoo"]) {
       doFoo();
    }
    ...
}

您可能希望参数检查更适合您的使用(也可能包含在#ifdef DEBUG … #endif以避免运送它).

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐