ios – Xcode 7.3中的UITesting中的launchArguments无效
发布时间:2020-12-14 18:55:34 所属栏目:百科 来源:网络整理
导读:我一直在 Xcode 7.3中编写UI测试,最近想添加一个启动参数来在应用程序中启用一些测试代码.我最初尝试设置XCUIApplication().launchArguments就像几个人在各种帖子中所做的那样,但它们不起作用. 挖掘它似乎无法在UI测试中设置launchArguments和launchEnvironm
我一直在
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以避免运送它). (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |