Skip to content

Commit

Permalink
Allow tests to be selected by name.
Browse files Browse the repository at this point in the history
Closes #7

R=kevmoo@google.com

Review URL: https://codereview.chromium.org//1041503002
  • Loading branch information
nex3 committed Mar 30, 2015
1 parent 46f1bee commit 64dfda5
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 2 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
## 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`.

Expand Down
40 changes: 40 additions & 0 deletions bin/unittest.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ void main(List<String> args) {
_parser.addFlag("help", abbr: "h", negatable: false,
help: "Shows this usage information.");
_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",
abbr: 'p',
help: 'The platform(s) on which to run the tests.',
Expand Down Expand Up @@ -72,6 +79,39 @@ void main(List<String> args) {
throw new LoadException(path, 'Does not exist.');
}));
}).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);
return reporter.run().then((success) {
exitCode = success ? 0 : 1;
Expand Down
100 changes: 99 additions & 1 deletion test/runner/runner_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ final _usage = """
Usage: pub run unittest:unittest [files or directories...]
-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.
[vm (default), chrome]
Expand Down Expand Up @@ -264,13 +268,107 @@ $_usage"""));
});
});

group("flags", () {
group("flags:", () {
test("with the --color flag, uses colors", () {
new File(p.join(_sandbox, "test.dart")).writeAsStringSync(_failure);
var result = _runUnittest(["--color", "test.dart"]);
// This is the color code for red.
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));
});
});
});
}

Expand Down

0 comments on commit 64dfda5

Please sign in to comment.