简体   繁体   English

如何查找我们是否在 Dart (Flutter) 中运行单元测试

[英]How to find if we are running unit test in Dart (Flutter)

While calling a function from unit test in Flutter (Dart), how can I find if I am running unit test or real application?在 Flutter (Dart) 中的单元测试中调用 function 时,我如何才能知道我是在运行单元测试还是实际应用程序? I want to pass different data if it's in unit test.如果它在单元测试中,我想传递不同的数据。

您可以使用以下内容来检查您是否正在运行测试。

Platform.environment.containsKey('FLUTTER_TEST')

The accepted answer is right but if you want to check for a testing environment without breaking your web code, you can use the universal_io package.公认的答案是正确的,但如果您想在不破坏 web 代码的情况下检查测试环境,则可以使用universal_io package。

Platform.environment.containsKey('FLUTTER_TEST')

On the web, Platform.environment will return an empty map instead of crashing your app.在 web 上,Platform.environment 将返回一个空的 map,而不是让您的应用程序崩溃。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM