✅ 规范
一个针对Dart和Flutter的精简测试框架。
它是什么?
Spec 在现有的Dart和Flutter测试工具之上构建,提供了一个精简而优雅的测试环境。Spec 同时提供了CLI工具
和Dart包。
CLI
Spec CLI允许您从CLI环境中运行spec命令并运行您的测试
- 直观的测试输出界面,灵感来自Jest。
- 交互式CLI(通过
spec --watch);当源代码更改时自动重新运行测试,并重新运行仅失败的测试。 - 使用一个命令并行运行Dart和Flutter测试。
- 使用Melos同时运行多个包中的测试。
包
spec包提供了一种不同的测试编写方式。Spec在设计时就考虑了类型安全和IDE自动补全,它改变了您
编写测试的方式,使其更具声明性,更少出错,并强制执行良好的习惯。
入门
匹配器
Spec CLI与正常的Dart test匹配器一起工作,但也提供了自定义匹配器,请参阅API参考文档。
使用Spec匹配器进行测试的示例:
import 'package:spec/spec.dart';
void main() {
test('future example', () async {
final future = Future.value(42);
expect(future).toEqual(future);
await expect(future).completion.toEqual(42);
await expect(future).throws.isArgumentError();
});
test('stream example', () async {
final stream = Stream.value(42);
await expect(stream).emits.toEqual(42);
await expect(stream).emits.isNull();
await expect(stream).emits.not.isNull();
await expect(stream).emitsError.isArgumentError();
});
test('function example', () {
void throwsFn() => throw Error();
expect(throwsFn).returnsNormally();
expect(throwsFn).throws.isArgumentError();
});
}
CLI
安装
dart pub global activate spec_cli
用法
运行测试:
spec
在交互模式下运行测试:
spec --watch
交互模式支持多种键盘快捷键
Watch Usage:
› Press f to run only failed tests.
› Press t to filter by a test name regex pattern.
› Press q to quit watch mode.
› Press Enter to trigger a test run.
由Invertase构建和维护。
