defineAutoTests 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 defineAutoTests(Type type) {
ClassMirror classMirror = reflectClass(type);
if (!classMirror.metadata.any((InstanceMirror annotation) =>
annotation.type.reflectedType == AutoTests)) {
String name = MirrorSystem.getName(classMirror.qualifiedName);
throw Exception('Class $name must have annotation "@autoTests" '
'in order to be run by runAutoTests.');
}
_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);
// setUp
if (_hasAnnotationInstance(memberMirror, beforeEach)) {
group.addSetUp((instance) => _runSetUp(instance, symbol, memberMirror));
return;
}
// test
if (memberName.startsWith('test') || _hasTestAnnotation(memberMirror)) {
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,
memberMirror: memberMirror, setUp: group.setUp);
}
});
}
return;
}
// soloTest
if (memberName.startsWith('soloTest') ||
memberName.startsWith('solo_test_') ||
_hasAnnotationInstance(memberMirror, soloTest)) {
group.addTest(true, memberName, memberMirror, () {
return _runTest(classMirror, symbol, memberMirror: memberMirror);
});
}
// failTest
if (memberName.startsWith('fail') || memberName.startsWith('fail_')) {
group.addTest(isSolo, memberName, memberMirror, () {
return _runFailingTest(classMirror, symbol);
});
}
// soloFail
if (memberName.startsWith('soloFail') ||
memberName.startsWith('solo_fail_')) {
group.addTest(true, memberName, memberMirror, () {
return _runFailingTest(classMirror, symbol);
});
}
// skipTest
if (memberName.startsWith('skipTest') ||
memberName.startsWith('skip_test_')) {
group.addSkippedTest(memberName);
}
});
// Support for the case of missing enclosing [defineReflectiveSuite].
_addTestsIfTopLevelSuite();
}