Reputation: 11
I have a written a set of unittest for one of my Dart libraries, However I would like to be able to filter those to allow only certain ones to run during my continuous builds. I noticed that the unittest api allows for this to done but i can not find any examples.
Upvotes: 1
Views: 90
Reputation: 14171
You can filter what tests are run by creating a custom configuration and using filterTests()
.
import "package:unittest/unittest.dart";
class FilterTests extends Configuration {
get autoStart => false; // this ensures that the tests won't just run automatically
}
void useFilteredTests() {
configure(new FilterTests()); // tell configure to use our custom configuration
ensureInitialized(); // needed to get the plumbing to work
}
Then, in main()
, you use useFilterTests()
and then call filterTests()
with string or a regexp for the tests you want to run.
void main() {
useFilteredTests();
// your tests go here
filterTests(some_string_or_regexp);
runTests();
}
Tests whose description matches the argument to filterTests()
will run; other tests will not. I wrote a blog post on using filterTests()
that you might find useful.
Another approach to filtering tests if to divide them into multiple libraries and then import()
the main()
function of only those libraries whose tests you want to run.
So, imagine one library containing some tests:
library foo_tests;
import "package:unittest/unittest.dart";
void main() {
// some tests for foo()
}
and another one containing other tests:
library bar_tests;
import "package:unittest/unittest.dart";
void main() {
// some tests for bar()
}
You can sew together a test-runner by importing main()
from each of these libraries. In, my_tests.dart
, you could do this to run all your tests:
import "package:unittest/unittest.dart";
import "foo_tests.dart" as foo_tests;
import "bar_tests.dart" as bar_tests;
void main() {
foo_tests.main();
bar_tests.main();
}
If you wanted to run only foo_tests
or only bar_tests
, you could just import one. This would effectively create a filter. Here is a simple working example of how these imports work
Upvotes: 2