Search in sources :

Example 1 with TeststepRun

use of io.irontest.models.testrun.TeststepRun in project irontest by zheng-wang.

the class RegularTestcaseRunner method run.

@Override
public TestcaseRun run() throws JsonProcessingException {
    RegularTestcaseRun testcaseRun = new RegularTestcaseRun();
    preProcessingForIIBTestcase();
    startTestcaseRun(testcaseRun);
    if (isTestcaseHasWaitForProcessingCompletionAction()) {
        // milliseconds
        long secondFraction = getTestcaseRunContext().getTestcaseRunStartTime().getTime() % 1000;
        long millisecondsUntilNextSecond = 1000 - secondFraction;
        Teststep waitStep = getTestcase().getTeststeps().get(0);
        waitStep.setName("Wait " + millisecondsUntilNextSecond + " milliseconds");
        waitStep.setOtherProperties(new WaitTeststepProperties(millisecondsUntilNextSecond));
    }
    // run test steps
    for (Teststep teststep : getTestcase().getTeststeps()) {
        testcaseRun.getStepRuns().add(runTeststep(teststep));
    }
    // test case run ends
    testcaseRun.setDuration(new Date().getTime() - testcaseRun.getStartTime().getTime());
    LOGGER.info("Finish running test case: " + getTestcase().getName());
    for (TeststepRun teststepRun : testcaseRun.getStepRuns()) {
        if (TestResult.FAILED == teststepRun.getResult()) {
            testcaseRun.setResult(TestResult.FAILED);
            break;
        }
    }
    // persist test case run details into database
    getTestcaseRunDAO().insert(testcaseRun);
    // prepare return object for UI (reduced contents for performance)
    List<TeststepRun> teststepRunsForUI = new ArrayList<>();
    for (TeststepRun stepRun : testcaseRun.getStepRuns()) {
        TeststepRun teststepRunForUI = new TeststepRun();
        teststepRunForUI.setId(stepRun.getId());
        teststepRunForUI.setResult(stepRun.getResult());
        Teststep teststepForUI = new Teststep();
        teststepForUI.setId(stepRun.getTeststep().getId());
        teststepRunForUI.setTeststep(teststepForUI);
        teststepRunsForUI.add(teststepRunForUI);
    }
    RegularTestcaseRun testcaseRunForUI = new RegularTestcaseRun();
    testcaseRunForUI.setId(testcaseRun.getId());
    testcaseRunForUI.setResult(testcaseRun.getResult());
    testcaseRunForUI.setStepRuns(teststepRunsForUI);
    return testcaseRunForUI;
}
Also used : Teststep(io.irontest.models.teststep.Teststep) WaitTeststepProperties(io.irontest.models.teststep.WaitTeststepProperties) TeststepRun(io.irontest.models.testrun.TeststepRun) ArrayList(java.util.ArrayList) RegularTestcaseRun(io.irontest.models.testrun.RegularTestcaseRun) Date(java.util.Date)

Example 2 with TeststepRun

use of io.irontest.models.testrun.TeststepRun in project irontest by zheng-wang.

the class TeststepRunMapper method map.

public TeststepRun map(int index, ResultSet rs, StatementContext ctx) throws SQLException {
    TeststepRun teststepRun = new TeststepRun();
    ObjectMapper objectMapper = new ObjectMapper();
    teststepRun.setStartTime(rs.getTimestamp("starttime"));
    teststepRun.setDuration(rs.getLong("duration"));
    teststepRun.setResult(TestResult.getByText(rs.getString("result")));
    Teststep teststep = null;
    try {
        teststep = objectMapper.readValue(rs.getString("teststep"), Teststep.class);
    } catch (IOException e) {
        throw new SQLException("Failed to deserialize teststep JSON.", e);
    }
    teststepRun.setTeststep(teststep);
    // Use LinkedHashMap here instead of Object (for covering specific response type like DBAPIResponse),
    // because TeststepRun is used for displaying report, so JSON representation of the response is sufficient.
    LinkedHashMap response = null;
    try {
        response = objectMapper.readValue(rs.getString("response"), LinkedHashMap.class);
    } catch (IOException e) {
        throw new SQLException("Failed to deserialize response JSON.", e);
    }
    teststepRun.setResponse(response);
    teststepRun.setInfoMessage(rs.getString("info_message"));
    teststepRun.setErrorMessage(rs.getString("error_message"));
    List<AssertionVerification> assertionVerifications = null;
    try {
        assertionVerifications = new ObjectMapper().readValue(rs.getString("assertion_verifications"), new TypeReference<List<AssertionVerification>>() {
        });
    } catch (IOException e) {
        throw new SQLException("Failed to deserialize stepruns JSON.", e);
    }
    teststepRun.setAssertionVerifications(assertionVerifications);
    return teststepRun;
}
Also used : Teststep(io.irontest.models.teststep.Teststep) TeststepRun(io.irontest.models.testrun.TeststepRun) SQLException(java.sql.SQLException) IOException(java.io.IOException) AssertionVerification(io.irontest.models.assertion.AssertionVerification) TypeReference(com.fasterxml.jackson.core.type.TypeReference) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) LinkedHashMap(java.util.LinkedHashMap)

