Search in sources :

Example 6 with TestExecution

use of com.teamscale.report.testwise.model.TestExecution in project teamscale-jacoco-agent by cqse.

the class RunningTest method endTestAndRetrieveCoverage.

/**
 * Signals to the agent that the test runner has finished executing this test and the result of the test run. It
 * will also parse the testwise coverage data returned by the agent for this test and return it so it can be
 * manually processed by you. The agent will not store or otherwise process this coverage, so be sure to do so
 * yourself.
 * <p>
 * This method assumes that the agent is configured to return each test's coverage data via HTTP. It will receive
 * and parse the data. If the agent is configured differently, this method will throw a terminal {@link
 * RuntimeException}. In this case, you must reconfigure the agent with the `tia-mode=http` option enabled.
 *
 * @throws AgentHttpRequestFailedException if communicating with the agent fails or in case of internal errors. This
 *                                         method already retries the request once, so this is likely a terminal
 *                                         failure. The recorded coverage is likely lost. The caller should log this
 *                                         problem appropriately.
 */
public TestInfo endTestAndRetrieveCoverage(TestRun.TestResultWithMessage result) throws AgentHttpRequestFailedException {
    // the agent already records test duration, so we can simply provide a dummy value here
    TestExecution execution = new TestExecution(uniformPath, 0L, result.result, result.message);
    ResponseBody body = AgentCommunicationUtils.handleRequestError(() -> api.testFinished(uniformPath, execution), "Failed to end coverage recording for test case " + uniformPath + ". Coverage for that test case is most likely lost.");
    String json = readBodyStringNullSafe(body);
    if (StringUtils.isBlank(json)) {
        throw new AgentConfigurationMismatch("You asked the tia-client to retrieve this test's coverage via HTTP" + " but the agent is not configured for this. Please reconfigure the agent to use `tia-mode=http`.");
    }
    try {
        return testInfoJsonAdapter.fromJson(json);
    } catch (JsonDataException | IOException e) {
        throw new AgentHttpRequestFailedException("Unable to parse the JSON returned by the agent. Maybe you have" + " a version mismatch between the tia-client and the agent?. Json returned by the agent: `" + json + "`", e);
    }
}
Also used : TestExecution(com.teamscale.report.testwise.model.TestExecution) JsonDataException(com.squareup.moshi.JsonDataException) IOException(java.io.IOException) ResponseBody(okhttp3.ResponseBody)

Example 7 with TestExecution

use of com.teamscale.report.testwise.model.TestExecution in project teamscale-jacoco-agent by cqse.

the class TestwiseCoverageCollectingExecutionListenerTest method testInteractionWithListenersAndCoverageApi.

@SuppressWarnings("unchecked")
@Test
void testInteractionWithListenersAndCoverageApi() {
    UniqueId testClassId = rootId.append("TEST_CONTAINER", "MyClass");
    UniqueId impactedTestCaseId = testClassId.append("TEST_CASE", "impactedTestCase()");
    UniqueId nonImpactedTestCaseId = testClassId.append("TEST_CASE", "nonImpactedTestCase()");
    UniqueId regularSkippedTestCaseId = testClassId.append("TEST_CASE", "regularSkippedTestCase()");
    TestDescriptor impactedTestCase = testCase(impactedTestCaseId);
    TestDescriptor nonImpactedTestCase = testCase(nonImpactedTestCaseId);
    TestDescriptor regularSkippedTestCase = testCase(regularSkippedTestCaseId);
    TestDescriptor testClass = testContainer(testClassId, impactedTestCase, nonImpactedTestCase, regularSkippedTestCase);
    TestDescriptor testRoot = testContainer(rootId, testClass);
    when(resolver.getUniformPath(impactedTestCase)).thenReturn(Optional.of("MyClass/impactedTestCase()"));
    when(resolver.getClusterId(impactedTestCase)).thenReturn(Optional.of("MyClass"));
    when(resolver.getUniformPath(nonImpactedTestCase)).thenReturn(Optional.of("MyClass/nonImpactedTestCase()"));
    when(resolver.getClusterId(nonImpactedTestCase)).thenReturn(Optional.of("MyClass"));
    when(resolver.getUniformPath(regularSkippedTestCase)).thenReturn(Optional.of("MyClass/regularSkippedTestCase()"));
    when(resolver.getClusterId(regularSkippedTestCase)).thenReturn(Optional.of("MyClass"));
    when(mockApi.testStarted(anyString())).thenReturn(mock(Call.class));
    when(mockApi.testFinished(anyString())).thenReturn(mock(Call.class));
    // Start engine and class.
    executionListener.executionStarted(testRoot);
    verify(executionListenerMock).executionStarted(testRoot);
    executionListener.executionStarted(testClass);
    verify(executionListenerMock).executionStarted(testClass);
    // Execution of impacted test case.
    executionListener.executionStarted(impactedTestCase);
    verify(mockApi).testStarted("MyClass/impactedTestCase()");
    verify(executionListenerMock).executionStarted(impactedTestCase);
    executionListener.executionFinished(impactedTestCase, successful());
    verify(mockApi).testFinished(eq("MyClass/impactedTestCase()"));
    verify(executionListenerMock).executionFinished(impactedTestCase, successful());
    // Non impacted test case is skipped.
    executionListener.executionSkipped(nonImpactedTestCase, TEST_NOT_IMPACTED_REASON);
    verify(executionListenerMock).executionSkipped(nonImpactedTestCase, TEST_NOT_IMPACTED_REASON);
    // Ignored or disabled impacted test case is skipped.
    executionListener.executionSkipped(regularSkippedTestCase, "Test is disabled.");
    verify(executionListenerMock).executionSkipped(regularSkippedTestCase, "Test is disabled.");
    // Finish class and engine.
    executionListener.executionFinished(testClass, successful());
    verify(executionListenerMock).executionFinished(testClass, successful());
    executionListener.executionFinished(testRoot, successful());
    verify(executionListenerMock).executionFinished(testRoot, successful());
    verifyNoMoreInteractions(mockApi);
    verifyNoMoreInteractions(executionListenerMock);
    List<TestExecution> testExecutions = executionListener.getTestExecutions();
    assertThat(testExecutions).hasSize(2);
    assertThat(testExecutions).allSatisfy(testExecution -> assertThat(testExecution.getUniformPath()).isNotEqualTo("MyClass/nonImpactedTestCase()"));
    assertThat(testExecutions).anySatisfy(testExecution -> assertThat(testExecution.getUniformPath()).isEqualTo("MyClass/impactedTestCase()"));
    assertThat(testExecutions).anySatisfy(testExecution -> assertThat(testExecution.getUniformPath()).isEqualTo("MyClass/regularSkippedTestCase()"));
}
Also used : UniqueId(org.junit.platform.engine.UniqueId) Call(retrofit2.Call) TestExecution(com.teamscale.report.testwise.model.TestExecution) TestDescriptor(org.junit.platform.engine.TestDescriptor) Test(org.junit.jupiter.api.Test)

