use of org.ballerinalang.testerina.core.entity.TestSuite in project ballerina by ballerina-lang.
the class BTestRunner method execute.
/**
* Run all tests.
*/
private void execute() {
Map<String, TestSuite> testSuites = TesterinaRegistry.getInstance().getTestSuites();
if (testSuites.isEmpty()) {
throw new BallerinaException("No test functions found in the provided ballerina files.");
}
AtomicBoolean shouldSkip = new AtomicBoolean();
testSuites.forEach((packageName, suite) -> {
outStream.println("---------------------------------------------------------------------------");
outStream.println("Running Tests of Package: " + packageName);
outStream.println("---------------------------------------------------------------------------");
TestAnnotationProcessor.injectMocks(suite);
tReport.addPackageReport(packageName);
if (suite.getInitFunction() != null) {
suite.getInitFunction().invoke();
}
suite.getBeforeSuiteFunctions().forEach(test -> {
String errorMsg;
try {
test.invoke();
} catch (BallerinaException e) {
shouldSkip.set(true);
errorMsg = String.format("Failed to execute before test suite function [%s] of test suite " + "package [%s]. Cause: %s", test.getName(), packageName, e.getMessage());
errStream.println(errorMsg);
}
});
suite.getTests().forEach(test -> {
if (!shouldSkip.get()) {
// run the beforeEach tests
suite.getBeforeEachFunctions().forEach(beforeEachTest -> {
String errorMsg;
try {
beforeEachTest.invoke();
} catch (BallerinaException e) {
shouldSkip.set(true);
errorMsg = String.format("Failed to execute before each test function [%s] for the " + "test [%s] of test suite package [%s]. Cause: %s", beforeEachTest.getName(), test.getTestFunction().getName(), packageName, e.getMessage());
errStream.println(errorMsg);
}
});
}
if (!shouldSkip.get()) {
// run before tests
String errorMsg;
try {
if (test.getBeforeTestFunctionObj() != null) {
test.getBeforeTestFunctionObj().invoke();
}
} catch (BallerinaException e) {
shouldSkip.set(true);
errorMsg = String.format("Failed to execute before test function" + " [%s] for the test " + "[%s] of test suite package [%s]. Cause: %s", test.getBeforeTestFunctionObj().getName(), test.getTestFunction().getName(), packageName, e.getMessage());
errStream.println(errorMsg);
}
}
// run the test
TesterinaResult functionResult = null;
try {
if (!shouldSkip.get()) {
BValue[] valueSets = null;
if (test.getDataProviderFunction() != null) {
valueSets = test.getDataProviderFunction().invoke();
}
if (valueSets == null) {
test.getTestFunction().invoke();
// report the test result
functionResult = new TesterinaResult(test.getTestFunction().getName(), true, shouldSkip.get(), null);
tReport.addFunctionResult(packageName, functionResult);
} else {
List<BValue[]> argList = extractArguments(valueSets);
argList.forEach(arg -> {
test.getTestFunction().invoke(arg);
TesterinaResult result = new TesterinaResult(test.getTestFunction().getName(), true, shouldSkip.get(), null);
tReport.addFunctionResult(packageName, result);
});
}
} else {
// report the test result
functionResult = new TesterinaResult(test.getTestFunction().getName(), false, shouldSkip.get(), null);
tReport.addFunctionResult(packageName, functionResult);
}
} catch (Exception e) {
String errorMsg = String.format("Failed to execute the test function [%s] of test suite package " + "[%s]. Cause: %s", test.getTestFunction().getName(), packageName, e.getMessage());
errStream.println(errorMsg);
// report the test result
functionResult = new TesterinaResult(test.getTestFunction().getName(), false, shouldSkip.get(), errorMsg);
tReport.addFunctionResult(packageName, functionResult);
return;
}
// run the after tests
String error;
try {
if (test.getAfterTestFunctionObj() != null) {
test.getAfterTestFunctionObj().invoke();
}
} catch (BallerinaException e) {
error = String.format("Failed to execute after test function" + " [%s] for the test [%s] of test " + "suite package [%s]. Cause: %s", test.getAfterTestFunctionObj().getName(), test.getTestFunction().getName(), packageName, e.getMessage());
errStream.println(error);
}
// run the afterEach tests
suite.getAfterEachFunctions().forEach(afterEachTest -> {
String errorMsg2;
try {
afterEachTest.invoke();
} catch (BallerinaException e) {
errorMsg2 = String.format("Failed to execute after each test function" + " [%s] for the test " + "[%s] of test suite package [%s]. Cause: %s", afterEachTest.getName(), test.getTestFunction().getName(), packageName, e.getMessage());
errStream.println(errorMsg2);
}
});
});
TestAnnotationProcessor.resetMocks(suite);
// Run After suite functions
suite.getAfterSuiteFunctions().forEach(func -> {
String errorMsg;
try {
func.invoke();
} catch (BallerinaException e) {
errorMsg = String.format("Failed to execute after test suite function [%s] of test suite " + "package [%s]. Cause: %s", func.getName(), packageName, e.getMessage());
errStream.println(errorMsg);
}
});
// print package test results
tReport.printTestSuiteSummary(packageName);
});
}
use of org.ballerinalang.testerina.core.entity.TestSuite in project ballerina by ballerina-lang.
the class TestAnnotationProcessor method resolveFunctions.
/**
* Resolve function names to {@link TesterinaFunction}s.
*
* @param suite {@link TestSuite} whose functions to be resolved.
*/
private static void resolveFunctions(TestSuite suite) {
List<TesterinaFunction> functions = suite.getTestUtilityFunctions();
List<String> functionNames = functions.stream().map(testerinaFunction -> testerinaFunction.getName()).collect(Collectors.toList());
for (Test test : suite.getTests()) {
if (test.getTestName() != null && functionNames.contains(test.getTestName())) {
test.setTestFunction(functions.stream().filter(e -> e.getName().equals(test.getTestName())).findFirst().get());
}
if (test.getBeforeTestFunction() != null && functionNames.contains(test.getBeforeTestFunction())) {
test.setBeforeTestFunctionObj(functions.stream().filter(e -> e.getName().equals(test.getBeforeTestFunction())).findFirst().get());
}
if (test.getAfterTestFunction() != null && functionNames.contains(test.getAfterTestFunction())) {
test.setAfterTestFunctionObj(functions.stream().filter(e -> e.getName().equals(test.getAfterTestFunction())).findFirst().get());
}
if (test.getDataProvider() != null && functionNames.contains(test.getDataProvider())) {
String dataProvider = test.getDataProvider();
test.setDataProviderFunction(functions.stream().filter(e -> e.getName().equals(test.getDataProvider())).findFirst().map(func -> {
// TODO these validations are not working properly with the latest refactoring
if (func.getbFunction().getRetParamTypes().length == 1) {
BType bType = func.getbFunction().getRetParamTypes()[0];
if (bType.getTag() == TypeTags.ARRAY_TAG) {
BArrayType bArrayType = (BArrayType) bType;
if (bArrayType.getElementType().getTag() != TypeTags.ARRAY_TAG) {
String message = String.format("Data provider function [%s] should return an array of" + " arrays.", dataProvider);
throw new BallerinaException(message);
}
} else {
String message = String.format("Data provider function [%s] should return an array of " + "arrays.", dataProvider);
throw new BallerinaException(message);
}
} else {
String message = String.format("Data provider function [%s] should have only one return type" + ".", dataProvider);
throw new BallerinaException(message);
}
return func;
}).get());
if (test.getDataProviderFunction() == null) {
String message = String.format("Data provider function [%s] cannot be found.", dataProvider);
throw new BallerinaException(message);
}
}
for (String dependsOnFn : test.getDependsOnTestFunctions()) {
// TODO handle missing func case
test.addDependsOnTestFunction(functions.stream().filter(e -> e.getName().equals(dependsOnFn)).findFirst().get());
}
}
// resolve mock functions
suite.getMockFunctionNamesMap().forEach((id, functionName) -> {
TesterinaFunction function = suite.getTestUtilityFunctions().stream().filter(e -> e.getName().equals(functionName)).findFirst().get();
suite.addMockFunctionObj(id, function);
});
suite.getBeforeSuiteFunctionNames().forEach(functionName -> {
TesterinaFunction function = suite.getTestUtilityFunctions().stream().filter(e -> e.getName().equals(functionName)).findFirst().get();
suite.addBeforeSuiteFunctionObj(function);
});
suite.getAfterSuiteFunctionNames().forEach(functionName -> {
TesterinaFunction function = suite.getTestUtilityFunctions().stream().filter(e -> e.getName().equals(functionName)).findFirst().get();
suite.addAfterSuiteFunctionObj(function);
});
suite.getBeforeEachFunctionNames().forEach(functionName -> {
TesterinaFunction function = suite.getTestUtilityFunctions().stream().filter(e -> e.getName().equals(functionName)).findFirst().get();
suite.addBeforeEachFunctionObj(function);
});
suite.getAfterEachFunctionNames().forEach(functionName -> {
TesterinaFunction function = suite.getTestUtilityFunctions().stream().filter(e -> e.getName().equals(functionName)).findFirst().get();
suite.addAfterEachFunctionObj(function);
});
}
Aggregations