【问题标题】:How to tell Detox is running tests?如何判断 Detox 正在运行测试?
【发布时间】:2018-05-21 01:02:33
【问题描述】:

我正在使用 Detox 在我的 React Native 项目中运行端到端测试。我还在使用predicter.js 来模拟我的API 请求,并且我正在努力寻找一种方法来了解应用程序当前是否处于“测试”模式。

我正在向下传递一个 env 变量(并使用 babel-transform-inline-environment-variables)来判断我是否应该模拟请求,但这会在我们的发布版本中破坏 shim.js

有什么方法可以告诉 Detox 启动了应用程序并在 JS 中运行测试?理想情况下,我正在寻找在测试时设置的某种变量或从命令行传递的东西(TESTING=true react-native start__TESTING__

【问题讨论】:

    标签: react-native detox


    【解决方案1】:

    尝试使用react-native-config。这里还有一篇关于 Managing Configuration in React Native 的好文章,带有 react-native-config。

    我还在这里animated-button-block-the-detox 给出了一个答案,其中包含如何使用 react-native-config 在测试期间禁用循环动画​​的工作示例。

    基本思想是为所有不同的构建环境(开发、生产、测试​​等)创建 .env 配置文件。它们保存您的配置变量,您可以从 Javascript、Objective-C/Swift 或Java 访问这些变量。

    然后,您可以在构建应用程序时指定要使用的 .env 配置文件:

    $ ENVFILE=.env.staging react-native run-ios # bash

    这是一个 package.json 文件的示例,其中 detox 使用 .env 配置文件来构建应用程序。

    "detox": {
      "specs": "e2e",
      "configurations": {
        "ios.sim.release": {
          "binaryPath": "ios/build/Build/Products/Release-iphonesimulator/example.app",
          "build": "ENVFILE=.env.production export RCT_NO_LAUNCH_PACKAGER=true && xcodebuild -project ios/example.xcodeproj -scheme example -configuration Release -sdk iphonesimulator -derivedDataPath ios/build",
          "type": "ios.simulator",
          "name": "iPhone 5s, iOS 10.3"
        },
        "ios.sim.test": {
          "binaryPath": "ios/build/Build/Products/Debug-iphonesimulator/example.app",
          "build": "ENVFILE=.env.testing xcodebuild -project ios/example.xcodeproj -scheme example -configuration Debug -sdk iphonesimulator -derivedDataPath ios/build -arch x86_64",
          "type": "ios.simulator",
          "name": "iPhone 5s, iOS 10.3"
        }
      }
    }
    

    【讨论】:

    • santomegonzalo 在下面删除的答案添加了我需要做的额外步骤:即使我修改了默认 ios.sim.debug 配置的构建命令,我仍然必须手动将配置传递给 detox:detox build --configuration ios.sim.debug
    【解决方案2】:

    我们正在利用 detox 在 iOS 命令行上使用 --args -detoxServer ... -detoxSessionId ... 和在 Android 的 InstrumentationRegistry 中设置的 { detoxServer: ..., detoxSessionId: ... } 调用二进制文件这一事实。

    我们目前将其暴露给 JS 的方式对于 StackOverflow 的回答来说有点多,但这里有一些示例代码,连同 react native 的文档应该可以让你到达那里 - 对于 Android:

    // This will throw ClassNotFoundException if not running under any test,
    // but it still might not be running under Detox
    Class<?> instrumentationRegistry = Class.forName("android.support.test.InstrumentationRegistry");
    Method getArguments = instrumentationRegistry.getMethod("getArguments");
    Bundle argumentsBundle = (Bundle) getArguments.invoke(null);
    
    // Say you're in your BaseJavaModule.getConstants() implementation:
    return Collections.<String, Object>singletonMap("isDetox", null != argumentsBundle.getString("detoxServer"));
    

    在 iOS 上,类似(没有 Objective-C 编译器 ATM):

    return @{@"isDetox": [[[NSProcessInfo processInfo] arguments] containsObject: @"-detoxServer"]}
    

    注意,detox 也可以通过以下方式添加您自己的参数:

    detox.init(config, { launchApp: false });
    device.launchApp({ newInstance: true, launchArgs: {
      myCustomArg: value,
      ...,
    } });
    

    如果能在某个时候将其完善为一个模块,那就太好了。

    【讨论】:

      【解决方案3】:

      了解环境的测试/生产代码是混乱的 IMO。
      我建议这样做的方式是创建不同的应用风格进行测试

      如果您使用 React Native,请查看 react-native-repackager 的说明。 或者,Detox docs 也有该部分。 如果您为 Android 编写 Java 代码,请使用 gradle build flavor 来创建用于测试的风味。

      您可以在我们的 E2E 套件here 中找到更多关于我们如何模拟的信息。

      【讨论】:

        猜你喜欢
        • 2020-01-03
        • 2020-10-23
        • 1970-01-01
        • 1970-01-01
        • 2019-05-17
        • 2018-10-24
        • 2018-12-22
        • 2019-06-24
        • 1970-01-01
        相关资源
        最近更新 更多