diff --git a/java/junit-runner/build.py b/java/junit-runner/build.py index 64cd0e9b9..4c4157502 100644 --- a/java/junit-runner/build.py +++ b/java/junit-runner/build.py @@ -51,7 +51,8 @@ with tempfile.TemporaryDirectory() as scratch: "##teamcity[testFailed name='fails' className='sample.SampleTest' message='org.opentest4j.AssertionFailedError: it|'s wrong ==> expected: <1> but was: <2>'", "##teamcity[testIgnored name='disabled' className='sample.SampleTest'", "##teamcity[testIgnored name='aborted' className='sample.SampleTest']", - "##teamcity[testFailed name='parameterized' className='sample.SampleTest'", + "##teamcity[testStarted name='parameterized' className='sample.SampleTest' invocation='|[1|] 1']", + "##teamcity[testFailed name='parameterized' className='sample.SampleTest' invocation='|[2|] 2'", "##teamcity[testFinished name='nested' className='sample.SampleTest$Inner' duration=", "7 tests completed, 2 failed, 2 skipped", ] diff --git a/java/junit-runner/openidea-junit-runner.jar b/java/junit-runner/openidea-junit-runner.jar index 201d25952..0bed48656 100644 Binary files a/java/junit-runner/openidea-junit-runner.jar and b/java/junit-runner/openidea-junit-runner.jar differ diff --git a/java/junit-runner/src/ai/openidea/junit/PlatformRunner.java b/java/junit-runner/src/ai/openidea/junit/PlatformRunner.java index aaa4c8bd8..c126dbc64 100644 --- a/java/junit-runner/src/ai/openidea/junit/PlatformRunner.java +++ b/java/junit-runner/src/ai/openidea/junit/PlatformRunner.java @@ -153,18 +153,25 @@ final class PlatformRunner implements TestExecutionListener { for (Output o : outputs) o.flushLines(true); } - /** `name='method' className='a.B$Nested'`, the method and class of the test or its closest source. */ + /** + * `name='method' className='a.B$Nested'`, the method and class of the test or its closest source; + * an invocation of a parameterized, repeated or dynamic test adds `invocation='[1] a'`, its display name. + */ private String id(TestIdentifier id) { + // The outermost method of the chain: the test itself, or the method container of its invocations. + TestIdentifier method = null; for (TestIdentifier at = id; at != null; at = plan.getParent(at).orElse(null)) { TestSource source = at.getSource().orElse(null); - if (source instanceof MethodSource) { - MethodSource m = (MethodSource) source; - return Reporter.attr("name", m.getMethodName()) + Reporter.attr("className", m.getClassName()); - } - if (source instanceof ClassSource) + if (source instanceof MethodSource) method = at; + else if (method != null) break; + else if (source instanceof ClassSource) return Reporter.attr("name", id.getLegacyReportingName()) + Reporter.attr("className", ((ClassSource) source).getClassName()); } - return Reporter.attr("name", id.getLegacyReportingName()) + Reporter.attr("className", id.getDisplayName()); + if (method == null) + return Reporter.attr("name", id.getLegacyReportingName()) + Reporter.attr("className", id.getDisplayName()); + MethodSource m = (MethodSource) method.getSource().get(); + String own = Reporter.attr("name", m.getMethodName()) + Reporter.attr("className", m.getClassName()); + return method == id ? own : own + Reporter.attr("invocation", id.getDisplayName()); } private Optional className(TestIdentifier id) { diff --git a/src/features/run/gradleTests.gradle b/src/features/run/gradleTests.gradle index 5ffd74e21..6a4767a76 100644 --- a/src/features/run/gradleTests.gradle +++ b/src/features/run/gradleTests.gradle @@ -12,6 +12,17 @@ def tc = { String s -> .replace('[', '|[').replace(']', '|]') } +// `name='method(Type)[1]' className='a.B'`; an invocation of a parameterized, repeated or dynamic +// test (its parent is the method, not the class) adds `invocation='[1] a'`, its display name. +def id = { TestDescriptor test -> + def own = "name='${tc(test.name)}' className='${tc(test.className)}'" + def parent = test.parent + if (parent == null || parent.className != test.className || parent.name == test.className) return own + // `getDisplayName` is Gradle 4.10+. + def shown = test.hasProperty('displayName') ? test.displayName : test.name + "${own} invocation='${tc(shown)}'" +} + allprojects { each(tasks.withType(Test)) { Test task -> task.outputs.upToDateWhen { false } @@ -21,29 +32,29 @@ allprojects { void afterSuite(TestDescriptor suite, TestResult result) {} void beforeTest(TestDescriptor test) { - println "##teamcity[testStarted name='${tc(test.name)}' className='${tc(test.className)}']" + println "##teamcity[testStarted ${id(test)}]" } void afterTest(TestDescriptor test, TestResult result) { - def id = "name='${tc(test.name)}' className='${tc(test.className)}'" + def own = id(test) def type = result.resultType.toString() if (type == 'FAILURE') { def error = result.exception def trace = new StringWriter() error?.printStackTrace(new PrintWriter(trace)) - println "##teamcity[testFailed ${id} message='${tc(error?.toString())}' details='${tc(trace.toString())}']" + println "##teamcity[testFailed ${own} message='${tc(error?.toString())}' details='${tc(trace.toString())}']" } else if (type == 'SKIPPED') { - println "##teamcity[testIgnored ${id}]" + println "##teamcity[testIgnored ${own}]" } - println "##teamcity[testFinished ${id} duration='${result.endTime - result.startTime}']" + println "##teamcity[testFinished ${own} duration='${result.endTime - result.startTime}']" } }) task.addTestOutputListener(new TestOutputListener() { void onOutput(TestDescriptor test, TestOutputEvent output) { if (test.className == null) return // A class's own output (`@BeforeAll`, `@AfterAll`) carries no test name. - def name = test.composite ? '' : " name='${tc(test.name)}'" - println "##teamcity[testStdOut${name} className='${tc(test.className)}' out='${tc(output.message)}']" + def own = test.composite ? "className='${tc(test.className)}'" : id(test) + println "##teamcity[testStdOut ${own} out='${tc(output.message)}']" } }) } diff --git a/src/features/run/testing/jvm.ts b/src/features/run/testing/jvm.ts index 60b64bada..d7a498c7a 100644 --- a/src/features/run/testing/jvm.ts +++ b/src/features/run/testing/jvm.ts @@ -32,7 +32,9 @@ function frameLocation(line: string): TestLocation | null { /** * Test events of the init script's messages. Tests are `[class, method]`, named as in the XML reports - * (`add(int, int)[1]` → `add`); `outcomes` keeps each test's failure or skip until it finishes. + * (`add(int, int)` → `add`), and an invocation of a parameterized, repeated or dynamic test is + * `[class, method, "[1] a"]` (its `invocation`, the display name); `outcomes` keeps each test's + * failure or skip until it finishes. */ function initScriptEvents({ kind, attrs }: ServiceMessage, outcomes: Map): TestEvent[] { if (!attrs.className) return []; @@ -43,8 +45,9 @@ function initScriptEvents({ kind, attrs }: ServiceMessage, outcomes: Map fails() FAILED expect(r.hasResult(["demo.CalcTest", "other"])).toBe(false); }); + it("shows each invocation of a parameterized test under its method", () => { + const r = results( + jvmParser(), + `##teamcity[testStarted name='accepts' className='demo.ParamTest' invocation='|[1|] a'] +##teamcity[testFinished name='accepts' className='demo.ParamTest' invocation='|[1|] a' duration='2'] +##teamcity[testStarted name='accepts' className='demo.ParamTest' invocation='|[2|] bb'] +##teamcity[testStdOut name='accepts' className='demo.ParamTest' invocation='|[2|] bb' out='checking bb|n'] +##teamcity[testFailed name='accepts' className='demo.ParamTest' invocation='|[2|] bb' message='no' details='org.opentest4j.AssertionFailedError: no|n\tat app//demo.ParamTest.accepts(ParamTest.java:12)|n'] +##teamcity[testFinished name='accepts' className='demo.ParamTest' invocation='|[2|] bb' duration='1'] +##teamcity[testStarted name='accepts(String)|[3|]' className='demo.ParamTest' invocation='|[3|] c'] +##teamcity[testFinished name='accepts(String)|[3|]' className='demo.ParamTest' invocation='|[3|] c' duration='1'] +`, + ); + const method = leaf(r, "demo.ParamTest", "accepts")!; + expect(method.children.map((n) => n.name)).toEqual(["[1] a", "[2] bb", "[3] c"]); + expect(nodeStatus(method)).toBe("failed"); + expect(leaf(r, "demo.ParamTest", "accepts", "[1] a")).toMatchObject({ own: "passed", durationMs: 2, output: [] }); + const failed = leaf(r, "demo.ParamTest", "accepts", "[2] bb")!; + expect(failed.own).toBe("failed"); + expect(failed.output.slice(0, 2)).toEqual(["checking bb", "org.opentest4j.AssertionFailedError: no"]); + expect(failed.location).toMatchObject({ path: "demo/ParamTest.java", line: 12 }); + expect(leaf(r, "demo.ParamTest", "accepts", "[3] c")?.own).toBe("passed"); + expect(failedTestPaths(r)).toEqual([["demo.ParamTest", "accepts", "[2] bb"]]); + }); + it("gives a class's own output from the gradle init script to the class", () => { const r = results( jvmParser(), diff --git a/src/features/run/testing/rerun.test.ts b/src/features/run/testing/rerun.test.ts index 2b9890b94..bf5bb9da7 100644 --- a/src/features/run/testing/rerun.test.ts +++ b/src/features/run/testing/rerun.test.ts @@ -244,6 +244,15 @@ describe("rerunFailedConfig", () => { ]); }); + it("jvm: a failed invocation of a parameterized test reruns its method", async () => { + expect( + (await rerunFailedConfig(cfg("gradle", "./gradlew", ["test"]), [ + ["com.a.FooTest", "add", "[2] b"], + ["com.a.FooTest", "add", "[3] c"], + ]))!.args, + ).toEqual(["test", "--tests", "com.a.FooTest.add"]); + }); + it("maven: a rerun replaces the previous selection and lets modules without the tests pass", async () => { const narrowed = cfg("maven", "./mvnw", [ "test", diff --git a/src/features/run/testing/rerun.ts b/src/features/run/testing/rerun.ts index 1fa731be3..6e7313a43 100644 --- a/src/features/run/testing/rerun.ts +++ b/src/features/run/testing/rerun.ts @@ -153,9 +153,8 @@ function goArgs(base: string[], paths: string[][], file?: string): string[] | nu } function jvmArgs(config: RunConfig, paths: string[][]): string[] | null { - const tests = paths - .filter((p) => p.length >= 2) - .map((p) => ({ cls: p[0]!, method: p[p.length - 1]!.replace(/\(\)$/, "") })); + // `[class, method]`, or `[class, method, invocation]`: an invocation reruns its method. + const tests = paths.filter((p) => p.length >= 2).map((p) => ({ cls: p[0]!, method: p[1]!.replace(/\(.*\)$/, "") })); const classes = unique(paths.filter((p) => p.length === 1).map((p) => p[0]!)); if (!tests.length && !classes.length) return null; if (isJunitRunner(config)) {