Skip to content

Commit

Permalink
Add annotation to tag tests by test case ID
Browse files Browse the repository at this point in the history
  • Loading branch information
Angelthree95 committed Jul 16, 2023
1 parent c4f5aeb commit 7ed16cd
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 3 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ This plugin extends `org.junit.jupiter.api.extension.Extension` and has
`junit.jupiter.extensions.autodetection.enabled=true` configured by default in
`pom.xml`. This means Jupiter will pick it up automatically.

You can point each executed test case to a specific case ID by annotating `@Test` method with `@TcmsTestCaseId(int)`,
where `int` is the test case ID. See the `KiwiTcmsExtension` example below.
If the test case is not found searching by ID, plugin will default to standard search method.
You can find the case ID in your TCMS instance URL (example: https://tcms.server/case/1)
or on the test case page in the test case name (TC-**1**: Test case 1).

You may alternatively decorate your test suite with the `KiwiTcmsExtension` class
but that should be redundant:

Expand All @@ -36,6 +42,7 @@ but that should be redundant:
@ExtendWith(KiwiTcmsExtension.class)
public class KiwiJsonRpcClientTest {
@Test
@TcmsTestCaseId(11)
public void yourTest(){
assertThat(...);
}
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>org.kiwitcms.java</groupId>
<artifactId>kiwitcms-junit-plugin</artifactId>
<packaging>jar</packaging>
<version>12.5</version>
<version>12.6</version>

<name>kiwitcms-junit-plugin</name>
<description>JUnit 5 plugin for Kiwi TCMS.</description>
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/org/kiwitcms/java/api/RpcClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -411,4 +411,22 @@ public TestExecutionStatus getTestExecutionStatus(String name, String weightLook
return null;
}
}

public TestCase getTestCaseById(int testCaseId) {
Map<String, Object> filter = new HashMap<>();
filter.put("id", testCaseId);
JSONArray jsonArray = (JSONArray) executeViaPositionalParams(TEST_CASE_FILTER, Arrays.asList(filter));

Check warning on line 418 in src/main/java/org/kiwitcms/java/api/RpcClient.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/kiwitcms/java/api/RpcClient.java#L416-L418

Added lines #L416 - L418 were not covered by tests
if (jsonArray == null || jsonArray.isEmpty()) {
System.out.printf("Case ID \"%s\" not found%n", testCaseId);
return null;

Check warning on line 421 in src/main/java/org/kiwitcms/java/api/RpcClient.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/kiwitcms/java/api/RpcClient.java#L420-L421

Added lines #L420 - L421 were not covered by tests
}

try {
TestCase[] testCases = new ObjectMapper().readValue(jsonArray.toJSONString(), TestCase[].class);
return testCases[0];
} catch (IOException e) {
e.printStackTrace();
return null;

Check warning on line 429 in src/main/java/org/kiwitcms/java/api/RpcClient.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/kiwitcms/java/api/RpcClient.java#L425-L429

Added lines #L425 - L429 were not covered by tests
}
}
}
5 changes: 5 additions & 0 deletions src/main/java/org/kiwitcms/java/junit/KiwiTcmsExtension.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package org.kiwitcms.java.junit;

import org.kiwitcms.java.config.Config;
import org.kiwitcms.java.model.TcmsTestCaseId;
import org.kiwitcms.java.model.TestMethod;
import org.junit.jupiter.api.TestInfo;
import org.junit.jupiter.api.TestInstance;
Expand Down Expand Up @@ -49,6 +50,10 @@ public void afterEach(ExtensionContext context){
test.result = "PASS";
}
test.containingClass = method.getDeclaringClass().getSimpleName();
if (method.isAnnotationPresent(TcmsTestCaseId.class)) {
TcmsTestCaseId tcmsTestCaseId = method.getAnnotation(TcmsTestCaseId.class);
test.id = tcmsTestCaseId.value();

Check warning on line 55 in src/main/java/org/kiwitcms/java/junit/KiwiTcmsExtension.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/kiwitcms/java/junit/KiwiTcmsExtension.java#L54-L55

Added lines #L54 - L55 were not covered by tests
}
tests.add(test);
}
}
Expand Down
11 changes: 9 additions & 2 deletions src/main/java/org/kiwitcms/java/junit/TestDataEmitter.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package org.kiwitcms.java.junit;

import net.minidev.json.JSONObject;
import org.apache.commons.lang3.ObjectUtils;
import org.kiwitcms.java.api.RpcClient;
import org.kiwitcms.java.config.Config;
import org.kiwitcms.java.model.*;
Expand Down Expand Up @@ -100,9 +101,15 @@ public void addTestResultsToRun(int runId, List<TestMethod> tests) {
int testPlanId = getPlanId();

for (TestMethod test : tests) {
TestCase testCase = client.getOrCreateTestCase(productId, categoryId, priorityId, test.getSummary());
TestCase testCase = null;

Check warning on line 104 in src/main/java/org/kiwitcms/java/junit/TestDataEmitter.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/kiwitcms/java/junit/TestDataEmitter.java#L104

Added line #L104 was not covered by tests
if (test.id != 0) {
testCase = client.getTestCaseById(test.id);

Check warning on line 106 in src/main/java/org/kiwitcms/java/junit/TestDataEmitter.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/kiwitcms/java/junit/TestDataEmitter.java#L106

Added line #L106 was not covered by tests
}
if (ObjectUtils.isEmpty(testCase)) {
testCase = client.getOrCreateTestCase(productId, categoryId, priorityId, test.getSummary());

Check warning on line 109 in src/main/java/org/kiwitcms/java/junit/TestDataEmitter.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/kiwitcms/java/junit/TestDataEmitter.java#L109

Added line #L109 was not covered by tests
}
client.addTestCaseToPlan(testPlanId, testCase.getCaseId());

TestExecution[] executions = client.addTestCaseToRunId(runId, testCase.getCaseId());
for (TestExecution testExecution : executions) {
client.updateTestExecution(testExecution.getTcRunId(), getTestExecutionStatusId(test.result));
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/org/kiwitcms/java/model/TcmsTestCaseId.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.kiwitcms.java.model;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface TcmsTestCaseId
{
int value();
}
1 change: 1 addition & 0 deletions src/main/java/org/kiwitcms/java/model/TestMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class TestMethod {
public String containingClass;
public String result;
public Throwable exception;
public int id;

public TestMethod(){};

Expand Down

0 comments on commit 7ed16cd

Please sign in to comment.