Skip to content

Commit

Permalink
feat: filter test execution by test plan case IDs
Browse files Browse the repository at this point in the history
Enhanced functionality to execute only the test methods specified in a test plan.

### Details:
- **Filter logic**: Test methods are filtered based on matching case IDs from the test plan.
- **Fallback behavior**: If no case IDs are defined, all test methods are executed as usual.
- **Configuration**: Case IDs are sourced via the `QASE_TESTOPS_PLAN_ID` environment variable or `qase.config.json`.

### Code improvements:
- Refactored the `intercept` method for better readability and efficiency.
- Added the `hasMatchingCaseId` helper method to encapsulate filtering logic.
  • Loading branch information
gibiw committed Nov 22, 2024
1 parent ba61e19 commit 3a7045d
Show file tree
Hide file tree
Showing 22 changed files with 97 additions and 16 deletions.
7 changes: 7 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# qase-java 4.0.3

## What's new

Implemented support for running only the tests included in a test plan.
To enable this feature, specify the test plan ID in the reporter configuration.
You can set it either in the `qase.config.json` file or through the `QASE_TESTOPS_PLAN_ID` environment variable.
2 changes: 1 addition & 1 deletion qase-api-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>qase-java</artifactId>
<groupId>io.qase</groupId>
<version>4.0.2</version>
<version>4.0.3</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion qase-api-v2-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>io.qase</groupId>
<artifactId>qase-java</artifactId>
<version>4.0.2</version>
<version>4.0.3</version>
</parent>

<artifactId>qase-api-v2-client</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion qase-cucumber-v3-reporter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>qase-java</artifactId>
<groupId>io.qase</groupId>
<version>4.0.2</version>
<version>4.0.3</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion qase-cucumber-v4-reporter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>qase-java</artifactId>
<groupId>io.qase</groupId>
<version>4.0.2</version>
<version>4.0.3</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion qase-cucumber-v5-reporter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>qase-java</artifactId>
<groupId>io.qase</groupId>
<version>4.0.2</version>
<version>4.0.3</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion qase-cucumber-v6-reporter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>qase-java</artifactId>
<groupId>io.qase</groupId>
<version>4.0.2</version>
<version>4.0.3</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion qase-cucumber-v7-reporter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>qase-java</artifactId>
<groupId>io.qase</groupId>
<version>4.0.2</version>
<version>4.0.3</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion qase-java-commons/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>io.qase</groupId>
<artifactId>qase-java</artifactId>
<version>4.0.2</version>
<version>4.0.3</version>
</parent>

<artifactId>qase-java-commons</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ public interface ApiClient {
void completeTestRun(Long runId) throws QaseException;

void uploadResults(Long runId, List<TestResult> results) throws QaseException;

List<Long> getTestCaseIdsForExecution() throws QaseException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.qase.client.v1.ApiClient;
import io.qase.client.v1.ApiException;
import io.qase.client.v1.api.AttachmentsApi;
import io.qase.client.v1.api.PlansApi;
import io.qase.client.v1.api.ResultsApi;
import io.qase.client.v1.api.RunsApi;
import io.qase.client.v1.models.*;
Expand Down Expand Up @@ -110,6 +111,29 @@ public void uploadResults(Long runId, List<TestResult> results) throws QaseExcep
}
}

@Override
public List<Long> getTestCaseIdsForExecution() throws QaseException {
if (this.config.testops.plan.id == 0) {
return Collections.emptyList();
}

try {
PlanResponse response = new PlansApi(client)
.getPlan(this.config.testops.project, this.config.testops.plan.id);

return Objects.requireNonNull(
Objects.requireNonNull(response.getResult())
.getCases())
.stream()
.map(PlanDetailedAllOfCases::getCaseId)
.collect(Collectors.toList()
);
} catch (ApiException e) {
throw new QaseException("Failed to get test case ids for execution: " + e.getResponseBody(), e.getCause());
}
}


private ResultCreate convertResult(TestResult result) {
ResultCreateCase caseModel = new ResultCreateCase()
.title(result.title);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -67,6 +68,11 @@ public void uploadResults(Long runId, List<TestResult> results) throws QaseExcep
}
}

