From 298adbe636ed37aace2426fd5f8c6618988e4ef2 Mon Sep 17 00:00:00 2001 From: Li Haoyi Date: Wed, 23 Oct 2024 00:26:34 +0800 Subject: [PATCH] Fix TestNg test runner (#3813) Fixes https://github.com/com-lihaoyi/mill/issues/3798, supersedes https://github.com/com-lihaoyi/mill/pull/3799 Not sure how this ever worked, but it seems like it works now Converted the example test in https://github.com/com-lihaoyi/mill/pull/3799 to a unit test. --------- Co-authored-by: Tobias Roeser --- build.mill | 1 + .../testng/src/mill/testng/TestNGRunner.java | 17 +++--- .../demo/testng/src/foo/HelloTests.java | 14 +++++ .../demo/testng/src/foo/WorldTests.java | 13 +++++ .../testngGrouping/src/foo/HelloTests.java | 14 +++++ .../testngGrouping/src/foo/WorldTests.java | 13 +++++ .../test/src/mill/testng/TestNGTests.scala | 53 +++++++++++++------ 7 files changed, 98 insertions(+), 27 deletions(-) create mode 100644 contrib/testng/test/resources/demo/testng/src/foo/HelloTests.java create mode 100644 contrib/testng/test/resources/demo/testng/src/foo/WorldTests.java create mode 100644 contrib/testng/test/resources/demo/testngGrouping/src/foo/HelloTests.java create mode 100644 contrib/testng/test/resources/demo/testngGrouping/src/foo/WorldTests.java diff --git a/build.mill b/build.mill index e37d1626fb9..0be918242ef 100644 --- a/build.mill +++ b/build.mill @@ -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() ) diff --git a/contrib/testng/src/mill/testng/TestNGRunner.java b/contrib/testng/src/mill/testng/TestNGRunner.java index d577fe8606a..6a725cd84e2 100644 --- a/contrib/testng/src/mill/testng/TestNGRunner.java +++ b/contrib/testng/src/mill/testng/TestNGRunner.java @@ -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; } diff --git a/contrib/testng/test/resources/demo/testng/src/foo/HelloTests.java b/contrib/testng/test/resources/demo/testng/src/foo/HelloTests.java new file mode 100644 index 00000000000..0404fda5c07 --- /dev/null +++ b/contrib/testng/test/resources/demo/testng/src/foo/HelloTests.java @@ -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"); + } +} diff --git a/contrib/testng/test/resources/demo/testng/src/foo/WorldTests.java b/contrib/testng/test/resources/demo/testng/src/foo/WorldTests.java new file mode 100644 index 00000000000..b28be5508c0 --- /dev/null +++ b/contrib/testng/test/resources/demo/testng/src/foo/WorldTests.java @@ -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"); + } +} diff --git a/contrib/testng/test/resources/demo/testngGrouping/src/foo/HelloTests.java b/contrib/testng/test/resources/demo/testngGrouping/src/foo/HelloTests.java new file mode 100644 index 00000000000..0404fda5c07 --- /dev/null +++ b/contrib/testng/test/resources/demo/testngGrouping/src/foo/HelloTests.java @@ -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"); + } +} diff --git a/contrib/testng/test/resources/demo/testngGrouping/src/foo/WorldTests.java b/contrib/testng/test/resources/demo/testngGrouping/src/foo/WorldTests.java new file mode 100644 index 00000000000..b28be5508c0 --- /dev/null +++ b/contrib/testng/test/resources/demo/testngGrouping/src/foo/WorldTests.java @@ -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"); + } +} diff --git a/contrib/testng/test/src/mill/testng/TestNGTests.scala b/contrib/testng/test/src/mill/testng/TestNGTests.scala index a512a10da67..8a009d0cf65 100644 --- a/contrib/testng/test/src/mill/testng/TestNGTests.scala +++ b/contrib/testng/test/src/mill/testng/TestNGTests.scala @@ -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")) } } }