如何检测iOS应用程序是否在UI测试模式下运行 [英] How to detect if iOS app is running in UI Testing mode

查看:219
本文介绍了如何检测iOS应用程序是否在UI测试模式下运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like my app to run special code (e.g. resetting its state) when running in UI Testing mode. I looked at environment variables that are set when the app is running from UI Testing and there aren't any obvious parameters to differentiate between the app running normally vs in UI Testing. Is there a way to find out?

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

Two workarounds that I'm not satisfied with are:


  1. 设置 XCUIApplication.launchEnvironment ,其中包含我稍后在应用中检查的变量。这不好,因为您必须在每个测试文件的 setUp 方法中设置它。我尝试从方案设置中设置环境变量,但在运行UI测试测试时不会传播到应用程序本身。

  2. 检查环境变量是否存在 __ XPC_DYLD_LIBRARY_PATH 。这似乎非常hacky,可能现在只能工作,因为我们如何设置目标构建设置。

  1. Set XCUIApplication.launchEnvironment with some variable that I later check in the app. This isn't good because you have to set it in the setUp method of each test file. I tried setting the environment variable from the scheme settings but that doesn't propagate to the app itself when running UI Testing tests.
  2. Check for the lack of existence of the environment variable __XPC_DYLD_LIBRARY_PATH. This seems very hacky and might only be working now because of a coincidence in how we have our target build settings set up.


推荐答案

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

I've been researching this myself and came across this question. I ended up going with @LironYahdav's first workaround:

在你的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测试无关。

@JoeMasilotti's solutions are useful for unit tests, because they share the same runtime as the app being tested, but are not relevant for UI tests.

这篇关于如何检测iOS应用程序是否在UI测试模式下运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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