k8s.io/kubernetes@v1.29.3/test/utils/junit/junit.go (about)

     1  /*
     2  Copyright 2017 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  // Package junit provides data structures to allow easy XML encoding
    18  // and decoding of JUnit test results.
    19  package junit
    20  
    21  import (
    22  	"encoding/xml"
    23  	"time"
    24  )
    25  
    26  // TestSuite is a top-level test suite containing test cases.
    27  type TestSuite struct {
    28  	XMLName xml.Name `xml:"testsuite"`
    29  
    30  	Name      string    `xml:"name,attr"`
    31  	Tests     int       `xml:"tests,attr"`
    32  	Disabled  int       `xml:"disabled,attr,omitempty"`
    33  	Errors    int       `xml:"errors,attr"`
    34  	Failures  int       `xml:"failures,attr"`
    35  	Skipped   int       `xml:"skipped,attr,omitempty"`
    36  	Time      float64   `xml:"time,attr"`
    37  	Timestamp time.Time `xml:"timestamp,attr"`
    38  	ID        int       `xml:"id,attr,omitempty"`
    39  	Package   string    `xml:"package,attr,omitempty"`
    40  	Hostname  string    `xml:"hostname,attr"`
    41  
    42  	Properties []*Property `xml:"properties,omitempty"`
    43  	TestCases  []*TestCase `xml:"testcase"`
    44  
    45  	SystemOut string `xml:"system-out,omitempty"`
    46  	SystemErr string `xml:"system-err,omitempty"`
    47  }
    48  
    49  // Update iterates through the TestCases and updates Tests, Errors,
    50  // Failures, and Skipped top level attributes.
    51  func (t *TestSuite) Update() {
    52  	t.Tests = len(t.TestCases)
    53  	for _, tc := range t.TestCases {
    54  		t.Errors += len(tc.Errors)
    55  		t.Failures += len(tc.Failures)
    56  		if len(tc.Skipped) > 0 {
    57  			t.Skipped++
    58  		}
    59  	}
    60  }
    61  
    62  // Property is a simple key-value property that can be attached to a TestSuite.
    63  type Property struct {
    64  	XMLName xml.Name `xml:"property"`
    65  
    66  	Name  string `xml:"name,attr"`
    67  	Value string `xml:"value,attr"`
    68  }
    69  
    70  // Error represents the errors in a test case.
    71  type Error struct {
    72  	XMLName xml.Name `xml:"error"`
    73  
    74  	Message string `xml:"message,attr,omitempty"`
    75  	Type    string `xml:"type,attr"`
    76  
    77  	Value string `xml:",cdata"`
    78  }
    79  
    80  // Failure represents the failures in a test case.
    81  type Failure struct {
    82  	XMLName xml.Name `xml:"failure"`
    83  
    84  	Message string `xml:"message,attr,omitempty"`
    85  	Type    string `xml:"type,attr"`
    86  
    87  	Value string `xml:",cdata"`
    88  }
    89  
    90  // TestCase represents a single test case within a suite.
    91  type TestCase struct {
    92  	XMLName xml.Name `xml:"testcase"`
    93  
    94  	Name       string  `xml:"name,attr"`
    95  	Classname  string  `xml:"classname,attr"`
    96  	Status     string  `xml:"status,attr,omitempty"`
    97  	Assertions int     `xml:"assertions,attr,omitempty"`
    98  	Time       float64 `xml:"time,attr"`
    99  
   100  	Skipped string `xml:"skipped,omitempty"`
   101  
   102  	Errors   []*Error   `xml:"error,omitempty"`
   103  	Failures []*Failure `xml:"failure,omitempty"`
   104  }