Example 8 with TestExecution

use of com.teamscale.report.testwise.model.TestExecution in project teamscale-jacoco-agent by cqse.

the class TestwiseCoverageCollectingExecutionListenerTest method testSkipOfTestClass.

@Test
void testSkipOfTestClass() {
    UniqueId testClassId = rootId.append("TEST_CONTAINER", "MyClass");
    UniqueId testCase1Id = testClassId.append("TEST_CASE", "testCase1()");
    UniqueId testCase2Id = testClassId.append("TEST_CASE", "testCase2()");
    TestDescriptor testCase1 = testCase(testCase1Id);
    TestDescriptor testCase2 = testCase(testCase2Id);
    TestDescriptor testClass = testContainer(testClassId, testCase1, testCase2);
    TestDescriptor testRoot = testContainer(rootId, testClass);
    when(resolver.getUniformPath(testCase1)).thenReturn(Optional.of("MyClass/testCase1()"));
    when(resolver.getClusterId(testCase1)).thenReturn(Optional.of("MyClass"));
    when(resolver.getUniformPath(testCase2)).thenReturn(Optional.of("MyClass/testCase2()"));
    when(resolver.getClusterId(testCase2)).thenReturn(Optional.of("MyClass"));
    // Start engine and class.
    executionListener.executionStarted(testRoot);
    verify(executionListenerMock).executionStarted(testRoot);
    executionListener.executionSkipped(testClass, "Test class is disabled.");
    verify(executionListenerMock).executionStarted(testClass);
    verify(executionListenerMock).executionSkipped(testCase1, "Test class is disabled.");
    verify(executionListenerMock).executionSkipped(testCase2, "Test class is disabled.");
    verify(executionListenerMock).executionFinished(testClass, successful());
    executionListener.executionFinished(testRoot, successful());
    verify(executionListenerMock).executionFinished(testRoot, successful());
    verifyNoMoreInteractions(executionListenerMock);
    List<TestExecution> testExecutions = executionListener.getTestExecutions();
    assertThat(testExecutions).hasSize(2);
    assertThat(testExecutions).allMatch(testExecution -> testExecution.getResult().equals(ETestExecutionResult.SKIPPED));
}
Also used : UniqueId(org.junit.platform.engine.UniqueId) TestExecution(com.teamscale.report.testwise.model.TestExecution) TestDescriptor(org.junit.platform.engine.TestDescriptor) Test(org.junit.jupiter.api.Test)

Example 9 with TestExecution

use of com.teamscale.report.testwise.model.TestExecution in project teamscale-jacoco-agent by cqse.

the class ImpactedTestsExecutor method execute.

@Override
public List<TestExecution> execute(TestExecutorRequest executorRequest) {
    AvailableTests availableTestDetails = TestDescriptorUtils.getAvailableTests(executorRequest.testEngine, executorRequest.engineTestDescriptor);
    List<PrioritizableTestCluster> testClusters = impactedTestsProvider.getImpactedTestsFromTeamscale(availableTestDetails.getTestList());
    if (testClusters == null) {
        LOGGER.debug(() -> "Falling back to execute all!");
        return super.execute(executorRequest);
    }
    AutoSkippingEngineExecutionListener executionListener = new AutoSkippingEngineExecutionListener(getImpactedTestUniqueIds(availableTestDetails, testClusters), executorRequest.engineExecutionListener, executorRequest.engineTestDescriptor);
    List<TestExecution> testExecutions = new ArrayList<>();
    LOGGER.debug(() -> "Re-discovering tests for delegate engine " + executorRequest.testEngine.getId());
    for (PrioritizableTestCluster testCluster : testClusters) {
        Set<UniqueId> uniqueIdsOfTestsToExecute = availableTestDetails.convertToUniqueIds(testCluster.tests);
        UniqueIdsDiscoveryRequest engineDiscoveryRequest = new UniqueIdsDiscoveryRequest(uniqueIdsOfTestsToExecute, executorRequest.configurationParameters);
        TestDescriptor testDescriptor = executorRequest.testEngine.discover(engineDiscoveryRequest, UniqueId.forEngine(executorRequest.testEngine.getId()));
        TestExecutorRequest impactedExecutorRequest = new TestExecutorRequest(executorRequest.testEngine, testDescriptor, executionListener, executorRequest.configurationParameters);
        List<TestExecution> testExecutionsForCluster = super.execute(impactedExecutorRequest);
        testExecutions.addAll(testExecutionsForCluster);
    }
    return testExecutions;
}
Also used : UniqueId(org.junit.platform.engine.UniqueId) TestExecution(com.teamscale.report.testwise.model.TestExecution) ArrayList(java.util.ArrayList) PrioritizableTestCluster(com.teamscale.client.PrioritizableTestCluster) TestDescriptor(org.junit.platform.engine.TestDescriptor)

Example 10 with TestExecution

use of com.teamscale.report.testwise.model.TestExecution in project teamscale-jacoco-agent by cqse.

the class TestwiseCoverageReportBuilder method createFrom.

/**
 * Adds the {@link TestCoverageBuilder} to the map. If there is already a test with the same ID the coverage is
 * merged.
 */
public static TestwiseCoverageReport createFrom(Collection<? extends TestDetails> testDetailsList, Collection<TestCoverageBuilder> testCoverage, Collection<TestExecution> testExecutions) {
    TestwiseCoverageReportBuilder report = new TestwiseCoverageReportBuilder();
    for (TestDetails testDetails : testDetailsList) {
        TestInfoBuilder container = new TestInfoBuilder(testDetails.uniformPath);
        container.setDetails(testDetails);
        report.tests.put(testDetails.uniformPath, container);
    }
    for (TestCoverageBuilder coverage : testCoverage) {
        TestInfoBuilder container = resolveUniformPath(report, coverage.getUniformPath());
        if (container == null) {
            continue;
        }
        container.setCoverage(coverage);
    }
    for (TestExecution testExecution : testExecutions) {
        TestInfoBuilder container = resolveUniformPath(report, testExecution.getUniformPath());
        if (container == null) {
            continue;
        }
        container.setExecution(testExecution);
    }
    return report.build();
}
Also used : TestExecution(com.teamscale.report.testwise.model.TestExecution) TestDetails(com.teamscale.client.TestDetails)

Aggregations

TestExecution (com.teamscale.report.testwise.model.TestExecution)18 TestDetails (com.teamscale.client.TestDetails)6 Test (org.junit.jupiter.api.Test)6 ArrayList (java.util.ArrayList)4 TestDescriptor (org.junit.platform.engine.TestDescriptor)4 File (java.io.File)3 UniqueId (org.junit.platform.engine.UniqueId)3 PrioritizableTest (com.teamscale.client.PrioritizableTest)2 PrioritizableTestCluster (com.teamscale.client.PrioritizableTestCluster)2 AgentOptions (com.teamscale.jacoco.agent.options.AgentOptions)2 TestwiseCoverage (com.teamscale.report.testwise.model.TestwiseCoverage)2 TestInfoBuilder (com.teamscale.report.testwise.model.builder.TestInfoBuilder)2 IOException (java.io.IOException)2 Path (java.nio.file.Path)2 ResponseBody (okhttp3.ResponseBody)2 JsonDataException (com.squareup.moshi.JsonDataException)1 ClusteredTestDetails (com.teamscale.client.ClusteredTestDetails)1 JacocoRuntimeController (com.teamscale.jacoco.agent.JacocoRuntimeController)1 Benchmark (com.teamscale.jacoco.agent.util.Benchmark)1 TestwiseCoverageReportWriter (com.teamscale.report.testwise.TestwiseCoverageReportWriter)1