Example 3 with TeststepRun

use of io.irontest.models.testrun.TeststepRun in project irontest by zheng-wang.

the class DataDrivenTestcaseRunner method run.

@Override
public TestcaseRun run() throws JsonProcessingException {
    DataDrivenTestcaseRun testcaseRun = new DataDrivenTestcaseRun();
    Cloner cloner = new Cloner();
    preProcessingForIIBTestcase();
    startTestcaseRun(testcaseRun);
    for (int dataTableRowIndex = 0; dataTableRowIndex < dataTable.getRows().size(); dataTableRowIndex++) {
        LinkedHashMap<String, Object> dataTableRow = dataTable.getRows().get(dataTableRowIndex);
        TestcaseIndividualRun individualRun = new TestcaseIndividualRun();
        testcaseRun.getIndividualRuns().add(individualRun);
        // start test case individual run
        individualRun.setStartTime(new Date());
        LOGGER.info("Start individually running test case with data table row: " + individualRun.getCaption());
        individualRun.setResult(TestResult.PASSED);
        getTestcaseRunContext().setTestcaseIndividualRunStartTime(individualRun.getStartTime());
        if (isTestcaseHasWaitForProcessingCompletionAction()) {
            // milliseconds
            long secondFraction = individualRun.getStartTime().getTime() % 1000;
            long millisecondsUntilNextSecond = 1000 - secondFraction;
            Teststep waitStep = getTestcase().getTeststeps().get(0);
            waitStep.setName("Wait " + millisecondsUntilNextSecond + " milliseconds");
            waitStep.setOtherProperties(new WaitTeststepProperties(millisecondsUntilNextSecond));
        }
        getReferenceableStringProperties().put(IMPLICIT_PROPERTY_NAME_TEST_CASE_INDIVIDUAL_START_TIME, IMPLICIT_PROPERTY_DATE_TIME_FORMAT.format(individualRun.getStartTime()));
        individualRun.setCaption((String) dataTableRow.get(DataTableColumn.COLUMN_NAME_CAPTION));
        getReferenceableEndpointProperties().putAll(dataTable.getEndpointPropertiesInRow(dataTableRowIndex));
        getReferenceableStringProperties().putAll(dataTable.getStringPropertiesInRow(dataTableRowIndex));
        // run test steps
        for (Teststep teststep : getTestcase().getTeststeps()) {
            Teststep clonedTeststep = cloner.deepClone(teststep);
            individualRun.getStepRuns().add(runTeststep(clonedTeststep));
        }
        // test case individual run ends
        individualRun.setDuration(new Date().getTime() - individualRun.getStartTime().getTime());
        LOGGER.info("Finish individually running test case with data table row: " + individualRun.getCaption());
        for (TeststepRun teststepRun : individualRun.getStepRuns()) {
            if (TestResult.FAILED == teststepRun.getResult()) {
                individualRun.setResult(TestResult.FAILED);
                break;
            }
        }
    }
    // test case run ends
    testcaseRun.setDuration(new Date().getTime() - testcaseRun.getStartTime().getTime());
    LOGGER.info("Finish running test case: " + getTestcase().getName());
    for (TestcaseIndividualRun individualRun : testcaseRun.getIndividualRuns()) {
        if (TestResult.FAILED == individualRun.getResult()) {
            testcaseRun.setResult(TestResult.FAILED);
            break;
        }
    }
    // persist test case run details into database
    getTestcaseRunDAO().insert(testcaseRun);
    return testcaseRun;
}
Also used : Teststep(io.irontest.models.teststep.Teststep) WaitTeststepProperties(io.irontest.models.teststep.WaitTeststepProperties) TeststepRun(io.irontest.models.testrun.TeststepRun) TestcaseIndividualRun(io.irontest.models.testrun.TestcaseIndividualRun) DataDrivenTestcaseRun(io.irontest.models.testrun.DataDrivenTestcaseRun) Date(java.util.Date) Cloner(com.rits.cloning.Cloner)

Example 4 with TeststepRun

use of io.irontest.models.testrun.TeststepRun in project irontest by zheng-wang.

the class TestcaseRunner method runTeststep.

protected TeststepRun runTeststep(Teststep teststep) {
    TeststepRun teststepRun = new TeststepRun();
    teststepRun.setTeststep(teststep);
    // test step run starts
    Date teststepRunStartTime = new Date();
    teststepRun.setStartTime(teststepRunStartTime);
    referenceableStringProperties.put(IMPLICIT_PROPERTY_NAME_TEST_STEP_START_TIME, IMPLICIT_PROPERTY_DATE_TIME_FORMAT.format(teststepRunStartTime));
    LOGGER.info("Start running test step: " + teststep.getName());
    // run test step
    BasicTeststepRun basicTeststepRun;
    // use this flag instead of checking stepRun.getErrorMessage() != null, for code clarity
    boolean exceptionOccurred = false;
    try {
        basicTeststepRun = TeststepRunnerFactory.getInstance().newTeststepRunner(teststep, teststepDAO, utilsDAO, referenceableStringProperties, referenceableEndpointProperties, testcaseRunContext).run();
        LOGGER.info("Finish running test step: " + teststep.getName());
        teststepRun.setResponse(basicTeststepRun.getResponse());
        teststepRun.setInfoMessage(basicTeststepRun.getInfoMessage());
    } catch (Exception e) {
        exceptionOccurred = true;
        String message = e.getMessage();
        // exception message could be null (though rarely)
        teststepRun.setErrorMessage(message == null ? "null" : message);
        LOGGER.error(message, e);
    }
    // verify assertions
    if (exceptionOccurred) {
        teststepRun.setResult(TestResult.FAILED);
    } else {
        teststepRun.setResult(TestResult.PASSED);
        // get input for assertion verifications
        Object apiResponse = teststepRun.getResponse();
        Object assertionVerificationInput;
        if (Teststep.TYPE_SOAP.equals(teststep.getType())) {
            assertionVerificationInput = ((SOAPAPIResponse) apiResponse).getHttpBody();
        } else if (Teststep.TYPE_DB.equals(teststep.getType())) {
            assertionVerificationInput = ((DBAPIResponse) apiResponse).getRowsJSON();
        } else if (Teststep.TYPE_MQ.equals(teststep.getType())) {
            assertionVerificationInput = ((MQAPIResponse) apiResponse).getValue();
        } else {
            assertionVerificationInput = apiResponse;
        }
        if (Teststep.TYPE_DB.equals(teststep.getType()) && assertionVerificationInput == null) {
        // SQL inserts/deletes/updates, no assertion verification needed
        } else {
            // verify assertions against the input
            for (Assertion assertion : teststep.getAssertions()) {
                AssertionVerification verification = new AssertionVerification();
                teststepRun.getAssertionVerifications().add(verification);
                verification.setAssertion(assertion);
                AssertionVerifier verifier = AssertionVerifierFactory.getInstance().create(assertion.getType(), referenceableStringProperties);
                AssertionVerificationResult verificationResult;
                try {
                    verificationResult = verifier.verify(assertion, assertionVerificationInput);
                } catch (Exception e) {
                    LOGGER.error("Failed to verify assertion", e);
                    verificationResult = new AssertionVerificationResult();
                    verificationResult.setResult(TestResult.FAILED);
                    String message = e.getMessage();
                    // exception message could be null (though rarely)
                    verificationResult.setError(message == null ? "null" : message);
                }
                verification.setVerificationResult(verificationResult);
                if (TestResult.FAILED == verificationResult.getResult()) {
                    teststepRun.setResult(TestResult.FAILED);
                }
            }
        }
    }
    // test step run ends
    teststepRun.setDuration(new Date().getTime() - teststepRun.getStartTime().getTime());
    return teststepRun;
}
Also used : AssertionVerifier(io.irontest.core.assertion.AssertionVerifier) TeststepRun(io.irontest.models.testrun.TeststepRun) AssertionVerificationResult(io.irontest.models.assertion.AssertionVerificationResult) Assertion(io.irontest.models.assertion.Assertion) AssertionVerification(io.irontest.models.assertion.AssertionVerification) Date(java.util.Date) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Aggregations

TeststepRun (io.irontest.models.testrun.TeststepRun)4 Teststep (io.irontest.models.teststep.Teststep)3 Date (java.util.Date)3 AssertionVerification (io.irontest.models.assertion.AssertionVerification)2 WaitTeststepProperties (io.irontest.models.teststep.WaitTeststepProperties)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Cloner (com.rits.cloning.Cloner)1 AssertionVerifier (io.irontest.core.assertion.AssertionVerifier)1 Assertion (io.irontest.models.assertion.Assertion)1 AssertionVerificationResult (io.irontest.models.assertion.AssertionVerificationResult)1 DataDrivenTestcaseRun (io.irontest.models.testrun.DataDrivenTestcaseRun)1 RegularTestcaseRun (io.irontest.models.testrun.RegularTestcaseRun)1 TestcaseIndividualRun (io.irontest.models.testrun.TestcaseIndividualRun)1 IOException (java.io.IOException)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1