简体   繁体   中英

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? 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.

Platform.environment.containsKey('FLUTTER_TEST')

On the web, Platform.environment will return an empty map instead of crashing your app.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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