Skip to content

Commit

Permalink
Fix TestNg test runner (com-lihaoyi#3813)
Browse files Browse the repository at this point in the history
Fixes com-lihaoyi#3798, supersedes
com-lihaoyi#3799

Not sure how this ever worked, but it seems like it works now

Converted the example test in
com-lihaoyi#3799 to a unit test.

---------

Co-authored-by: Tobias Roeser <[email protected]>
  • Loading branch information
lihaoyi and lefou authored Oct 22, 2024
1 parent 9eb2cbc commit 298adbe
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 27 deletions.
1 change: 1 addition & 0 deletions build.mill
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,7 @@ object dist0 extends MillPublishJavaModule {
build.contrib.jmh.testDep(),
build.contrib.playlib.testDep(),
build.contrib.playlib.worker("2.8").testDep(),
build.contrib.testng.testDep(),
build.bsp.worker.testDep(),
build.testkit.testDep()
)
Expand Down
17 changes: 7 additions & 10 deletions contrib/testng/src/mill/testng/TestNGRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,14 @@ public TestNGRunner(String[] args, String[] remoteArgs, ClassLoader testClassLoa
}

public Task[] tasks(TaskDef[] taskDefs) {
CommandLineArgs cliArgs = new CommandLineArgs();
new JCommander(cliArgs).parse(args); // args is an output parameter of the constructor!
if(cliArgs.testClass == null){
String[] names = new String[taskDefs.length];
for(int i = 0; i < taskDefs.length; i += 1){
names[i] = taskDefs[i].fullyQualifiedName();
}
cliArgs.testClass = String.join(",", names);
Task[] returnTasks = new Task[taskDefs.length];
for(int i = 0; i < taskDefs.length; i += 1){
CommandLineArgs cliArgs = new CommandLineArgs();
new JCommander(cliArgs).parse(args); // args is an output parameter of the constructor!
cliArgs.testClass = taskDefs[i].fullyQualifiedName();
returnTasks[i] = new TestNGTask(taskDefs[i], testClassLoader, cliArgs);
}
if (taskDefs.length == 0) return new Task[]{};
else return new Task[]{new TestNGTask(taskDefs[0], testClassLoader, cliArgs)};
return returnTasks;
}

public String done() { return null; }
Expand Down
14 changes: 14 additions & 0 deletions contrib/testng/test/resources/demo/testng/src/foo/HelloTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package foo;

import static org.testng.Assert.assertTrue;
import org.testng.annotations.Test;

public class HelloTests {

@Test
public void hello() throws Exception {
System.out.println("Testing Hello");
Thread.sleep(1000);
System.out.println("Testing Hello Completed");
}
}
13 changes: 13 additions & 0 deletions contrib/testng/test/resources/demo/testng/src/foo/WorldTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package foo;

import static org.testng.Assert.assertTrue;
import org.testng.annotations.Test;

public class WorldTests {
@Test
public void world() throws Exception {
System.out.println("Testing World");
Thread.sleep(1000);
System.out.println("Testing World Completed");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package foo;

import static org.testng.Assert.assertTrue;
import org.testng.annotations.Test;

public class HelloTests {

@Test
public void hello() throws Exception {
System.out.println("Testing Hello");
Thread.sleep(1000);
System.out.println("Testing Hello Completed");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package foo;

import static org.testng.Assert.assertTrue;
import org.testng.annotations.Test;

public class WorldTests {
@Test
public void world() throws Exception {
System.out.println("Testing World");
Thread.sleep(1000);
System.out.println("Testing World Completed");
}
}
53 changes: 36 additions & 17 deletions contrib/testng/test/src/mill/testng/TestNGTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,46 @@ object TestNGTests extends TestSuite {
}
}

}
object testng extends JavaTests with TestModule.TestNg {
def ivyDeps = super.ivyDeps() ++ Agg(
ivy"org.testng:testng:7.10.2"
)
}

object testngGrouping extends JavaTests with TestModule.TestNg {
def ivyDeps = super.ivyDeps() ++ Agg(
ivy"org.testng:testng:7.10.2"
)
def testForkGrouping = discoveredTestClasses().grouped(1).toSeq
}
}
val resourcePath: os.Path = os.Path(sys.env("MILL_TEST_RESOURCE_DIR")) / "demo"

def tests: Tests = Tests {
test("TestNG") {
test("demo") - UnitTester(demo, resourcePath).scoped { eval =>
val Right(result) = eval.apply(demo.test.testFramework)
assert(
result.value == "mill.testng.TestNGFramework",
result.evalCount > 0
)
}
test("Test case lookup from inherited annotations") - UnitTester(demo, resourcePath).scoped {
eval =>
val Right(result) = eval.apply(demo.test.test())
val tres = result.value.asInstanceOf[(String, Seq[mill.testrunner.TestResult])]
assert(
tres._2.size == 8
)
}
test("demo") - UnitTester(demo, resourcePath).scoped { eval =>
val Right(result) = eval.apply(demo.test.testFramework)
assert(
result.value == "mill.testng.TestNGFramework",
result.evalCount > 0
)
}
test("Test case lookup from inherited annotations") - UnitTester(demo, resourcePath).scoped {
eval =>
val Right(result) = eval.apply(demo.test.test())
val tres = result.value
assert(tres._2.size == 8)
}
test("noGrouping") - UnitTester(demo, resourcePath).scoped {
eval =>
val Right(result) = eval.apply(demo.testng.test())
val tres = result.value._2
assert(tres.map(_.fullyQualifiedName).toSet == Set("foo.HelloTests", "foo.WorldTests"))
}
test("testForkGrouping") - UnitTester(demo, resourcePath).scoped {
eval =>
val Right(result) = eval.apply(demo.testngGrouping.test())
val tres = result.value._2
assert(tres.map(_.fullyQualifiedName).toSet == Set("foo.HelloTests", "foo.WorldTests"))
}
}
}

0 comments on commit 298adbe

Please sign in to comment.