简体   繁体   English

在 Dart 中运行所有单元测试

[英]Running all unit tests in Dart

I'm working on a Flutter project and trying to find the best way to structure my tests.我正在开发一个 Flutter 项目,并试图找到构建测试的最佳方法。 Typically I structure tests to mirror the directory structure of the main project which is being tested.通常,我构建测试以反映正在测试的主项目的目录结构。

lib
 |models
 |providers
   |userprovider.dart
test
 lib
   |models
   |providers
     |userproviderShould.dart  

However, I'm having trouble figuring out if this approach is less than optimal for the Dart code.但是,我无法确定这种方法对于 Dart 代码是否不是最优的。 Each file in the test project seems to need to have a main method, which feels odd.测试项目中的每个文件似乎都需要一个main方法,感觉很奇怪。 I'm also not clear on how to run the entire test suite.我也不清楚如何运行整个测试套件。 The Flutter test running ( flutter test ) doesn't seem to understand directories. Flutter 测试运行( flutter test )似乎不理解目录。 Running flutter test test/lib/providers doesn't work while flutter test test/lib/providers/userproviderShould.dart does.运行flutter test test/lib/providers不起作用,而flutter test test/lib/providers/userproviderShould.dart可以。 If it doesn't understand directories it certainly doesn't understand having to recurse into directories.如果它不理解目录,它肯定不理解必须递归到目录中。

Is there a way to solve this that doesn't involve either having to build a fragile entry point that manually includes all the rest of the tests or writing a shell script to go run each file individually?有没有办法解决这个问题,既不需要构建一个手动包含所有其余测试的脆弱入口点,也不需要编写一个 shell 脚本来单独运行每个文件?

If you want flutter test or pub run test to execute a file without manually passing its path as parameter to the command, then the file must:如果您希望flutter testpub run test执行文件而不手动将其路径作为参数传递给命令,则该文件必须:

  • be inside the /test folder/test文件夹内
  • its name must be suffixed by _test.dart它的名字必须以_test.dart为后缀

Anything that does not have this _test will not be executed by the command line.任何没有这个_test的东西都不会被命令行执行。

To run all the unit tests in a project, type one of the following commands in the terminal while in the root of your project:要在项目中运行所有单元测试,请在项目的根目录中的终端中键入以下命令之一:

Flutter project颤振项目

flutter test

Dart project飞镖项目

dart test

Notes笔记

  • Dart projects used to be pub run test . Dart 项目曾经是pub run test You can probably still do that but dart test is the new way to do it and is also easier to remember.你可能仍然可以这样做,但dart test是一种新的方法,而且更容易记住。
  • test package documentation测试包文档
  • You may need to run flutter pub get or dart pub get to retrieve the test package if you haven't previously.如果您以前没有,您可能需要运行flutter pub getdart pub get来检索测试包。

With Flutter 2.5 (and perhaps below, though I haven't checked), apparently, you can use any directory name as an argument and it'll run all **/*_test.dart files in it.显然,使用 Flutter 2.5(也许在下面,虽然我没有检查过),您可以使用任何目录名称作为参数,它将运行其中的所有**/*_test.dart文件。

So: flutter test lib does the trick!所以: flutter test lib可以解决问题! You can pass additional arguments to the command, as long as the directory name is the very last argument ( flutter test --no-pub --coverage lib ).只要目录名称是最后一个参数( flutter test --no-pub --coverage lib ),您就可以将其他参数传递给命令。

Docs: https://pub.dev/packages/test#running-tests文档: https ://pub.dev/packages/test#running-tests

For commands, other answers here show you can use flutter test and dart test ( from @Suragch here )对于命令,此处的其他答案显示您可以使用flutter testdart test (来自@Suragch这里

About your flutter test test/lib/providers not working, try using test/lib/providers/* instead, because that worked for me when I had a similar issue.关于您的flutter test test/lib/providers不起作用,请尝试改用test/lib/providers/* ,因为当我遇到类似问题时这对我有用。


If you're using VS Code, for a launch command (by pressing F5 ) you can go to the Run and debug tab, and at the top-right of that sidebar, click the gear/settings icon which opens your launch.json , where you can add this section:如果您使用的是 VS Code,对于启动命令(按F5 ),您可以转到Run and debug选项卡,然后在该侧边栏的右上角,单击打开您的launch.json的齿轮/设置图标,您可以在其中添加此部分:

{
  "name": "Dart: Run all tests",
  "type": "dart",
  "request": "launch",
  "program": "./test/"
},

To hotkey this launch command, just go back to the Run and debug tab and at the top, use the dropdown menu to select the "name" of it, which will set it to launch by default when you press F5要热键此启动命令,只需返回“ Run and debug选项卡并在顶部,使用下拉菜单选择它的“名称”,这将在您按F5时将其设置为默认启动

And you can add more sections with different names and test folders for targeted tests that you'd want to launch quickly, eg您可以添加更多具有不同名称和测试文件夹的部分,用于您想要快速启动的目标测试,例如

{
  "name": "Run user tests",
  "type": "dart",
  "request": "launch",
  "program": "./test/users/"
},

As others mentioned, the flutter test or dart test (for tests that don't need the flutter SDK) commands will run anything under your test/ dir by default whose filename ends in _test.dart .正如其他人所提到的, flutter testdart test (对于不需要 flutter SDK 的测试)命令将默认运行test/目录下文件名以_test.dart结尾的任何内容。 (See the test package and flutter docs .) (请参阅测试包和颤振文档。)

However, if you're expecting to see nice output per test on the command line, you won't since the default --reporter type is compact, showing only a single line updated dynamically.但是,如果您希望在命令行上看到每个测试的良好输出,那么您不会因为默认的--reporter类型是紧凑的,仅显示动态更新的一行。 Furthermore, since tests are run concurrently by default (see --concurrency=<jobs> flag), it's possible one test's message will stomp on another's even when using --reporter expanded .此外,由于默认情况下测试是同时运行的(请参阅--concurrency=<jobs>标志),即使使用--reporter expanded ,一个测试的消息也可能会踩到另一个测试的消息。 If you really want to see a friendly line per test, limit concurrency with:如果您真的想在每个测试中看到一条友好的线路,请使用以下命令限制并发:

flutter test -j 1 -r expanded

Other notes:其他注意事项:

  • There's a lot of options one can pass to flutter/dart test .有很多选项可以传递给flutter/dart test See eg flutter test --help参见例如flutter test --help
  • dart test was introduced as of Dart 2.10 - prior sdk versions must use pub run test从 Dart 2.10 开始引入dart test - 之前的 sdk 版本必须使用pub run test
  • flutter pub test is equivalent to pub run test which is now dart test as noted above. flutter pub test等同于pub run test ,它现在是如上所述的dart test flutter pub test --help states: flutter pub test --help状态:

This is similar to "flutter test", but instead of hosting the tests in the flutter environment it hosts the tests in a pure Dart environment.这类似于“颤振测试”,但不是在颤振环境中托管测试,而是在纯 Dart 环境中托管测试。 The main differences are that the "dart:ui" library is not available and that tests run faster.主要区别在于“dart:ui”库不可用并且测试运行得更快。 This is helpful for testing libraries that do not depend on any packages from the Flutter SDK.这对于测试不依赖于 Flutter SDK 中的任何包的库很有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM