Skip to content
Snippets Groups Projects
Commit 64dfda5c authored by Natalie Weizenbaum's avatar Natalie Weizenbaum
Browse files

Allow tests to be selected by name.

Closes #7

R=kevmoo@google.com

Review URL: https://codereview.chromium.org//1041503002
parent 46f1bee9
No related branches found
No related tags found
No related merge requests found
## 0.12.0-beta.1 ### 0.12.0-beta.1
* Add a `--name` (shorthand `-n`) flag to the test runner for selecting which
test to run.
* Add a missing dependency on `string_scanner`. * Add a missing dependency on `string_scanner`.
......
...@@ -26,6 +26,13 @@ void main(List<String> args) { ...@@ -26,6 +26,13 @@ void main(List<String> args) {
_parser.addFlag("help", abbr: "h", negatable: false, _parser.addFlag("help", abbr: "h", negatable: false,
help: "Shows this usage information."); help: "Shows this usage information.");
_parser.addOption("package-root", hide: true); _parser.addOption("package-root", hide: true);
_parser.addOption("name",
abbr: 'n',
help: 'A substring of the name of the test to run.\n'
'Regular expression syntax is supported.');
_parser.addOption("plain-name",
abbr: 'N',
help: 'A plain-text substring of the name of the test to run.');
_parser.addOption("platform", _parser.addOption("platform",
abbr: 'p', abbr: 'p',
help: 'The platform(s) on which to run the tests.', help: 'The platform(s) on which to run the tests.',
...@@ -72,6 +79,39 @@ void main(List<String> args) { ...@@ -72,6 +79,39 @@ void main(List<String> args) {
throw new LoadException(path, 'Does not exist.'); throw new LoadException(path, 'Does not exist.');
})); }));
}).then((suites) { }).then((suites) {
suites = flatten(suites);
var pattern;
if (options["name"] != null) {
if (options["plain-name"] != null) {
_printUsage("--name and --plain-name may not both be passed.");
exitCode = exit_codes.data;
return null;
}
pattern = new RegExp(options["name"]);
} else if (options["plain-name"] != null) {
pattern = options["plain-name"];
}
if (pattern != null) {
suites = suites.map((suite) {
return suite.change(
tests: suite.tests.where((test) => test.name.contains(pattern)));
}).toList();
if (suites.every((suite) => suite.tests.isEmpty)) {
stderr.write('No tests match ');
if (pattern is RegExp) {
stderr.write('regular expression "${pattern.pattern}".');
} else {
stderr.writeln('"$pattern".');
}
exitCode = exit_codes.data;
return null;
}
}
var reporter = new CompactReporter(flatten(suites), color: color); var reporter = new CompactReporter(flatten(suites), color: color);
return reporter.run().then((success) { return reporter.run().then((success) {
exitCode = success ? 0 : 1; exitCode = success ? 0 : 1;
......
...@@ -38,6 +38,10 @@ final _usage = """ ...@@ -38,6 +38,10 @@ final _usage = """
Usage: pub run unittest:unittest [files or directories...] Usage: pub run unittest:unittest [files or directories...]
-h, --help Shows this usage information. -h, --help Shows this usage information.
-n, --name A substring of the name of the test to run.
Regular expression syntax is supported.
-N, --plain-name A plain-text substring of the name of the test to run.
-p, --platform The platform(s) on which to run the tests. -p, --platform The platform(s) on which to run the tests.
[vm (default), chrome] [vm (default), chrome]
...@@ -264,13 +268,107 @@ $_usage""")); ...@@ -264,13 +268,107 @@ $_usage"""));
}); });
}); });
group("flags", () { group("flags:", () {
test("with the --color flag, uses colors", () { test("with the --color flag, uses colors", () {
new File(p.join(_sandbox, "test.dart")).writeAsStringSync(_failure); new File(p.join(_sandbox, "test.dart")).writeAsStringSync(_failure);
var result = _runUnittest(["--color", "test.dart"]); var result = _runUnittest(["--color", "test.dart"]);
// This is the color code for red. // This is the color code for red.
expect(result.stdout, contains("\u001b[31m")); expect(result.stdout, contains("\u001b[31m"));
}); });
group("with the --name flag,", () {
test("selects tests with matching names", () {
new File(p.join(_sandbox, "test.dart")).writeAsStringSync("""
import 'dart:async';
import 'package:unittest/unittest.dart';
void main() {
test("selected 1", () {});
test("nope", () => throw new TestFailure("oh no"));
test("selected 2", () {});
}
""");
var result = _runUnittest(["--name", "selected", "test.dart"]);
expect(result.stdout, contains("+2: All tests passed!"));
expect(result.exitCode, equals(0));
});
test("supports RegExp syntax", () {
new File(p.join(_sandbox, "test.dart")).writeAsStringSync("""
import 'dart:async';
import 'package:unittest/unittest.dart';
void main() {
test("test 1", () {});
test("test 2", () => throw new TestFailure("oh no"));
test("test 3", () {});
}
""");
var result = _runUnittest(["--name", "test [13]", "test.dart"]);
expect(result.stdout, contains("+2: All tests passed!"));
expect(result.exitCode, equals(0));
});
test("produces an error when no tests match", () {
new File(p.join(_sandbox, "test.dart")).writeAsStringSync(_success);
var result = _runUnittest(["--name", "no match", "test.dart"]);
expect(result.stderr,
contains('No tests match regular expression "no match".'));
expect(result.exitCode, equals(exit_codes.data));
});
});
group("with the --plain-name flag,", () {
test("selects tests with matching names", () {
new File(p.join(_sandbox, "test.dart")).writeAsStringSync("""
import 'dart:async';
import 'package:unittest/unittest.dart';
void main() {
test("selected 1", () {});
test("nope", () => throw new TestFailure("oh no"));
test("selected 2", () {});
}
""");
var result = _runUnittest(["--plain-name", "selected", "test.dart"]);
expect(result.stdout, contains("+2: All tests passed!"));
expect(result.exitCode, equals(0));
});
test("doesn't support RegExp syntax", () {
new File(p.join(_sandbox, "test.dart")).writeAsStringSync("""
import 'dart:async';
import 'package:unittest/unittest.dart';
void main() {
test("test 1", () => throw new TestFailure("oh no"));
test("test 2", () => throw new TestFailure("oh no"));
test("test [12]", () {});
}
""");
var result = _runUnittest(["--plain-name", "test [12]", "test.dart"]);
expect(result.stdout, contains("+1: All tests passed!"));
expect(result.exitCode, equals(0));
});
test("produces an error when no tests match", () {
new File(p.join(_sandbox, "test.dart")).writeAsStringSync(_success);
var result = _runUnittest(["--plain-name", "no match", "test.dart"]);
expect(result.stderr,
contains('No tests match "no match".'));
expect(result.exitCode, equals(exit_codes.data));
});
});
}); });
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment