dart test
dart test
命令用于运行依赖于 test
包,并且位于当前 Dart 项目 test
目录下的测试。有关如何编写测试的信息,请参阅测试文档。如果你正在处理 Flutter 代码,则应改用 flutter test
命令,具体请参阅测试 Flutter 应用。
以下是使用 dart test
运行当前项目 test
目录下所有测试的示例:
cd my_app
dart test
要控制运行哪些测试,你可以添加 test
目录下的目录或文件路径:
dart test test/library_tour/io_test.dart
00:00 +0: readAsString, readAsLines
00:00 +1: readAsBytes
...
另一种运行测试子集的方法是使用 --name
(-n
)、--tags
(-t
) 或 --exclude-tags
(-x
) 标志,添加部分或全部字符串以匹配。
dart test --name String
00:00 +0: test/library_tour/io_test.dart: readAsString, readAsLines
00:00 +1: test/library_tour/core_test.dart: print: print(nonString)
00:00 +2: test/library_tour/core_test.dart: print: print(String)
00:00 +3: test/library_tour/core_test.dart: numbers: toString()
...
当你在同一命令行中多次使用这些标志时,仅运行匹配所有条件的测试。
dart test --name String --name print
00:00 +0: test/library_tour/core_test.dart: print: print(nonString)
00:00 +1: test/library_tour/core_test.dart: print: print(String)
00:00 +2: All tests passed!
dart test
命令有许多其他标志,可用于控制运行哪些测试、如何运行它们(例如,并发和超时),以及输出显示的位置和方式。有关命令行选项的更多信息,请参阅 test
包或使用 --help
标志。
dart test --help