美文网首页跨平台Flutter
Flutter了解之测试

Flutter了解之测试

作者: 平安喜乐698 | 来源:发表于2020-11-18 22:21 被阅读0次

3种

1. 单元测试UnitTest,测试某一个类或方法
2. 组件测试WidgetTest,测试组件
3. 集成测试IntegrationTest,测试某一功能
默认情况下:

1. pubspec.yaml文件有flutter_test:
dev_dependencies:
  flutter_test:
    sdk: flutter
2. 根目录下有一个test目录,目录下有一个widget_test.dart
删掉widget_test.dart

例(单元测试、组件测试)

main.dart文件:

import 'package:flutter/material.dart';
class Test{
  static hello(String name){
    return 'hello $name';
  }
}
void main() {
  runApp(MaterialApp(
    home: TestWidget(),
  ));
}
class TestWidget extends StatefulWidget{
  @override
  TestWidgetState createState() {
    return TestWidgetState();
  }
}
class TestWidgetState extends State<TestWidget>{
  int _count=0;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        child: Row(
          children: <Widget>[
            Text('hello'),
            ActionChip(
              key: Key('actionChipKey'),
              label: Text('$_count',key: Key('actionChipLabelKey'),),
              onPressed: (){
                setState(() {
                  _count++;
                });
              },
            )
          ],
        ),
      ),
    );
  }
}
新建hello_test.dart文件:

import 'package:flutter_test/flutter_test.dart';
import '../lib/main.dart';
//
void main(){
  // 单元测试
  test('单元测试描述', (){
    var str=Test.hello('world');
    // 添加断言,期望str的结果是hello world
    expect(str, 'hello world');
  });

  // 组件测试
  testWidgets('组件测试描述', (WidgetTester tester) async {
    await tester.pumpWidget(
      MaterialApp(
        home: TestWidget(),
      ),
    );

    // 获取内容为hello的文本组件
    final labelText=find.text('hello');
    // 添加断言,期望不存在这个组件
    expect(labelText, findsNothing);
    // 添加断言,期望找到一个
    expect(labelText, findsOneWidget);
    // 添加断言,期望找到n个,这里是2
    expect(labelText, findsNWidgets(2));

    final actionChipLabelText=find.text('0');
    expect(actionChipLabelText, findsOneWidget);
    //
    final actionChip=find.byType(ActionChip);
    await tester.tap(actionChip); // 点击
    await tester.pump();    // 重建

    final actionChipLabelTextAfterTap=find.text('1');
    expect(actionChipLabelTextAfterTap, findsOneWidget);
  });
}
终端根目录下执行

flutter test 运行所有测试文件
flutter test lib/hello_test.dart 运行指定测试文件

例(集成测试)

添加flutter_driver依赖:
dev_dependencies:
  test: #
  flutter_driver:
    sdk: flutter

根目录下创建test_driver目录
test_driver目录下创建app.dart、app_test.dart
app.dart文件内容如下:

import 'package:flutter_driver/driver_extension.dart';
import '../lib/main.dart' as app;
void main(){
  // 启用FlutterDriverExtension
  enableFlutterDriverExtension();
  // 启动应用
  app.main();
}
app_test.dart文件内容如下:

import 'package:flutter_driver/flutter_driver.dart';
import 'package:test/test.dart';
void main(){
  group('测试的描述', (){
    FlutterDriver driver;
    //
    final actionChip = find.byValueKey('actionChipKey');
    final actionChipLabelText = find.byValueKey('actionChipLabelKey');

    // 运行测试之前调用
    setUpAll(() async {
      driver=await FlutterDriver.connect();
    });
    // 测试完后调用
    tearDownAll(() async {
      // 关掉driver
      if(driver!=null){
        driver.close();
      }
    });
    
    test('测试描述 0', () async {
      expect(await driver.getText(actionChipLabelText), '0');
    });
    test('测试描述 1', () async {
      await driver.tap(actionChip);
      expect(await driver.getText(actionChipLabelText), '1');
    });
  },timeout: Timeout(Duration(minutes: 1)));
}
终端根目录下执行(会启动应用并安装到设备上)

flutter driver --target=test_driver/app.dart   
错误1:VMServiceFlutterDriver: tap message is taking a long time to complete...
解决:

相关文章

网友评论

    本文标题:Flutter了解之测试

    本文链接:https://www.haomeiwen.com/subject/rflwektx.html