Skip to content

Commit

Permalink
Merge pull request #23 from kaplanelad/master
Browse files Browse the repository at this point in the history
add support for `file` attribute on `testcase`
  • Loading branch information
bachp authored Apr 27, 2022
2 parents 153286b + d32fb1d commit ed34e2c
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 16 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Generate JUnit compatible XML reports in Rust.
// Create a successful test case
let test_success = TestCaseBuilder::success("good test", Duration::seconds(15))
.set_classname("MyClass")
.set_filepath("MyFilePath")
.build();

// Create a test case that encountered an unexpected error condition
Expand Down
16 changes: 16 additions & 0 deletions src/collections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ pub struct TestCase {
pub time: Duration,
pub result: TestResult,
pub classname: Option<String>,
pub filepath: Option<String>,
pub system_out: Option<String>,
pub system_err: Option<String>,
}
Expand All @@ -160,6 +161,7 @@ impl TestCase {
time,
result: TestResult::Success,
classname: None,
filepath: None,
system_out: None,
system_err: None,
}
Expand All @@ -170,6 +172,11 @@ impl TestCase {
self.classname = Some(classname.to_owned());
}

/// Set the `file` for the `TestCase`
pub fn set_filepath(&mut self, filepath: &str) {
self.filepath = Some(filepath.to_owned());
}

/// Set the `system_out` for the `TestCase`
pub fn set_system_out(&mut self, system_out: &str) {
self.system_out = Some(system_out.to_owned());
Expand Down Expand Up @@ -197,6 +204,7 @@ impl TestCase {
message: message.into(),
},
classname: None,
filepath: None,
system_out: None,
system_err: None,
}
Expand All @@ -219,6 +227,7 @@ impl TestCase {
message: message.into(),
},
classname: None,
filepath: None,
system_out: None,
system_err: None,
}
Expand All @@ -238,6 +247,7 @@ impl TestCase {
time: Duration::ZERO,
result: TestResult::Skipped,
classname: None,
filepath: None,
system_out: None,
system_err: None,
}
Expand Down Expand Up @@ -269,6 +279,12 @@ impl TestCaseBuilder {
self
}

/// Set the `file` for the `TestCase`
pub fn set_filepath(&mut self, filepath: &str) -> &mut Self {
self.testcase.filepath = Some(filepath.to_owned());
self
}

/// Set the `system_out` for the `TestCase`
pub fn set_system_out(&mut self, system_out: &str) -> &mut Self {
self.testcase.system_out = Some(system_out.to_owned());
Expand Down
8 changes: 5 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
/// Duration::seconds(10),
/// "assert_eq",
/// "not equal",
/// ).set_classname("classname")
/// ).set_classname("classname").set_filepath("./foo.rs")
/// .build();
///
/// let ts1 = TestSuiteBuilder::new("ts1").set_timestamp(timestamp).build();
Expand Down Expand Up @@ -237,6 +237,7 @@ mod tests {

let test_success = TestCaseBuilder::success("good test", Duration::milliseconds(15001))
.set_classname("MyClass")
.set_filepath("./foo.rs")
.build();
let test_error = TestCaseBuilder::error(
"error test",
Expand Down Expand Up @@ -278,7 +279,7 @@ mod tests {
<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\" classname=\"MyClass\" time=\"15.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\" />
</testcase>
Expand All @@ -296,6 +297,7 @@ mod tests {

let test_success = TestCaseBuilder::success("good test", Duration::milliseconds(15001))
.set_classname("MyClass")
.set_filepath("./foo.rs")
.set_system_out("Some sysout message")
.build();
let test_error = TestCaseBuilder::error(
Expand Down Expand Up @@ -341,7 +343,7 @@ mod tests {
<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\" classname=\"MyClass\" time=\"15.001\">
<testcase name=\"good test\" time=\"15.001\" classname=\"MyClass\" file=\"./foo.rs\">
<system-out><![CDATA[Some sysout message]]></system-out>
</testcase>
<testcase name=\"error test\" time=\"5\">
Expand Down
24 changes: 12 additions & 12 deletions src/reports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,21 +78,21 @@ impl Report {
//ew.write(XmlEvent::end_element())?;

for tc in &ts.testcases {
let time = format!("{}", tc.time.as_seconds_f64());
let mut testcase_element = XmlEvent::start_element("testcase")
.attr("name", &tc.name)
.attr("time", &time);

if let Some(classname) = &tc.classname {
ew.write(
XmlEvent::start_element("testcase")
.attr("name", &tc.name)
.attr("classname", classname)
.attr("time", &format!("{}", tc.time.as_seconds_f64())),
)?;
} else {
ew.write(
XmlEvent::start_element("testcase")
.attr("name", &tc.name)
.attr("time", &format!("{}", tc.time.as_seconds_f64())),
)?;
testcase_element = testcase_element.attr("classname", classname);
}

if let Some(filepath) = &tc.filepath {
testcase_element = testcase_element.attr("file", filepath);
}

ew.write(testcase_element)?;

match tc.result {
TestResult::Success => {
if let Some(system_out) = &tc.system_out {
Expand Down
1 change: 1 addition & 0 deletions tests/JUnit.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
<xs:attribute name="assertions" type="xs:string" use="optional"/>
<xs:attribute name="time" type="xs:string" use="optional"/>
<xs:attribute name="classname" type="xs:string" use="optional"/>
<xs:attribute name="file" type="xs:string" use="optional"/>
<xs:attribute name="status" type="xs:string" use="optional"/>
</xs:complexType>
</xs:element>
Expand Down
1 change: 1 addition & 0 deletions tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ fn reference_report() {

let test_success = TestCaseBuilder::success("test1", Duration::seconds(15))
.set_classname("MyClass")
.set_filepath("./foo.rs")
.build();
let test_error = TestCase::error(
"test3",
Expand Down
2 changes: 1 addition & 1 deletion tests/reference.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<testsuites>
<testsuite id="0" name="ts1" package="testsuite/ts1" tests="4" errors="1" failures="1" hostname="localhost" timestamp="2018-04-21T12:02:00Z" time="30">
<testcase name="test1" classname="MyClass" time="15" />
<testcase name="test1" time="15" classname="MyClass" file="./foo.rs" />
<testcase name="test2" time="10">
<failure type="assert_eq" message="What was not true" />
</testcase>
Expand Down

0 comments on commit ed34e2c

Please sign in to comment.