检查应用程序是否在测试环境中运行 [英] Check if App is running in a Testing Environment

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

问题描述

只是想知道我是否可以确定我的应用程序当前是否在测试"环境中运行.原因是我正在运行自动屏幕截图,并且只想在运行该UI测试时隐藏/修改我的App的某些部分.

was just wondering if I can determine if my app currently runs in a Testing environment. Reason is that I am running automated screenshots and want to hide/modify parts of my App only when running that UI Test.

例如,我想跳过注册推送通知,以避免启动时出现该iOS弹出窗口.

For example I'd like to skip registering for push notifications to avoid that iOS Popup at launch.

我正在寻找类似的东西

if (kTestingMode) { ... } 

我知道我们确实有一个驱动程序,该驱动程序基本上可以启动应用程序,然后进行连接.猜猜该应用程序实际上甚至不知道它是否在测试模式下运行.但是也许有人知道答案.

I know that we do have a driver that basically launches the app and then connects. Guess the App actually does not even know if it is running in Testmode or not. But maybe someone knows an answer.

谢谢!

推荐答案

好吧,我自己找到了一个解决方案.我要做的是引入一个全局变量,该变量将在主驱动程序中设置.

Okay I just found a solution my myself. What I did is introduce a global variable which I will set in my main driver.

首先,我创建了一个新的 globals.dart 文件:

First I created a new globals.dart file:

library my_prj.globals;

bool testingActive = false;

然后,在我的 test_driver/main.dart 文件中导入该文件,并将 testingActive 变量设置为 true

Then, in my test_driver/main.dart file I import that and set the testingActive variable to true

import '../lib/globals.dart' as globals;

..

void main() {
  final DataHandler handler = (_) async {
    final response = {};
    return Future.value(c.jsonEncode(response));
  };
  // Enable integration testing with the Flutter Driver extension.
  // See https://flutter.io/testing/ for more info.
  enableFlutterDriverExtension(handler: handler);

  globals.testingActive = true;

  WidgetsApp.debugAllowBannerOverride = false; // remove debug banner
  runApp(App());
} 

现在,仅通过导入和检查,我的Flutter应用程序中到处都有这个全局变量.

Now, I do have this global variable everywhere in my Flutter App by simply importing and checking.

例如在我的 app.dart

import '../globals.dart' as globals;

...

if (globals.testingActive) {
   print("We are in a testing environment!");
}

有更好的解决方案吗?猜猜这很好用!

Is there a better solution? Guess this works just fine!

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

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