Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add annotation to tag tests by test case ID #336

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add comment to clarify example:

Suggested change
@TcmsTestCaseId(11)
@TcmsTestCaseId(11) // optional

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>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Revert this change:

Suggested change
<version>12.6</version>
<version>12.5</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 @@
return null;
}
}

public TestCase getTestCaseById(int testCaseId) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will need some test coverage.

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 @@
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
9 changes: 8 additions & 1 deletion 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,7 +101,13 @@
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());
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();
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the purpose of this class? Is this where the new annotation is defined? If yes, I think model/ isn't the appropriate place for it. Maybe create a new directory like annotations/ ? WDYT ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or maybe add this class into TestMethod.java ? Not sure what makes sense here.

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
Loading