defineReflectiveTests function
Runs test methods existing in the given type
.
If there is a "solo" test method in the top-level suite, only "solo" methods are run.
If there is a "solo" test type, only its test methods are run.
Otherwise all tests methods of all test types are run.
Each method is run with a new instance of type
.
So, type
should have a default constructor.
If type
declares method setUp
, it methods will be invoked before any test
method invocation.
If type
declares method tearDown
, it will be invoked after any test
method invocation. If method returns Future to test some asynchronous
behavior, then tearDown
will be invoked in Future.complete
.
Implementation
void defineReflectiveTests(Type type) {
ClassMirror classMirror = reflectClass(type);
if (!classMirror.metadata.any((InstanceMirror annotation) =>
annotation.type.reflectedType == _ReflectiveTest)) {
String name = MirrorSystem.getName(classMirror.qualifiedName);
throw Exception('Class $name must have annotation "@reflectiveTest" '
'in order to be run by runReflectiveTests.');
}
_Group group;
{
bool isSolo = _hasAnnotationInstance(classMirror, soloTest);
String className = MirrorSystem.getName(classMirror.simpleName);
group = _Group(isSolo, _combineNames(_currentSuiteName, className));
_currentGroups.add(group);
}
classMirror.instanceMembers
.forEach((Symbol symbol, MethodMirror memberMirror) {
// we need only methods
if (!memberMirror.isRegularMethod) {
return;
}
// prepare information about the method
String memberName = MirrorSystem.getName(symbol);
bool isSolo = memberName.startsWith('solo_') ||
_hasAnnotationInstance(memberMirror, soloTest);
// test_
if (memberName.startsWith('test_')) {
if (_hasSkippedTestAnnotation(memberMirror)) {
group.addSkippedTest(memberName);
} else {
group.addTest(isSolo, memberName, memberMirror, () {
if (_hasFailingTestAnnotation(memberMirror) ||
_isCheckedMode && _hasAssertFailingTestAnnotation(memberMirror)) {
return _runFailingTest(classMirror, symbol);
} else {
return _runTest(classMirror, symbol);
}
});
}
return;
}
// solo_test_
if (memberName.startsWith('solo_test_')) {
group.addTest(true, memberName, memberMirror, () {
return _runTest(classMirror, symbol);
});
}
// fail_test_
if (memberName.startsWith('fail_')) {
group.addTest(isSolo, memberName, memberMirror, () {
return _runFailingTest(classMirror, symbol);
});
}
// solo_fail_test_
if (memberName.startsWith('solo_fail_')) {
group.addTest(true, memberName, memberMirror, () {
return _runFailingTest(classMirror, symbol);
});
}
// skip_test_
if (memberName.startsWith('skip_test_')) {
group.addSkippedTest(memberName);
}
});
// Support for the case of missing enclosing [defineReflectiveSuite].
_addTestsIfTopLevelSuite();
}