Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running all unit tests in Dart

I'm working on a Flutter project and trying to find the best way to structure my tests. 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. Each file in the test project seems to need to have a main method, which feels odd. 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. Running flutter test test/lib/providers doesn't work while flutter test test/lib/providers/userproviderShould.dart does. 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?

like image 756
stimms Avatar asked Jan 19 '20 17:01

stimms


People also ask

What is TDD in flutter?

Test-Driven Development cycle TDD is also called the Red-Green-Refactor process regarding its iterative process, which is consisted of 3 following steps: Write a test that fails (Red) Write a code to make a test pass (Green) Refactor your code to obtain high code quality (Refactor)

What is a test at Dart?

Unit Testing involves testing every individual unit of an application. It helps the developer to test small functionalities without running the entire complex application. The Dart external library named "test" provides a standard way of writing and running unit tests.

What are UnitTests in Dart testing?

Testing terminology varies, but these are the terms and concepts that you are likely to encounter when using Dart technologies: Unittests focus on verifying the smallest piece of testable software, such as a function, method, or class. Your test suites should have more unit tests than other kinds of tests.

How do I run a dart test on a terminal?

You can run tests on the command line using the dart testcommand (or, for Flutter apps, flutter test). Kinds of testing The Dart testing docs focus on three kinds of testing, out of the many kinds of testingthat you may be familiar with: unit, component, and end-to-end (a form of integration testing).

What are the best packages for testing in Dart?

Although your tests partly depend on the platform your code is intended for—Flutter, the web, or server-side, for example—the following packages are useful across Dart platforms: package:test Provides a standard way of writing tests in Dart. You can use the test package to: Write single tests, or groups of tests.

What is software testing in Dart?

Software testing, an important part of app development, helps verify that your app is working correctly before you release it. This Dart testing guide outlines several types of testing, and points you to where you can learn how to test your Flutter, web, and server-side apps and scripts.


Video Answer


2 Answers

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:

  • be inside the /test folder
  • its name must be suffixed by _test.dart

Anything that does not have this _test will not be executed by the command line.

like image 88
Rémi Rousselet Avatar answered Sep 29 '22 17:09

Rémi Rousselet


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. You can probably still do that but dart test is the new way to do it and is also easier to remember.
  • 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.
like image 22
Suragch Avatar answered Sep 29 '22 17:09

Suragch