Help us shape the future of Go! Take the 2026 Go Developer Survey

junit

package
v1.233.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 21, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package junit models JUnit XML test reports and provides encoding, decoding, and a markdown renderer. Aside from the repo-wide perf tracking helper it has no Atmos dependencies, so it can be reused by any producer of test results (terraform/opentofu test, the `junit` workflow step, etc.).

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrMarshal indicates the report could not be encoded to XML.
	ErrMarshal = errors.New("failed to encode JUnit XML")
	// ErrParse indicates the input could not be decoded as JUnit XML.
	ErrParse = errors.New("failed to parse JUnit XML")
)

Static errors (the repo bans dynamic errors; these keep the package dependency-free).

Functions

func Format

func Format(r *Report) ([]byte, error)

Format encodes the report as indented JUnit XML with an XML header.

func Markdown

func Markdown(r *Report, opts Options) string

Markdown renders a GitHub-flavored markdown summary of the report: a heading, total/passed/failed/errored/skipped badges, a per-case results table, and the failure messages. It is pure and reusable as a CI step summary for any JUnit-producing test runner.

Types

type Case

type Case struct {
	Name      string  `xml:"name,attr"`
	Classname string  `xml:"classname,attr,omitempty"`
	File      string  `xml:"file,attr,omitempty"`
	Line      int     `xml:"line,attr,omitempty"`
	Time      float64 `xml:"time,attr,omitempty"`
	Failure   *Detail `xml:"failure,omitempty"`
	Error     *Detail `xml:"error,omitempty"`
	Skipped   *Detail `xml:"skipped,omitempty"`
}

Case is a JUnit `<testcase>` — one per test/run. Exactly one of Failure, Error, or Skipped is set for non-passing cases; all nil means the case passed. File/Line are optional attributes many CI test reporters and editors honor.

func (*Case) Status

func (c *Case) Status() string

Status reports whether a case passed, failed, errored, or was skipped.

type Detail

type Detail struct {
	Message string `xml:"message,attr,omitempty"`
	Type    string `xml:"type,attr,omitempty"`
	Text    string `xml:",chardata"`
}

Detail is the body of a `<failure>`, `<error>`, or `<skipped>` element: a short Message attribute plus optional Text content.

type FailureRef

type FailureRef struct {
	Suite   string
	Name    string
	File    string
	Line    int
	Message string
}

FailureRef is a flattened reference to a failing/errored case, for callers that build CI annotations (which live outside this dependency-free package).

type Options

type Options struct {
	// Title is the heading shown above the report (e.g. "Terraform tests").
	// Defaults to "Test results" when empty.
	Title string
}

Options controls how Markdown renders a report.

type Report

type Report struct {
	XMLName  xml.Name `xml:"testsuites"`
	Name     string   `xml:"name,attr,omitempty"`
	Tests    int      `xml:"tests,attr"`
	Failures int      `xml:"failures,attr"`
	Errors   int      `xml:"errors,attr"`
	Skipped  int      `xml:"skipped,attr"`
	Time     float64  `xml:"time,attr,omitempty"`
	Suites   []Suite  `xml:"testsuite"`
}

Report is the JUnit `<testsuites>` root: a collection of suites with rolled-up counts.

func Parse

func Parse(data []byte) (Report, error)

Parse decodes JUnit XML into a Report. It accepts both a `<testsuites>` root and a bare `<testsuite>` root (which is wrapped into a single-suite report), since test runners differ on which they emit.

func (*Report) Aggregate

func (r *Report) Aggregate()

Aggregate recomputes each suite's counts from its cases and the report's totals from its suites, so callers can build a Report by appending cases and let the library fill in the attribute counts.

func (*Report) FailedCases

func (r *Report) FailedCases() []FailureRef

FailedCases returns one FailureRef per failing or errored case across all suites (named to avoid colliding with the Failures count field).

func (*Report) Passed

func (r *Report) Passed() bool

Passed reports whether every case passed (no failures or errors). Skips do not count as failures.

type Suite

type Suite struct {
	Name     string  `xml:"name,attr"`
	Tests    int     `xml:"tests,attr"`
	Failures int     `xml:"failures,attr"`
	Errors   int     `xml:"errors,attr"`
	Skipped  int     `xml:"skipped,attr"`
	Time     float64 `xml:"time,attr,omitempty"`
	Cases    []Case  `xml:"testcase"`
}

Suite is a JUnit `<testsuite>` — typically one per test file.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL