Файли
openidea/src/features/run/testing/export.test.ts
T
Mansur Akhmadiтаmansur-akhmadi 77503e5fee feat(run): Export Test Results to HTML or XML from the test results toolbar (#4413)
The test results toolbar and Find Action get Export Test Results…: a dialog with HTML or XML format, the output file (default "<working directory>/Test Results - <configuration>.html") and an option to open the HTML report in the browser. HTML is a self-contained report (summary, suites, per-test status, duration, output and location, no scripts); XML uses the <testrun> format with count, suite, test, output and expected/actual diff. An existing file is replaced only after confirmation. Both entry points are disabled while tests run and when there are no results.

Closes #4412

Co-authored-by: mansur-akhmadi <[email protected]>
2026-11-19 17:57:29 +08:00

153 рядки
7.0 KiB
TypeScript

// @vitest-environment node
import { describe, expect, it } from "vitest";
import { exportTestResults, testResultsHtml, testResultsXml } from "./export";
import { TestResults } from "./results";
const info = { name: "go test ./calc", cwd: "/work/app", date: new Date(2026, 10, 19, 5, 40) };
function results() {
const r = new TestResults();
r.apply([
{ type: "result", path: ["calc", "TestAdd"], status: "passed", durationMs: 3 },
{ type: "result", path: ["calc", "TestSub"], status: "failed", durationMs: 12 },
{ type: "output", path: ["calc", "TestSub"], line: "expected: <1> but was: <2>" },
{ type: "location", path: ["calc", "TestSub"], location: { path: "calc/sub_test.go", line: 9, resolve: "path" } },
{ type: "result", path: ["calc", "TestDiv"], status: "skipped" },
{ type: "location", path: ["calc", "TestDiv"], location: { path: "div_test.go", line: 4, resolve: "name" } },
]);
r.finish(1);
return r;
}
describe("testResultsXml", () => {
it("writes IDEA's testrun format: counts, suites, tests, statuses, durations, output and the diff", () => {
expect(testResultsXml(results(), info)).toBe(
[
'<?xml version="1.0" encoding="UTF-8"?>',
`<testrun duration="15" footerText="Generated by OpenIDEA on ${info.date.toLocaleString()}" name="go test ./calc">`,
' <count name="total" value="3"/>',
' <count name="passed" value="1"/>',
' <count name="failed" value="1"/>',
' <count name="ignored" value="1"/>',
' <suite name="calc" status="failed" duration="15">',
' <test name="TestAdd" status="passed" duration="3"/>',
' <test locationUrl="file:///work/app/calc/sub_test.go:9" name="TestSub" status="failed" duration="12">',
' <diff actual="2" expected="1"/>',
' <output type="stdout">expected: &lt;1&gt; but was: &lt;2&gt;</output>',
" </test>",
// A file name to look up in the project is no location to link to.
' <test name="TestDiv" status="ignored"/>',
" </suite>",
"</testrun>",
"",
].join("\n"),
);
});
it("prefers the runner's total duration and keeps absolute and Windows locations as they are", () => {
const r = new TestResults();
r.apply([
{ type: "result", path: ["A"], status: "passed" },
{ type: "location", path: ["A"], location: { path: "C:\\src\\a_test.go", line: 2, resolve: "path" } },
{ type: "result", path: ["B"], status: "passed" },
{ type: "location", path: ["B"], location: { path: "/abs/b_test.go", resolve: "path" } },
{ type: "summary", durationMs: 1500 },
]);
r.finish(0);
const xml = testResultsXml(r, info);
expect(xml).toContain('<testrun duration="1500"');
expect(xml).toContain('locationUrl="file://C:/src/a_test.go:2"');
expect(xml).toContain('locationUrl="file:///abs/b_test.go"');
});
it("escapes markup, quotes and line breaks, and drops characters XML can't hold", () => {
const r = new TestResults();
r.apply([
{ type: "result", path: ['Test "quotes" & <tags>'], status: "failed" },
{ type: "output", path: ['Test "quotes" & <tags>'], line: "bell\u0007 and \u001b[31mred</output>" },
{ type: "output", path: ['Test "quotes" & <tags>'], line: 'expected: <"a\tb"> but was: <x & y>' },
]);
r.finish(1);
const xml = testResultsXml(r, { ...info, name: "it's <all>" });
expect(xml).toContain('name="it\'s &lt;all&gt;"');
expect(xml).toContain('<test name="Test &quot;quotes&quot; &amp; &lt;tags&gt;" status="failed">');
expect(xml).toContain('<diff actual="x &amp; y" expected="&quot;a&#9;b&quot;"/>');
expect(xml).toContain("bell and [31mred&lt;/output&gt;\nexpected:");
for (const c of ["\u0000", "\u0007", "\u001b"]) expect(xml).not.toContain(c);
});
it("rounds durations to whole milliseconds", () => {
const r = new TestResults();
r.apply([
{ type: "result", path: ["S", "a"], status: "passed", durationMs: 0.1 },
{ type: "result", path: ["S", "b"], status: "passed", durationMs: 1.2 },
]);
r.finish(0);
const xml = testResultsXml(r, info);
expect(xml).toContain('<suite name="S" status="passed" duration="1">');
expect(xml).toContain('<test name="a" status="passed" duration="0"/>');
expect(xml).toContain('<test name="b" status="passed" duration="1"/>');
});
it("writes an empty run, and marks tests the run never finished as terminated", () => {
expect(testResultsXml(new TestResults(), info)).toContain('<count name="total" value="0"/>\n</testrun>');
const r = new TestResults();
r.apply([{ type: "start", path: ["Slow"] }]);
expect(testResultsXml(r, info)).toContain('<test name="Slow" status="terminated"/>');
});
});
describe("testResultsHtml", () => {
it("is a standalone page with the summary, the tree, failures' output and source locations", () => {
const html = testResultsHtml(results(), info);
expect(html.startsWith("<!DOCTYPE html>")).toBe(true);
expect(html).toContain("<title>Test Results - go test ./calc</title>");
expect(html).toContain(
'<p class="summary"><b>3</b> tests · <b>1</b> passed · <b>1</b> failed · <b>1</b> ignored · 15 ms</p>',
);
expect(html).toContain(
'<details open class="failed"><summary><span class="status">Failed</span><span class="name">calc</span>',
);
expect(html).toContain(
'<span class="name">TestSub</span><span class="location">sub_test.go:9</span><span class="duration">12 ms</span>',
);
expect(html).toContain("<pre>expected: &lt;1&gt; but was: &lt;2&gt;</pre>");
expect(html).toContain(
'<div class="test ignored"><span class="status">Ignored</span><span class="name">TestDiv</span><span class="location">div_test.go:4</span></div>',
);
// Nothing loads from outside the file.
expect(html).not.toMatch(/<(script|link)\b/);
});
it("escapes test names and output", () => {
const r = new TestResults();
r.apply([
{ type: "result", path: ["<script>alert(1)</script>"], status: "failed" },
{ type: "output", path: ["<script>alert(1)</script>"], line: "<img src=x onerror=alert(1)>" },
]);
r.finish(1);
const html = testResultsHtml(r, { ...info, name: "<b>run</b>" });
expect(html).not.toContain("<script>alert");
expect(html).not.toContain("<img");
expect(html).toContain("<title>Test Results - &lt;b&gt;run&lt;/b&gt;</title>");
expect(html).toContain("&lt;script&gt;alert(1)&lt;/script&gt;");
});
it("leaves out ignored and duration from the summary when there are none", () => {
const r = new TestResults();
r.apply([{ type: "result", path: ["A"], status: "passed" }]);
r.finish(0);
expect(testResultsHtml(r, info)).toContain(
'<p class="summary"><b>1</b> tests · <b>1</b> passed · <b>0</b> failed</p>',
);
});
});
describe("exportTestResults", () => {
it("picks the writer by format", () => {
const r = results();
expect(exportTestResults("html", r, info)).toBe(testResultsHtml(r, info));
expect(exportTestResults("xml", r, info)).toBe(testResultsXml(r, info));
});
});