[关闭]
@TryLoveCatch 2019-09-12T02:58:44.000000Z 字数 833 阅读 903

Flutter之测试

flutter


区分开发和生产环境

  1. /// 当App运行在Release环境时,为true;
  2. /// 当App运行在Debug和Profile环境时,为false。
  3. static bool isRelease() {
  4. return bool.fromEnvironment('dart.vm.product');
  5. }

单元测试

添加依赖

在pubspec.yaml里面添加依赖

  1. dependencies:
  2. flutter:
  3. sdk: flutter
  4. dev_dependencies:
  5. # 这里~~~~
  6. test:
  7. flutter_test:
  8. sdk: flutter

添加了test和flutter_test,这里最好也添加flutter_test,要不然后面执行的时候,会比较麻烦

创建测试文件

  1. counter_app/
  2. lib/
  3. counter.dart
  4. test/
  5. counter_test.dart

创建一个要测试的类

  1. class Counter {
  2. int value = 0;
  3. void increment() => value++;
  4. void decrement() => value--;
  5. }

为创建的类写一个测试

  1. import 'package:test/test.dart';
  2. import '../lib/counter.dart';
  3. void main() {
  4. test('Counter value should be incremented', () {
  5. final counter = Counter();
  6. counter.increment();
  7. print(counter.value);
  8. expect(counter.value, 1);
  9. });
  10. }

执行

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注