Skip to content

Commit c1342f4

Browse files
terraform test: Include test case execution time in JUnit XML
Our currently-experimental JUnit XML output didn't previously include the "time" attribute for test runs at all, but from testing with common JUnit XML consumers we've learned that populating this is effectively mandatory because otherwise software just assumes that it's zero, producing a misleading result. We'll now populate it based on the test runner's measure of the execution time, assuming that the test runner actually measured it. If not, we'll leave it omitted and thus get the same behavior as before, which in practice means that most software will treat it as zero anyway. Because JUnit XML output is still experimental with the goal of getting feedback on exactly how we map the Terraform testing model onto that Java-oriented format, this is intentionally not supported for remote test runs in Terraform Cloud yet. It isn't really practical for Terraform Cloud to participate in short-lived Terraform CLI experiments, because Terraform Cloud has a different release schedule than Terraform CLI does. However, if this experiment is successful then we will eventually need to find a way to plumb this additional information through the JSON logs and ensure that it arrives in the locally-generated JUnit XML output.
1 parent 397ea6d commit c1342f4

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

internal/command/views/test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,19 @@ func junitXMLTestReport(suite *moduletest.Suite) ([]byte, error) {
886886
Failure *WithMessage `xml:"failure,omitempty"`
887887
Error *WithMessage `xml:"error,omitempty"`
888888
Stderr *WithMessage `xml:"system-err,omitempty"`
889+
890+
// RunTime is the time spent executing the run associated
891+
// with this test case, in seconds with the fractional component
892+
// representing partial seconds.
893+
//
894+
// We assume here that it's not practically possible for an
895+
// execution to take literally zero fractional seconds at
896+
// the accuracy we're using here (nanoseconds converted into
897+
// floating point seconds) and so use zero to represent
898+
// "not known", and thus omit that case. (In practice many
899+
// JUnit XML consumers treat the absense of this attribute
900+
// as zero anyway.)
901+
RunTime float64 `xml:"time,attr,omitempty"`
889902
}
890903

891904
testCase := TestCase{
@@ -898,6 +911,9 @@ func junitXMLTestReport(suite *moduletest.Suite) ([]byte, error) {
898911
// Java-shaped languages.
899912
Classname: file.Name,
900913
}
914+
if execMeta := run.ExecutionMeta; execMeta != nil {
915+
testCase.RunTime = execMeta.Duration.Seconds()
916+
}
901917
switch run.Status {
902918
case moduletest.Skip:
903919
testCase.Skipped = &WithMessage{

0 commit comments

Comments
 (0)