Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(159)

Unified Diff: test/runner_test.dart

Issue 933083002: Add a test runner executable. (Closed) Base URL: git@github.com:dart-lang/unittest@master
Patch Set: Code review changes Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « test/loader_test.dart ('k') | test/utils.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: test/runner_test.dart
diff --git a/test/runner_test.dart b/test/runner_test.dart
new file mode 100644
index 0000000000000000000000000000000000000000..2b22667f2bab4fc19fe6f3237480b3fff211892e
--- /dev/null
+++ b/test/runner_test.dart
@@ -0,0 +1,203 @@
+// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+import 'dart:io';
+
+import 'package:path/path.dart' as p;
+import 'package:unittest/src/exit_codes.dart' as exit_codes;
+import 'package:unittest/unittest.dart';
+
+import 'io.dart';
+
+String _sandbox;
+
+final _success = """
+import 'dart:async';
+
+void main() {
+ var declarer = Zone.current[#unittest.declarer];
+ declarer.test("success", () {});
+}
+""";
+
+final _failure = """
+import 'dart:async';
+
+import 'package:unittest/unittest.dart';
+
+void main() {
+ var declarer = Zone.current[#unittest.declarer];
+ declarer.test("failure", () => throw new TestFailure("oh no"));
+}
+""";
+
+void main() {
+ setUp(() {
+ _sandbox = Directory.systemTemp.createTempSync('unittest_').path;
+ });
+
+ tearDown(() {
+ new Directory(_sandbox).deleteSync(recursive: true);
+ });
+
+ test("prints help information", () {
+ var result = _runUnittest(["--help"]);
+ expect(result.stdout, equals("""
+Runs tests in this package.
+
+Usage: pub run unittest:unittest [files or directories...]
+
+-h, --help Shows this usage information.
+"""));
+ expect(result.exitCode, equals(exit_codes.success));
+ });
+
+ group("fails gracefully if", () {
+ test("an invalid option is passed", () {
+ var result = _runUnittest(["--asdf"]);
+ expect(result.stderr, equals("""
+Could not find an option named "asdf".
+
+Usage: pub run unittest:unittest [files or directories...]
+
+-h, --help Shows this usage information.
+"""));
+ expect(result.exitCode, equals(exit_codes.usage));
+ });
+
+ test("a non-existent file is passed", () {
+ var result = _runUnittest(["file"]);
+ expect(result.stderr, equals('Failed to load "file": Does not exist.\n'));
+ expect(result.exitCode, equals(exit_codes.data));
+ });
+
+ test("the default directory doesn't exist", () {
+ var result = _runUnittest([]);
+ expect(result.stderr, equals(
+ 'Failed to load "test": No test files were passed and the default '
+ 'directory doesn\'t exist.\n'));
+ expect(result.exitCode, equals(exit_codes.data));
+ });
+
+ test("a test file fails to load", () {
+ var testPath = p.join(_sandbox, "test.dart");
+ new File(testPath).writeAsStringSync("invalid Dart file");
+ var result = _runUnittest(["test.dart"]);
+
+ expect(result.stderr, equals(
+ 'Failed to load "${p.relative(testPath, from: _sandbox)}":\n'
+ "line 1 pos 1: unexpected token 'invalid'\n"
+ "invalid Dart file\n"
+ "^\n"));
+ expect(result.exitCode, equals(exit_codes.data));
+ });
+
+ test("a test file throws", () {
+ var testPath = p.join(_sandbox, "test.dart");
+ new File(testPath).writeAsStringSync("void main() => throw 'oh no';");
+
+ var result = _runUnittest(["test.dart"]);
+ expect(result.stderr, startsWith(
+ 'Failed to load "${p.relative(testPath, from: _sandbox)}": oh no\n'));
+ expect(result.exitCode, equals(exit_codes.data));
+ });
+
+ test("a test file doesn't have a main defined", () {
+ var testPath = p.join(_sandbox, "test.dart");
+ new File(testPath).writeAsStringSync("void foo() {}");
+
+ var result = _runUnittest(["test.dart"]);
+ expect(result.stderr, startsWith(
+ 'Failed to load "${p.relative(testPath, from: _sandbox)}": No '
+ 'top-level main() function defined.\n'));
+ expect(result.exitCode, equals(exit_codes.data));
+ });
+
+ test("a test file has a non-function main", () {
+ var testPath = p.join(_sandbox, "test.dart");
+ new File(testPath).writeAsStringSync("int main;");
+
+ var result = _runUnittest(["test.dart"]);
+ expect(result.stderr, startsWith(
+ 'Failed to load "${p.relative(testPath, from: _sandbox)}": Top-level '
+ 'main getter is not a function.\n'));
+ expect(result.exitCode, equals(exit_codes.data));
+ });
+
+ test("a test file has a main with arguments", () {
+ var testPath = p.join(_sandbox, "test.dart");
+ new File(testPath).writeAsStringSync("void main(arg) {}");
+
+ var result = _runUnittest(["test.dart"]);
+ expect(result.stderr, startsWith(
+ 'Failed to load "${p.relative(testPath, from: _sandbox)}": Top-level '
+ 'main() function takes arguments.\n'));
+ expect(result.exitCode, equals(exit_codes.data));
+ });
+
+ // TODO(nweiz): test what happens when a test file is unreadable once issue
+ // 15078 is fixed.
+ });
+
+ group("runs successful tests", () {
+ test("defined in a single file", () {
+ new File(p.join(_sandbox, "test.dart")).writeAsStringSync(_success);
+ var result = _runUnittest(["test.dart"]);
+ expect(result.exitCode, equals(0));
+ });
+
+ test("defined in a directory", () {
+ for (var i = 0; i < 3; i++) {
+ new File(p.join(_sandbox, "${i}_test.dart"))
+ .writeAsStringSync(_success);
+ }
+
+ var result = _runUnittest(["."]);
+ expect(result.exitCode, equals(0));
+ });
+
+ test("defaulting to the test directory", () {
+ new Directory(p.join(_sandbox, "test")).createSync();
+ for (var i = 0; i < 3; i++) {
+ new File(p.join(_sandbox, "test", "${i}_test.dart"))
+ .writeAsStringSync(_success);
+ }
+
+ var result = _runUnittest([]);
+ expect(result.exitCode, equals(0));
+ });
+ });
+
+ group("runs failing tests", () {
+ test("defined in a single file", () {
+ new File(p.join(_sandbox, "test.dart")).writeAsStringSync(_failure);
+ var result = _runUnittest(["test.dart"]);
+ expect(result.exitCode, equals(1));
+ });
+
+ test("defined in a directory", () {
+ for (var i = 0; i < 3; i++) {
+ new File(p.join(_sandbox, "${i}_test.dart"))
+ .writeAsStringSync(_failure);
+ }
+
+ var result = _runUnittest(["."]);
+ expect(result.exitCode, equals(1));
+ });
+
+ test("defaulting to the test directory", () {
+ new Directory(p.join(_sandbox, "test")).createSync();
+ for (var i = 0; i < 3; i++) {
+ new File(p.join(_sandbox, "test", "${i}_test.dart"))
+ .writeAsStringSync(_failure);
+ }
+
+ var result = _runUnittest([]);
+ expect(result.exitCode, equals(1));
+ });
+ });
+}
+
+ProcessResult _runUnittest(List<String> args) =>
+ runUnittest(args, workingDirectory: _sandbox);
« no previous file with comments | « test/loader_test.dart ('k') | test/utils.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698