Reputation: 3942
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?
Upvotes: 17
Views: 3284
Reputation: 17000
Use the test Dart package https://pub.dartlang.org/packages/test
This package was previously named unittest which is now deprecated, but you might come across old articles about it. New tests should be written for the "test" package.
The tests can be run manually (like any other Dart program), or using by pub
(which can run individual tests or multiple tests found under a directory).
Upvotes: 4
Reputation: 10824
dartlang.org has a very long article about the current state of unit testing in dart. API documentation can be found here.
import 'package:unittest/unittest.dart';
void main() {
test('QuickSort', () =>
expect(QuickSort([5, 4, 3, 2, 1]),
orderedEquals([1, 2, 3, 4, 5]))
);
}
int _Partition(List array, int left, int right, int pivotIndex) {
var pivotValue = array[pivotIndex];
array[pivotIndex] = array[right];
array[right] = pivotValue;
var storeIndex = left;
for (var i = left; i < right; i++) {
if (array[i] < pivotValue) {
var tmp = array[i];
array[i] = array[storeIndex];
array[storeIndex] = tmp;
}
}
var tmp = array[storeIndex];
array[storeIndex] = array[right];
array[right] = tmp;
return storeIndex;
}
void _QuickSort(List array, int left, int right) {
if (left < right) {
int pivotIndex = left + ((right-left) / 2);
pivotIndex = _Partition(array, left, right, pivotIndex);
_QuickSort(array, left, pivotIndex-1);
_QuickSort(array, pivotIndex+1, right);
}
}
List QuickSort(List array) {
_QuickSort(array, 0, array.length-1);
return array;
}
Upvotes: 7
Reputation: 2165
There's an example of one approach, DartUnit, on YouTube: TDD for FizzBuzz in Dart
The code's on GitHub.
Upvotes: 1
Reputation: 14388
This post from "Adam Coding" (via G+) gives a very detailed description of the current state of dart unit testing, with nice screenshots of the In-Browser view of your unit tests.
Upvotes: 5
Reputation: 8128
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.
Upvotes: 13