集成测试

Catalogue   

integration_test

集成测试框架,可分析测试用例的性能指标

官方文档:https://flutter.cn/docs/cookbook/testing/integration/introduction

基本使用

1. 添加配置

1
2
3
4
5
6
7
8
9
dev_dependencies:
flutter_test:
sdk: flutter
flutter_lints: ^1.0.0
mockito:
integration_test:
sdk: flutter
flutter_driver:
sdk: flutter

2. 根目录下新建test_driver文件夹,编写integration_test.dart文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import 'dart:io';
import 'package:integration_test/integration_test_driver_extended.dart';

Future<void> main() => integrationDriver(
onScreenshot: (String screenshotPath, List<int> screenshotBytes) async {

final File image = File(screenshotPath);
final dir = image.parent;
print(image);

if(!await dir.exists()) await dir.create(recursive: true);
image.writeAsBytesSync(screenshotBytes);

return true;

}
);

3. 根目录新建integration_test文件夹,编写app_test.dart文件

1
2
3
4
5
6
7
8
9
void main() async {
// 确保Flutter框架已经初始化完成
IntegrationTestWidgetsFlutterBinding.ensureInitialized();

testWidgets('test app', (widgetTester) async {
expect(XDragonApp(), isA<StatefulWidget>());
});

}

4. 运行

1
2
3
4
5
6
# --flavor 指定develop、staging、release
# --driver 指定驱动文件
# --target 指定测试用例,可以不设置driver,默认driver名字为测试用例名字_test.dart
# 比如:target的名字为app_feature.dart,则driver名字为app_feature_test.dart

flutter driver --driver=test_driver/integration_test.dart --target=integration_test/app_test.dart --flavor development

统计覆盖率

1
2
3
4
5
6
7
8
9
10
11
12
# Generate `coverage/lcov.info` file
# 运行测试脚本
flutter test --coverage

# Generate HTML report# Note: on macOS you need to have lcov installed on your system (`brew install lcov`) to use this:

# 将生成文件转换成html文件
genhtml coverage/lcov.info -o coverage/html

# Open the report
open coverage/html/index.html

参考文档