Skip to content

Commit

Permalink
feat: add trace node to failure and error
Browse files Browse the repository at this point in the history
  • Loading branch information
b4nst committed Nov 27, 2023
1 parent a6f951f commit 3015498
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 4 deletions.
26 changes: 24 additions & 2 deletions src/collections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,16 @@ pub struct TestCase {
pub enum TestResult {
Success,
Skipped,
Error { type_: String, message: String },
Failure { type_: String, message: String },
Error {
type_: String,
message: String,
cause: Option<String>,
},
Failure {
type_: String,
message: String,
cause: Option<String>,
},
}

impl TestCase {
Expand Down Expand Up @@ -202,6 +210,7 @@ impl TestCase {
result: TestResult::Error {
type_: type_.into(),
message: message.into(),
cause: None,
},
classname: None,
filepath: None,
Expand All @@ -225,6 +234,7 @@ impl TestCase {
result: TestResult::Failure {
type_: type_.into(),
message: message.into(),
cause: None,
},
classname: None,
filepath: None,
Expand Down Expand Up @@ -297,6 +307,18 @@ impl TestCaseBuilder {
self
}

/// Set the `result.trace` for the `TestCase`
///
/// It has no effect on successful `TestCase`s.
pub fn set_trace(&mut self, trace: &str) -> &mut Self {
match self.testcase.result {
TestResult::Error { ref mut cause, .. } => *cause = Some(trace.to_owned()),
TestResult::Failure { ref mut cause, .. } => *cause = Some(trace.to_owned()),
_ => {}
}
self
}

/// Creates a new TestCaseBuilder for an erroneous `TestCase`
///
/// An erroneous `TestCase` is one that encountered an unexpected error condition.
Expand Down
65 changes: 65 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,71 @@ mod tests {
<system-err><![CDATA[Another system error message]]></system-err>\
</testcase>\
</testsuite>\
</testsuites>",
);
}

#[test]
fn test_cases_with_trace() {
let timestamp = datetime!(1970-01-01 01:01 UTC);

let test_success = TestCaseBuilder::success("good test", Duration::milliseconds(15001))
.set_classname("MyClass")
.set_filepath("./foo.rs")
.set_trace("Some trace message") // This should be ignored
.build();
let test_error = TestCaseBuilder::error(
"error test",
Duration::seconds(5),
"git error",
"unable to fetch",
)
.set_trace("Some error trace")
.build();
let test_failure = TestCaseBuilder::failure(
"failure test",
Duration::seconds(10),
"assert_eq",
"not equal",
)
.set_trace("Some failure trace")
.build();

let ts1 = TestSuiteBuilder::new("ts1")
.set_timestamp(timestamp)
.build();
let ts2 = TestSuiteBuilder::new("ts2")
.set_timestamp(timestamp)
.add_testcase(test_success)
.add_testcase(test_error)
.add_testcase(test_failure)
.build();

let r = ReportBuilder::new()
.add_testsuite(ts1)
.add_testsuite(ts2)
.build();

let mut out: Vec<u8> = Vec::new();

r.write_xml(&mut out).unwrap();

// language=xml
assert_eq!(
String::from_utf8(out).unwrap(),
"\
<?xml version=\"1.0\" encoding=\"utf-8\"?>\
<testsuites>\
<testsuite id=\"0\" name=\"ts1\" package=\"testsuite/ts1\" tests=\"0\" errors=\"0\" failures=\"0\" hostname=\"localhost\" timestamp=\"1970-01-01T01:01:00Z\" time=\"0\"/>\
<testsuite id=\"1\" name=\"ts2\" package=\"testsuite/ts2\" tests=\"3\" errors=\"1\" failures=\"1\" hostname=\"localhost\" timestamp=\"1970-01-01T01:01:00Z\" time=\"30.001\">\
<testcase name=\"good test\" time=\"15.001\" classname=\"MyClass\" file=\"./foo.rs\"/>\
<testcase name=\"error test\" time=\"5\">\
<error type=\"git error\" message=\"unable to fetch\"><![CDATA[Some error trace]]></error>\
</testcase>\
<testcase name=\"failure test\" time=\"10\">\
<failure type=\"assert_eq\" message=\"not equal\"><![CDATA[Some failure trace]]></failure>\
</testcase>\
</testsuite>\
</testsuites>",
);
}
Expand Down
30 changes: 28 additions & 2 deletions src/reports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,23 +123,49 @@ impl TestCase {
TestResult::Error {
ref type_,
ref message,
ref cause,
} => w
.create_element("error")
.with_attributes([
("type", type_.as_str()),
("message", message.as_str()),
])
.write_empty(),
.write_empty_or_inner(
|_| cause.is_none(),
|w| {
w.write_opt(cause.as_ref(), |w, cause| {
let data = BytesCData::new(cause.as_str());
w.write_event(Event::CData(BytesCData::new(
String::from_utf8_lossy(&data),
)))
.map(|_| w)
})
.map(drop)
},
),
TestResult::Failure {
ref type_,
ref message,
ref cause,
} => w
.create_element("failure")
.with_attributes([
("type", type_.as_str()),
("message", message.as_str()),
])
.write_empty(),
.write_empty_or_inner(
|_| cause.is_none(),
|w| {
w.write_opt(cause.as_ref(), |w, cause| {
let data = BytesCData::new(cause.as_str());
w.write_event(Event::CData(BytesCData::new(
String::from_utf8_lossy(&data),
)))
.map(|_| w)
})
.map(drop)
},
),
TestResult::Skipped => w.create_element("skipped").write_empty(),
}?
.write_opt(self.system_out.as_ref(), |w, out| {
Expand Down

0 comments on commit 3015498

Please sign in to comment.