Skip to content

Commit 5bd20fa

Browse files
Merge pull request #72 from testomatio/id-test-runner-testng
Implemented id run filtering for testng
2 parents 3a2ad8f + 13531b1 commit 5bd20fa

File tree

4 files changed

+88
-344
lines changed

4 files changed

+88
-344
lines changed

java-reporter-testng/pom.xml

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
55
<modelVersion>4.0.0</modelVersion>
66

7-
<groupId>io.testomat</groupId>
7+
<groupId>test</groupId>
88
<artifactId>java-reporter-testng</artifactId>
9-
<version>0.6.8</version>
9+
<version>test</version>
1010
<packaging>jar</packaging>
1111

1212
<name>Testomat.io Java Reporter TestNG</name>
@@ -46,7 +46,7 @@
4646
<dependency>
4747
<groupId>io.testomat</groupId>
4848
<artifactId>java-reporter-core</artifactId>
49-
<version>0.6.8</version>
49+
<version>0.7.5</version>
5050
</dependency>
5151
<dependency>
5252
<groupId>org.testng</groupId>
@@ -94,6 +94,17 @@
9494
</configuration>
9595
</plugin>
9696

97+
<plugin>
98+
<groupId>org.apache.maven.plugins</groupId>
99+
<artifactId>maven-surefire-plugin</artifactId>
100+
<version>3.0.0-M9</version>
101+
<configuration>
102+
<suiteXmlFiles>
103+
<suiteXmlFile>testng.xml</suiteXmlFile>
104+
</suiteXmlFiles>
105+
</configuration>
106+
</plugin>
107+
97108
<plugin>
98109
<groupId>org.apache.maven.plugins</groupId>
99110
<artifactId>maven-source-plugin</artifactId>
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package io.testomat.testng.filter;
2+
3+
import io.testomat.core.annotation.TestId;
4+
import org.testng.ITestResult;
5+
import org.testng.SkipException;
6+
7+
/**
8+
* Filter for TestNG tests based on @TestId annotation.
9+
* Provides filtering logic to include only tests with matching IDs.
10+
*/
11+
public class TestIdFilter {
12+
13+
/**
14+
* Checks if test should be executed based on provided filter IDs.
15+
* Throws SkipException if test should be skipped.
16+
*
17+
* @param result test result containing method information
18+
* @throws SkipException if test should be filtered out
19+
*/
20+
public void filterTest(ITestResult result) {
21+
String providedIds = System.getProperty("ids");
22+
if (providedIds == null || providedIds.trim().isEmpty()) {
23+
return;
24+
}
25+
26+
TestId testIdAnnotation = result.getMethod().getConstructorOrMethod()
27+
.getMethod().getAnnotation(TestId.class);
28+
29+
if (testIdAnnotation == null) {
30+
throw new SkipException("Test filtered: no @TestId annotation");
31+
}
32+
33+
String testId = testIdAnnotation.value();
34+
if (!isTestIdIncluded(testId, providedIds)) {
35+
throw new SkipException("Test filtered by ID: " + testId);
36+
}
37+
}
38+
39+
private boolean isTestIdIncluded(String testId, String providedIds) {
40+
if (testId == null || providedIds == null) {
41+
return false;
42+
}
43+
44+
String[] allowedIds = providedIds.split(",");
45+
for (String allowedId : allowedIds) {
46+
String trimmed = allowedId.trim();
47+
if (!trimmed.isEmpty() && testId.equals(trimmed)) {
48+
return true;
49+
}
50+
}
51+
return false;
52+
}
53+
}

java-reporter-testng/src/main/java/io/testomat/testng/listener/TestNgListener.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
import io.testomat.core.propertyconfig.impl.PropertyProviderFactoryImpl;
88
import io.testomat.core.propertyconfig.interf.PropertyProvider;
99
import io.testomat.core.runmanager.GlobalRunManager;
10+
import io.testomat.testng.filter.TestIdFilter;
1011
import io.testomat.testng.methodexporter.TestNgMethodExportManager;
1112
import io.testomat.testng.reporter.TestNgTestResultReporter;
1213
import java.util.Set;
1314
import java.util.concurrent.ConcurrentHashMap;
1415
import org.slf4j.Logger;
1516
import org.slf4j.LoggerFactory;
17+
import org.testng.IInvokedMethod;
1618
import org.testng.IInvokedMethodListener;
1719
import org.testng.ISuite;
1820
import org.testng.ISuiteListener;
@@ -23,10 +25,10 @@
2325
/**
2426
* TestNG listener for Testomat.io integration.
2527
* Reports TestNG test execution results to Testomat.io platform.
26-
* Supports custom annotations (@Title, @TestId) and handles disabled tests.
2728
* Also exports test method bodies when required.
2829
*/
29-
public class TestNgListener implements ISuiteListener, ITestListener, IInvokedMethodListener {
30+
public class TestNgListener implements ISuiteListener, ITestListener,
31+
IInvokedMethodListener {
3032
private static final Logger log = LoggerFactory.getLogger(TestNgListener.class);
3133
private static final String LISTENING_REQUIRED_PROPERTY_NAME = "testomatio.listening";
3234

@@ -36,6 +38,7 @@ public class TestNgListener implements ISuiteListener, ITestListener, IInvokedMe
3638
private final PropertyProvider provider;
3739

3840
private final Set<String> processedClasses;
41+
private final TestIdFilter testIdFilter;
3942

4043
public TestNgListener() {
4144
this.methodExportManager = new TestNgMethodExportManager();
@@ -44,6 +47,7 @@ public TestNgListener() {
4447
this.reporter = new TestNgTestResultReporter();
4548
this.provider =
4649
PropertyProviderFactoryImpl.getPropertyProviderFactory().getPropertyProvider();
50+
this.testIdFilter = new TestIdFilter();
4751
}
4852

4953
/**
@@ -58,6 +62,7 @@ public TestNgListener(TestNgMethodExportManager methodExportManager,
5862
this.methodExportManager = methodExportManager;
5963
this.provider = provider;
6064
this.processedClasses = ConcurrentHashMap.newKeySet();
65+
this.testIdFilter = new TestIdFilter();
6166
}
6267

6368
@Override
@@ -117,6 +122,15 @@ public void onTestFailure(ITestResult result) {
117122
exportTestClassIfNotProcessed(result.getTestClass().getRealClass());
118123
}
119124

125+
@Override
126+
public void onTestStart(ITestResult result) {
127+
if (!isListeningRequired()) {
128+
return;
129+
}
130+
131+
testIdFilter.filterTest(result);
132+
}
133+
120134
@Override
121135
public void onTestSkipped(ITestResult result) {
122136
if (!isListeningRequired()) {
@@ -140,6 +154,11 @@ private void exportTestClassIfNotProcessed(Class<?> testClass) {
140154
}
141155
}
142156

157+
@Override
158+
public void afterInvocation(IInvokedMethod method, ITestResult testResult) {
159+
160+
}
161+
143162
private boolean isListeningRequired() {
144163
try {
145164
return provider.getProperty(LISTENING_REQUIRED_PROPERTY_NAME) != null;

0 commit comments

Comments
 (0)