Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to write unit-tests in Dart?

Dart doesn't seem to have a dedicated unit-testing framework yet. What is the best approach to write and run unit-tests?

There are some very low-level examples in Dart source code (e.g. \tests\corelib\src\ListTest.dart), like this:

class ListTest {

  static testMain() {
    testList();
    testExpandableList();
  }

  static void testList() { ... }
  static void testExpandableList() { ... }
}

main() {
  ListTest.testMain();
}

Is this the recommended way to write tests in Dart, or is there any effort to make it easier with some unit-testing library, like x-unit?

Is there an easy way how to run all unit-tests written in this way and see results?

like image 637
dkl Avatar asked Dec 08 '11 09:12

dkl


1 Answers

Unit-testing in Dart is still very much under development. We (the Dart authors) currently use a python script (tools/test.py) to execute all our tests. The script runs through predefined directories, looks for files ending with 'Test', executes them, and compares them to the expected outcome.

Several days ago, a first version of test.dart (the equivalent in Dart) has been submitted. In the near future we will switch from tools/test.py to tools/test.dart to execute all our tests.

If you are writing a big project you could reuse our testing-framework. We are using it on a daily basis and it is pretty stable. For smaller projects the time spent on learning the framework might not be worth the effort. I would furthermore not be surprised if there are (or will be) other testing-frameworks.

The ListTest from your question has been written very early, when top-level functions were not yet available. It has since been modified (adding the main function) but we wouldn't write the test in this way anymore. Unless needed, we don't create classes in our test-cases. See, for example, here for a more recent test.

Edit: There is also a unit-test framework in client/testing/unittest/. See here for a test using this framework. This one also has the advantage that it runs in the browser.

like image 78
Florian Loitsch Avatar answered Nov 07 '22 20:11

Florian Loitsch