- 第一步的第一步的第一步:pubspec文件中添加依赖
1. 单元测试
dev_dependencies:
flutter_test:
sdk: flutter
- 测试全局方法
int sum(int a,int b){
return a + b;
}
int mul(int a,int b){
return a*b;
}
测试:
import 'package:cayj_cystudio/apptest/test_func.dart';
import 'package:flutter_test/flutter_test.dart';
void main(){
group("测试组", (){
test("测试test_func中的sum方法", (){
final result = sum(1, 9);
expect(result, 11);
});
test("测试test_func中的mul方法", (){
final result = mul(1, 9);
expect(result, 9);
});
});
}
不使用测试组也可以单独写:
// test("测试test_func中的sum方法", (){
// final result = sum(1, 9);
// expect(result, 50);
// });
// test("测试test_func中的mul方法", (){
// final result = mul(1, 9);
// expect(result, 9);
// });
- 测试全局类中的变量和方法
class Counter{
int value = 0;
int increment() => value++;
int decrement() => value--;
}
测试:
import 'package:cayj_cystudio/apptest/test_func.dart';
import 'package:flutter_test/flutter_test.dart';
void main(){
group("测试类", (){
Counter counter;
setUpAll((){
counter = Counter();
});
group("测试方法", (){
test("测试初始值", (){
expect(counter.value, 0);
});
test("value++", (){
counter.increment();
expect(counter.value, 1);
});
test("value--", (){
counter.decrement();
expect(counter.value, 0);
});
});
});
}
2. widget测试
widget:
class TestWidget extends StatelessWidget {
final List<String> _names;
TestWidget(this._names);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("test"),),
body: ListView(
children: _names.map((item){
return ListTile(
leading: Icon(Icons.stop),
title: Text(item),
);
}).toList(),
),
);
}
}
测试:
void main(){
testWidgets("测试widget", (WidgetTester tester) async{
// 注入widget
await tester.pumpWidget(MaterialApp(home: TestWidget(["cy","cy1"])));
// 在TestWidget中查找Text
final cyText = find.text("cy");
final cy1Text = find.text("cy1");
final icons = find.byIcon(Icons.stop);
expect(cyText, findsOneWidget);
expect(cy1Text, findsOneWidget);
expect(icons, findsNWidgets(2));
});
}
3. 集成测试
pudspec文件添加依赖
dev_dependencies:
flutter_test:
sdk: flutter
flutter_driver:
sdk: flutter
test: any
要测试的代码:
class TestTest extends StatefulWidget {
@override
_TestTestState createState() => _TestTestState();
}
class _TestTestState extends State<TestTest> {
var _counter = 2;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("集成测试"),
),
body: Column(
children: [
Text(_counter.toString(),key: ValueKey("numberKey")),
RaisedButton(
key: ValueKey("floatBtn"),
child: Text("+"),
onPressed: (){
setState(() {
_counter++;
});
},
)
],
),
);
}
}
test文件路径下编辑集成测试启动文件:
import 'package:cayj_cystudio/main.dart' as app;
import 'package:flutter_driver/driver_extension.dart';
void main(){
// 初始化Driver
enableFlutterDriverExtension();
// 运行程序
app.main();
}
撰写集成测试代码:(通过valuekey绑定)
import 'package:flutter_driver/flutter_driver.dart';
import 'package:test/test.dart';//要使用这个引入,否则会报错ui:dart
void main(){
group("集成测试", (){
FlutterDriver driver;
// 所有测试执行开始调用
setUpAll(() async {
driver = await FlutterDriver.connect();
});
// 获取text的值
final number = find.byValueKey("numberKey");
// 获取按钮
final floatBtn = find.byValueKey("floatBtn");
test("测试默认值", () async {
expect(await driver.getText(number), "2");
});
test("测试按钮点击后的值", () async {
await driver.tap(floatBtn);
expect(await driver.getText(number), "3");
});
// 所有测试执行结束调用
tearDownAll((){
driver.close();
});
});
}
注:
文件名要以XXX_test.dart(_test)方式结尾
terminal中运行集成测试代码
flutter drive --target=test_driver/app.dart
liutiesngdeMBP2:cayj_cystudio liutiesong$ flutter drive --target=test_driver/app.dart
Using device iPhone 12.
Starting application: test_driver/app.dart
Running Xcode build...
└─Compiling, linking and signing... 4.6s
Xcode build done. 16.7s
flutter: Observatory listening on http://127.0.0.1:56637/kaVeRZod69Q=/
00:00 +0: 集成测试 (setUpAll)
VMServiceFlutterDriver: Connecting to Flutter application at http://127.0.0.1:56637/kaVeRZod69Q=/
VMServiceFlutterDriver: Isolate found with number: 2082988028718143
VMServiceFlutterDriver: Isolate is paused at start.
VMServiceFlutterDriver: Attempting to resume isolate
VMServiceFlutterDriver: Waiting for service extension
flutter: 物理分辨率:1170.0 x 2532.0(宽 x 高)
flutter: 屏幕宽高:390.0 x 844.0(宽 x 高)
VMServiceFlutterDriver: Connected to Flutter application.
00:00 +0: 集成测试 测试默认值
00:00 +1: 集成测试 测试按钮点击后的值
00:01 +2: 集成测试 (tearDownAll)
00:01 +2: All tests passed!
Stopping application instance.
liutiesngdeMBP2:cayj_cystudio liutiesong$