1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-22 23:34:47 -05:00
denoland-deno/cli/tools/test/reporters/junit.rs
Yusuke Tanaka 64e8c36805
fix(cli): output more detailed information for steps when using JUnit reporter (#22797)
This patch gets JUnit reporter to output more detailed information for
test steps (subtests).

## Issue with previous implementation

In the previous implementation, the test hierarchy was represented using
several XML tags like the following:

- `<testsuites>` corresponds to the entire test (one execution of `deno
test` has exactly one `<testsuites>` tag)
- `<testsuite>` corresponds to one file, such as `main_test.ts`
- `<testcase>` corresponds to one `Deno.test(...)`
- `<property>` corresponds to one `t.step(...)`

This structure describes the test layers but one problem is that
`<property>` tag is used for any use cases so some tools that can ingest
a JUnit XML file might not be able to interpret `<property>` as
subtests.

## How other tools address it

Some of the testing frameworks in the ecosystem address this issue by
fitting subtests into the `<testcase>` layer. For instance, take a look
at the following Go test file:

```go
package main_test

import "testing"

func TestMain(t *testing.T) {
  t.Run("child 1", func(t *testing.T) {
    // OK
  })

  t.Run("child 2", func(t *testing.T) {
    // Error
    t.Fatal("error")
  })
}
```

Running [gotestsum], we can get the output like this:

```xml
<?xml version="1.0" encoding="UTF-8"?>
<testsuites tests="3" failures="2" errors="0" time="1.013694">
	<testsuite tests="3" failures="2" time="0.510000" name="example/gosumtest" timestamp="2024-03-11T12:26:39+09:00">
		<properties>
			<property name="go.version" value="go1.22.1 darwin/arm64"></property>
		</properties>
		<testcase classname="example/gosumtest" name="TestMain/child_2" time="0.000000">
			<failure message="Failed" type="">=== RUN   TestMain/child_2&#xA;    main_test.go:12: error&#xA;--- FAIL: TestMain/child_2 (0.00s)&#xA;</failure>
		</testcase>
		<testcase classname="example/gosumtest" name="TestMain" time="0.000000">
			<failure message="Failed" type="">=== RUN   TestMain&#xA;--- FAIL: TestMain (0.00s)&#xA;</failure>
		</testcase>
		<testcase classname="example/gosumtest" name="TestMain/child_1" time="0.000000"></testcase>
	</testsuite>
</testsuites>
``` 

This output shows that nested test cases are squashed into the
`<testcase>` layer by treating them as the same layer as their parent,
`TestMain`. We can still distinguish nested ones by their `name`
attributes that look like `TestMain/<subtest_name>`.

As described in #22795, [vitest] solves the issue in the same way as
[gotestsum].

One downside of this would be that one test failure that happens in a
nested test case will end up being counted multiple times, because not
only the subtest but also its wrapping container(s) are considered to be
failures. In fact, in the [gotestsum] output above, `TestMain/child_2`
failed (which is totally expected) while its parent, `TestMain`, was
also counted as failure. As
https://github.com/denoland/deno/pull/20273#discussion_r1307558757
pointed out, there is a test runner that offers flexibility to prevent
this, but I personally don't think the "duplicate failure count" issue
is a big deal.

## How to fix the issue in this patch

This patch fixes the issue with the same approach as [gotestsum] and
[vitest].
More specifically, nested test cases are put into the `<testcase>` level
and their names are now represented as squashed test names concatenated
by `>` (e.g. `parent 2 > child 1 > grandchild 1`). This change also
allows us to put a detailed error message as `<failure>` tag within the
`<testcase>` tag, which should be handled nicely by third-party tools
supporting JUnit XML.

## Extra fix

Also, file paths embedded into XML outputs are changed from absolute
path to relative path, which is helpful when running the test suites in
several different environments like CI.

Resolves #22795

[gotestsum]: https://github.com/gotestyourself/gotestsum
[vitest]: https://vitest.dev/

---------

Co-authored-by: Bartek Iwańczuk <biwanczuk@gmail.com>
2024-03-26 00:08:46 +09:00

425 lines
12 KiB
Rust

// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
use std::collections::VecDeque;
use std::path::PathBuf;
use super::fmt::to_relative_path_or_remote_url;
use super::*;
pub struct JunitTestReporter {
cwd: Url,
output_path: String,
// Stores TestCases (i.e. Tests) by the Test ID
cases: IndexMap<usize, quick_junit::TestCase>,
// Stores nodes representing test cases in such a way that can be traversed
// from child to parent to build the full test name that reflects the test
// hierarchy.
test_name_tree: TestNameTree,
}
impl JunitTestReporter {
pub fn new(cwd: Url, output_path: String) -> Self {
Self {
cwd,
output_path,
cases: IndexMap::new(),
test_name_tree: TestNameTree::new(),
}
}
fn convert_status(status: &TestResult) -> quick_junit::TestCaseStatus {
match status {
TestResult::Ok => quick_junit::TestCaseStatus::success(),
TestResult::Ignored => quick_junit::TestCaseStatus::skipped(),
TestResult::Failed(failure) => quick_junit::TestCaseStatus::NonSuccess {
kind: quick_junit::NonSuccessKind::Failure,
message: Some(failure.overview()),
ty: None,
description: Some(failure.detail()),
reruns: vec![],
},
TestResult::Cancelled => quick_junit::TestCaseStatus::NonSuccess {
kind: quick_junit::NonSuccessKind::Error,
message: Some("Cancelled".to_string()),
ty: None,
description: None,
reruns: vec![],
},
}
}
fn convert_step_status(
status: &TestStepResult,
) -> quick_junit::TestCaseStatus {
match status {
TestStepResult::Ok => quick_junit::TestCaseStatus::success(),
TestStepResult::Ignored => quick_junit::TestCaseStatus::skipped(),
TestStepResult::Failed(failure) => {
quick_junit::TestCaseStatus::NonSuccess {
kind: quick_junit::NonSuccessKind::Failure,
message: Some(failure.overview()),
ty: None,
description: Some(failure.detail()),
reruns: vec![],
}
}
}
}
}
impl TestReporter for JunitTestReporter {
fn report_register(&mut self, description: &TestDescription) {
let mut case = quick_junit::TestCase::new(
description.name.clone(),
quick_junit::TestCaseStatus::skipped(),
);
case.classname = Some(to_relative_path_or_remote_url(
&self.cwd,
&description.location.file_name,
));
case.extra.insert(
String::from("line"),
description.location.line_number.to_string(),
);
case.extra.insert(
String::from("col"),
description.location.column_number.to_string(),
);
self.cases.insert(description.id, case);
self.test_name_tree.add_node(description.clone().into());
}
fn report_plan(&mut self, _plan: &TestPlan) {}
fn report_wait(&mut self, _description: &TestDescription) {}
fn report_output(&mut self, _output: &[u8]) {
/*
TODO(skycoop): Right now I can't include stdout/stderr in the report because
we have a global pair of output streams that don't differentiate between the
output of different tests. This is a nice to have feature, so we can come
back to it later
*/
}
fn report_result(
&mut self,
description: &TestDescription,
result: &TestResult,
elapsed: u64,
) {
if let Some(case) = self.cases.get_mut(&description.id) {
case.status = Self::convert_status(result);
case.set_time(Duration::from_millis(elapsed));
}
}
fn report_uncaught_error(&mut self, _origin: &str, _error: Box<JsError>) {}
fn report_step_register(&mut self, description: &TestStepDescription) {
self.test_name_tree.add_node(description.clone().into());
let test_case_name =
self.test_name_tree.construct_full_test_name(description.id);
let mut case = quick_junit::TestCase::new(
test_case_name,
quick_junit::TestCaseStatus::skipped(),
);
case.classname = Some(to_relative_path_or_remote_url(
&self.cwd,
&description.location.file_name,
));
case.extra.insert(
String::from("line"),
description.location.line_number.to_string(),
);
case.extra.insert(
String::from("col"),
description.location.column_number.to_string(),
);
self.cases.insert(description.id, case);
}
fn report_step_wait(&mut self, _description: &TestStepDescription) {}
fn report_step_result(
&mut self,
description: &TestStepDescription,
result: &TestStepResult,
elapsed: u64,
_tests: &IndexMap<usize, TestDescription>,
_test_steps: &IndexMap<usize, TestStepDescription>,
) {
if let Some(case) = self.cases.get_mut(&description.id) {
case.status = Self::convert_step_status(result);
case.set_time(Duration::from_millis(elapsed));
}
}
fn report_summary(
&mut self,
_elapsed: &Duration,
_tests: &IndexMap<usize, TestDescription>,
_test_steps: &IndexMap<usize, TestStepDescription>,
) {
}
fn report_sigint(
&mut self,
tests_pending: &HashSet<usize>,
tests: &IndexMap<usize, TestDescription>,
_test_steps: &IndexMap<usize, TestStepDescription>,
) {
for id in tests_pending {
if let Some(description) = tests.get(id) {
self.report_result(description, &TestResult::Cancelled, 0)
}
}
}
fn report_completed(&mut self) {
// TODO(mmastrac): This reporter does not handle stdout/stderr yet, and when we do, we may need to redirect
// pre-and-post-test output somewhere.
}
fn flush_report(
&mut self,
elapsed: &Duration,
tests: &IndexMap<usize, TestDescription>,
test_steps: &IndexMap<usize, TestStepDescription>,
) -> anyhow::Result<()> {
let mut suites: IndexMap<String, quick_junit::TestSuite> = IndexMap::new();
for (id, case) in &self.cases {
let abs_filename = match (tests.get(id), test_steps.get(id)) {
(Some(test), _) => &test.location.file_name,
(_, Some(step)) => &step.location.file_name,
(None, None) => {
unreachable!("Unknown test ID '{id}' provided");
}
};
let filename = to_relative_path_or_remote_url(&self.cwd, abs_filename);
suites
.entry(filename.clone())
.and_modify(|s| {
s.add_test_case(case.clone());
})
.or_insert_with(|| {
let mut suite = quick_junit::TestSuite::new(filename);
suite.add_test_case(case.clone());
suite
});
}
let mut report = quick_junit::Report::new("deno test");
report
.set_time(*elapsed)
.add_test_suites(suites.into_values());
if self.output_path == "-" {
report
.serialize(std::io::stdout())
.with_context(|| "Failed to write JUnit report to stdout")?;
} else {
let file =
crate::util::fs::create_file(&PathBuf::from(&self.output_path))
.context("Failed to open JUnit report file.")?;
report.serialize(file).with_context(|| {
format!("Failed to write JUnit report to {}", self.output_path)
})?;
}
Ok(())
}
}
#[derive(Debug, Default)]
struct TestNameTree(IndexMap<usize, TestNameTreeNode>);
impl TestNameTree {
fn new() -> Self {
// Pre-allocate some space to avoid excessive reallocations.
Self(IndexMap::with_capacity(256))
}
fn add_node(&mut self, node: TestNameTreeNode) {
self.0.insert(node.id, node);
}
/// Constructs the full test name by traversing the tree from the specified
/// node as a child to its parent nodes.
/// If the provided ID is not found in the tree, or the tree is broken (e.g.
/// a child node refers to a parent node that doesn't exist), this method
/// just panics.
fn construct_full_test_name(&self, id: usize) -> String {
let mut current_id = Some(id);
let mut name_pieces = VecDeque::new();
loop {
let Some(id) = current_id else {
break;
};
let Some(node) = self.0.get(&id) else {
// The ID specified as a parent node by the child node should exist in
// the tree, but it doesn't. In this case we give up constructing the
// full test name.
unreachable!("Unregistered test ID '{id}' provided");
};
name_pieces.push_front(node.test_name.as_str());
current_id = node.parent_id;
}
if name_pieces.is_empty() {
unreachable!("Unregistered test ID '{id}' provided");
}
let v: Vec<_> = name_pieces.into();
v.join(" > ")
}
}
#[derive(Debug)]
struct TestNameTreeNode {
id: usize,
parent_id: Option<usize>,
test_name: String,
}
impl From<TestDescription> for TestNameTreeNode {
fn from(description: TestDescription) -> Self {
Self {
id: description.id,
parent_id: None,
test_name: description.name,
}
}
}
impl From<TestStepDescription> for TestNameTreeNode {
fn from(description: TestStepDescription) -> Self {
Self {
id: description.id,
parent_id: Some(description.parent_id),
test_name: description.name,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn construct_full_test_name_one_node() {
let mut tree = TestNameTree::new();
tree.add_node(TestNameTreeNode {
id: 0,
parent_id: None,
test_name: "root".to_string(),
});
assert_eq!(tree.construct_full_test_name(0), "root".to_string());
}
#[test]
fn construct_full_test_name_two_level_hierarchy() {
let mut tree = TestNameTree::new();
tree.add_node(TestNameTreeNode {
id: 0,
parent_id: None,
test_name: "root".to_string(),
});
tree.add_node(TestNameTreeNode {
id: 1,
parent_id: Some(0),
test_name: "child".to_string(),
});
assert_eq!(tree.construct_full_test_name(0), "root".to_string());
assert_eq!(tree.construct_full_test_name(1), "root > child".to_string());
}
#[test]
fn construct_full_test_name_three_level_hierarchy() {
let mut tree = TestNameTree::new();
tree.add_node(TestNameTreeNode {
id: 0,
parent_id: None,
test_name: "root".to_string(),
});
tree.add_node(TestNameTreeNode {
id: 1,
parent_id: Some(0),
test_name: "child".to_string(),
});
tree.add_node(TestNameTreeNode {
id: 2,
parent_id: Some(1),
test_name: "grandchild".to_string(),
});
assert_eq!(tree.construct_full_test_name(0), "root".to_string());
assert_eq!(tree.construct_full_test_name(1), "root > child".to_string());
assert_eq!(
tree.construct_full_test_name(2),
"root > child > grandchild".to_string()
);
}
#[test]
fn construct_full_test_name_one_root_two_chains() {
// 0
// / \
// 1 2
// / \
// 3 4
let mut tree = TestNameTree::new();
tree.add_node(TestNameTreeNode {
id: 0,
parent_id: None,
test_name: "root".to_string(),
});
tree.add_node(TestNameTreeNode {
id: 1,
parent_id: Some(0),
test_name: "child 1".to_string(),
});
tree.add_node(TestNameTreeNode {
id: 2,
parent_id: Some(0),
test_name: "child 2".to_string(),
});
tree.add_node(TestNameTreeNode {
id: 3,
parent_id: Some(1),
test_name: "grandchild 1".to_string(),
});
tree.add_node(TestNameTreeNode {
id: 4,
parent_id: Some(1),
test_name: "grandchild 2".to_string(),
});
assert_eq!(tree.construct_full_test_name(0), "root".to_string());
assert_eq!(
tree.construct_full_test_name(1),
"root > child 1".to_string(),
);
assert_eq!(
tree.construct_full_test_name(2),
"root > child 2".to_string(),
);
assert_eq!(
tree.construct_full_test_name(3),
"root > child 1 > grandchild 1".to_string(),
);
assert_eq!(
tree.construct_full_test_name(4),
"root > child 1 > grandchild 2".to_string(),
);
}
}