如何检测iOS应用程序是否在UI测试模式下运行

问题描述 投票:36回答:7

我希望我的应用程序在UI测试模式下运行时运行特殊代码(例如重置其状态)。我查看了从UI测试运行应用程序时设置的环境变量,并且没有任何明显的参数来区分正常运行的应用程序与UI测试中的应用程序。有没有办法找出来?

我不满意的两个解决方法是:

  1. 设置XCUIApplication.launchEnvironment的一些变量,我稍后在应用程序中检查。这不好,因为你必须在每个测试文件的setUp方法中设置它。我尝试从方案设置中设置环境变量,但在运行UI测试测试时不会传播到应用程序本身。
  2. 检查环境变量__XPC_DYLD_LIBRARY_PATH是否存在。这似乎非常hacky,可能现在只能工作,因为我们如何设置目标构建设置是巧合。
ios xcode-ui-testing
7个回答
51
投票

我自己一直在研究这个问题并遇到了这个问题。我最终选择了@ LironYahdav的第一个解决方法:

在您的UI测试中:

- (void)setUp
{
    [super setUp];

    XCUIApplication *app = [[XCUIApplication alloc] init];
    app.launchEnvironment = @{@"isUITest": @YES};
    [app launch];
}

在您的应用中:

NSDictionary *environment = [[NSProcessInfo processInfo] environment];
if (environment[@"isUITest"]) {
    // Running in a UI test
}

@ JoeMasilotti的解决方案对于单元测试非常有用,因为它们与正在测试的应用程序共享相同的运行时,但与UI测试无关。


10
投票

我没有成功设置启动环境,但让它与启动参数一起工作。

在你的测试中,setUp()函数添加:

  let app = XCUIApplication()
  app.launchArguments = ["testMode"]
  app.launch()

在您的生产代码中添加一个检查,如:

let testMode =  NSProcessInfo.processInfo().arguments.contains("testMode")
if testMode {
  // Do stuff
}

使用XCode 7.1.1验证。


5
投票

您可以使用预处理器宏。我发现你有几个选择:

新目标

制作应用程序目标的副本,并将其用作要测试的目标。可以在代码中访问此目标副本中的任何预处理器宏。

缺点是您必须向复制目标添加新的类/资源,有时很容易忘记。

新的构建配置

复制Debug构建配置,将任何预处理器宏设置为此配置并将其用于测试(请参阅下面的屏幕截图)。

一个小问题:每当您想要记录UI测试会话时,您需要更改运行以使用新的测试配置。

添加重复配置:

Add a duplicate conf

用于测试:

Use it for your *Test*


3
投票

我刚添加了此扩展程序

 @available(iOS 9, *)
 extension XCUIApplication {

 func test(){
   launchEnvironment = ["TEST":"true"]
   launch()
  }
 }

所以我可以使用test()而不是launch()


1
投票

Swift 3基于之前的答案。

class YourApplicationUITests: XCTestCase {

    override func setUp() {
        super.setUp()

        // Put setup code here. This method is called before the invocation of each test method in the class.

        // In UI tests it is usually best to stop immediately when a failure occurs.
        continueAfterFailure = false
        // UI tests must launch the application that they test. Doing this in setup will make sure it happens for each test method.
        let app = XCUIApplication()
        app.launchArguments = ["testMode"]
        app.launch()

        // In UI tests it’s important to set the initial state - such as interface orientation - required for your tests before they run. The setUp method is a good place to do this.
    }

    override func tearDown() {
        // Put teardown code here. This method is called after the invocation of each test method in the class.
        super.tearDown()
    }

    func testExample() {
        // Use recording to get started writing UI tests.
        // Use XCTAssert and related functions to verify your tests produce the correct results.
    }

}


extension UIApplication {
    public static var isRunningTest: Bool {
        return ProcessInfo().arguments.contains("testMode")
    }
}

然后在代码中调用UIApplication.isRunningTest。


0
投票

在Swift 3中,您可以检查XCInjectBundleInto键,或以XC开头的内容。

let isInTestMode = ProcessInfo.processInfo.environment["XCInjectBundleInto"] != nil

这也适用于OS X.


0
投票

我的解决方案几乎与上面的that of Ciryon完全相同,只是对于我的macOS基于文档的应用程序,我必须在参数名称前加一个连字符:

let app = XCUIApplication()
app.launchArguments.append("-Testing")
app.launch() 

...否则,“测试”最终被解释为启动应用程序时要打开的文档的名称,因此我收到错误提示:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.