Skip to content

Commit

Permalink
Add solo test and group (#881)
Browse files Browse the repository at this point in the history
  • Loading branch information
grouma authored Jun 15, 2018
1 parent f9fc83b commit 38b5696
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 8 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 0.12.42

* Add support for `solo` test and group. When the argument is `true` only tests
and groups marked as solo will be run. It is still recommended that users
instead filter their tests by using the runner argument `-n`.

## 0.12.41

* Add support for debugging VM tests.
Expand Down
25 changes: 22 additions & 3 deletions lib/src/backend/declarer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ class Declarer {
/// Whether [build] has been called for this declarer.
bool _built = false;

/// The tests and/or groups that have been flagged as solo.
final _soloEntries = new Set<GroupEntry>();

/// Whether any tests and/or groups have been flagged as solo.
bool get _solo => _soloEntries.isNotEmpty;

/// The current zone-scoped declarer.
static Declarer get current => Zone.current[#test.declarer] as Declarer;

Expand Down Expand Up @@ -127,7 +133,8 @@ class Declarer {
skip,
Map<String, dynamic> onPlatform,
tags,
int retry}) {
int retry,
bool solo = false}) {
_checkNotBuilt("test");

var newMetadata = new Metadata.parse(
Expand Down Expand Up @@ -164,6 +171,10 @@ class Declarer {
// useful errors when calling `test()` and `group()` within a test.
zoneValues: {#test.declarer: this});
}, trace: _collectTraces ? new Trace.current(2) : null, guarded: false));

if (solo) {
_soloEntries.add(_entries.last);
}
}

/// Creates a group of tests.
Expand All @@ -173,7 +184,8 @@ class Declarer {
skip,
Map<String, dynamic> onPlatform,
tags,
int retry}) {
int retry,
bool solo = false}) {
_checkNotBuilt("group");

var newMetadata = new Metadata.parse(
Expand All @@ -197,6 +209,10 @@ class Declarer {
throw new ArgumentError("Groups may not be async.");
});
_entries.add(declarer.build());

if (solo || declarer._solo) {
_soloEntries.add(_entries.last);
}
}

/// Returns [name] prefixed with this declarer's group name.
Expand Down Expand Up @@ -240,7 +256,10 @@ class Declarer {
_checkNotBuilt("build");

_built = true;
return new Group(_name, _entries.toList(),
var entries = _entries.toList();
if (_solo) entries.removeWhere((entry) => !_soloEntries.contains(entry));

return new Group(_name, entries,
metadata: _metadata,
trace: _trace,
setUpAll: _setUpAll,
Expand Down
24 changes: 20 additions & 4 deletions lib/test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -132,21 +132,29 @@ Declarer get _declarer {
///
/// If multiple platforms match, the annotations apply in order as through
/// they were in nested groups.
///
/// If the `solo` flag is `true`, only tests and groups marked as
/// "solo" will be be run. This only restricts tests *within this test
/// suite*—tests in other suites will run as normal. We recommend that users
/// avoid this flag if possible and instead use the test runner flag `-n` to
/// filter tests by name.
@isTest
void test(description, body(),
{String testOn,
Timeout timeout,
skip,
tags,
Map<String, dynamic> onPlatform,
int retry}) {
int retry,
@deprecated bool solo = false}) {
_declarer.test(description.toString(), body,
testOn: testOn,
timeout: timeout,
skip: skip,
onPlatform: onPlatform,
tags: tags,
retry: retry);
retry: retry,
solo: solo);

// Force dart2js not to inline this function. We need it to be separate from
// `main()` in JS stack traces in order to properly determine the line and
Expand Down Expand Up @@ -202,21 +210,29 @@ void test(description, body(),
///
/// If multiple platforms match, the annotations apply in order as through
/// they were in nested groups.
///
/// If the `solo` flag is `true`, only tests and groups marked as
/// "solo" will be be run. This only restricts tests *within this test
/// suite*—tests in other suites will run as normal. We recommend that users
/// avoid this flag if possible, and instead use the test runner flag `-n` to
/// filter tests by name.
@isTestGroup
void group(description, body(),
{String testOn,
Timeout timeout,
skip,
tags,
Map<String, dynamic> onPlatform,
int retry}) {
int retry,
@deprecated bool solo = false}) {
_declarer.group(description.toString(), body,
testOn: testOn,
timeout: timeout,
skip: skip,
tags: tags,
onPlatform: onPlatform,
retry: retry);
retry: retry,
solo: solo);

// Force dart2js not to inline this function. We need it to be separate from
// `main()` in JS stack traces in order to properly determine the line and
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: test
version: 0.12.41
version: 0.12.42-dev
author: Dart Team <[email protected]>
description: A library for writing dart unit tests.
homepage: https://github.com/dart-lang/test
Expand Down
64 changes: 64 additions & 0 deletions test/runner/solo_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright (c) 2018, 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.

@TestOn("vm")
import 'package:test_descriptor/test_descriptor.dart' as d;

import 'package:test/test.dart';

import '../io.dart';

void main() {
test("only runs the tests marked as solo", () async {
await d.file("test.dart", """
import 'dart:async';
import 'package:test/test.dart';
void main() {
test("passes", () {
expect(true, isTrue);
}, solo: true);
test("failed", () {
throw 'some error';
});
}
""").create();

var test = await runTest(["test.dart"]);
expect(test.stdout, emitsThrough(contains("+1: All tests passed!")));
await test.shouldExit(0);
});

test("only runs groups marked as solo", () async {
await d.file("test.dart", """
import 'dart:async';
import 'package:test/test.dart';
void main() {
group('solo', () {
test("first pass", () {
expect(true, isTrue);
});
test("second pass", () {
expect(true, isTrue);
});
}, solo: true);
group('no solo', () {
test("failure", () {
throw 'some error';
});
test("another failure", () {
throw 'some error';
});
});
}
""").create();

var test = await runTest(["test.dart"]);
expect(test.stdout, emitsThrough(contains("+2: All tests passed!")));
await test.shouldExit(0);
});
}

0 comments on commit 38b5696

Please sign in to comment.