Skip to content

Commit

Permalink
feat: support test case selection in TestNG framework (#4506)
Browse files Browse the repository at this point in the history
needed for: scalameta/metals#7200

This allows to select and run single tests in Metals using
TestNGFramework.

![TestNGSingleTest](https://github.com/user-attachments/assets/9038111e-ef84-46cd-9168-9a34a6112d5a)

Previously only whole suite selection was possible.

Tested it manually as visible on the GIF above.

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
  • Loading branch information
kasiaMarek and autofix-ci[bot] authored Feb 13, 2025
1 parent 56aca57 commit 35fc951
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion contrib/testng/src/mill/testng/ResultEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public Fingerprint fingerprint() {
}

public Selector selector() {
return new SuiteSelector();
return new TestSelector(testNGResult.getName());
}

public Status status() {
Expand Down
19 changes: 19 additions & 0 deletions contrib/testng/src/mill/testng/TestNGRunner.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package mill.testng;

import com.beust.jcommander.JCommander;
import java.util.ArrayList;
import java.util.List;
import org.testng.CommandLineArgs;
import sbt.testing.EventHandler;
import sbt.testing.Logger;
import sbt.testing.Runner;
import sbt.testing.Selector;
import sbt.testing.SuiteSelector;
import sbt.testing.Task;
import sbt.testing.TaskDef;
import sbt.testing.TestSelector;

class TestNGTask implements Task {

Expand Down Expand Up @@ -55,11 +60,25 @@ public Task[] tasks(TaskDef[] taskDefs) {
CommandLineArgs cliArgs = new CommandLineArgs();
new JCommander(cliArgs).parse(args); // args is an output parameter of the constructor!
cliArgs.testClass = taskDefs[i].fullyQualifiedName();
cliArgs.commandLineMethods =
testMethods(taskDefs[i].selectors(), taskDefs[i].fullyQualifiedName());
returnTasks[i] = new TestNGTask(taskDefs[i], testClassLoader, cliArgs);
}
return returnTasks;
}

private List<String> testMethods(Selector[] selectors, String testClass) {
ArrayList<String> testMethods = new ArrayList<String>();
for (int i = 0; i < selectors.length; i += 1) {
if (selectors[i] instanceof TestSelector) {
testMethods.add(testClass + "." + ((TestSelector) selectors[i]).testName());
} else if (selectors[i] instanceof SuiteSelector) {
return new ArrayList<String>();
}
}
return testMethods;
}

public String done() {
return null;
}
Expand Down

0 comments on commit 35fc951

Please sign in to comment.