Search in sources :

Example 1 with TesterinaResult

use of org.ballerinalang.testerina.core.entity.TesterinaResult 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);
    });
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TesterinaResult(org.ballerinalang.testerina.core.entity.TesterinaResult) TestSuite(org.ballerinalang.testerina.core.entity.TestSuite) BValue(org.ballerinalang.model.values.BValue) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException)

Aggregations

AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 BValue (org.ballerinalang.model.values.BValue)1 TestSuite (org.ballerinalang.testerina.core.entity.TestSuite)1 TesterinaResult (org.ballerinalang.testerina.core.entity.TesterinaResult)1 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)1