@Override
public List<Long> getTestCaseIdsForExecution() throws QaseException {
return this.apiClientV1.getTestCaseIdsForExecution();
}

private ResultCreate convertResult(TestResult result) {
List<String> attachments = result.attachments.stream()
.map(this.apiClientV1::uploadAttachment)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package io.qase.commons.config;

public class PlanConfig {
public int id = 0;
public Integer id = 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import org.slf4j.LoggerFactory;
import org.slf4j.Logger;

import java.util.List;


public class CoreReporter implements Reporter {
private static final Logger logger = LoggerFactory.getLogger(CoreReporter.class);
Expand Down Expand Up @@ -54,6 +56,10 @@ public void uploadResults() {
executeWithFallback(() -> reporter.uploadResults(), "upload results");
}

public List<Long> getTestCaseIdsForExecution() {
return reporter.getTestCaseIdsForExecution();
}

private void executeWithFallback(ReporterAction action, String actionName) {
if (reporter != null) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -139,4 +140,9 @@ public void setResults(List<TestResult> results) {
this.results.clear();
this.results.addAll(results);
}

@Override
public List<Long> getTestCaseIdsForExecution() {
return Collections.emptyList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ public interface InternalReporter {
List<TestResult> getResults();

void setResults(List<TestResult> results);

List<Long> getTestCaseIdsForExecution();
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import io.qase.commons.QaseException;
import io.qase.commons.models.domain.TestResult;

import java.util.List;

public interface Reporter {
void startTestRun();

Expand All @@ -11,4 +13,6 @@ public interface Reporter {
void addResult(TestResult result);

void uploadResults();

List<Long> getTestCaseIdsForExecution();
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class TestopsReporter implements InternalReporter {
Expand Down Expand Up @@ -63,7 +64,7 @@ public void uploadResults() throws QaseException {
int batchSize = this.config.batch.size;
int totalResults = this.results.size();

if (totalResults == 0){
if (totalResults == 0) {
return;
}

Expand Down Expand Up @@ -92,6 +93,15 @@ public void setResults(List<TestResult> results) {
this.results.addAll(results);
}

@Override
public List<Long> getTestCaseIdsForExecution() {
try {
return this.client.getTestCaseIdsForExecution();
} catch (QaseException e) {
return Collections.emptyList();
}
}

private String prepareLink(Long id, String title) {
String baseLink = this.getBaseUrl(this.config.api.host) + "/run/"
+ this.config.project + "/dashboard/" + this.testRunId
Expand Down
2 changes: 1 addition & 1 deletion qase-junit4-reporter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>qase-java</artifactId>
<groupId>io.qase</groupId>
<version>4.0.2</version>
<version>4.0.3</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion qase-junit5-reporter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>qase-java</artifactId>
<groupId>io.qase</groupId>
<version>4.0.2</version>
<version>4.0.3</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion qase-testng-reporter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>qase-java</artifactId>
<groupId>io.qase</groupId>
<version>4.0.2</version>
<version>4.0.3</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import java.lang.reflect.Method;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static io.qase.commons.utils.IntegrationUtils.*;
Expand Down Expand Up @@ -191,9 +192,22 @@ private Map<String, String> getParameters(final ITestResult testResult) {
return testParameters;
}

// TODO: implement filtration
@Override
public List<IMethodInstance> intercept(List<IMethodInstance> list, ITestContext iTestContext) {
return list;
public List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext context) {
List<Long> caseIdsToExecute = qaseTestCaseListener.getTestCaseIdsForExecution();

if (caseIdsToExecute.isEmpty()) {
return methods;
}

return methods.stream()
.filter(this::hasMatchingCaseId)
.collect(Collectors.toList());
}

private boolean hasMatchingCaseId(IMethodInstance methodInstance) {
Method method = methodInstance.getMethod().getConstructorOrMethod().getMethod();
Long caseId = getCaseId(method);
return caseId != null && qaseTestCaseListener.getTestCaseIdsForExecution().contains(caseId);
}
}

0 comments on commit 3a7045d

Please sign in to comment.