Skip to content

Commit

Permalink
Merge pull request #12 from dnephin/more-tests
Browse files Browse the repository at this point in the history
Add a test for junitxml
  • Loading branch information
dnephin committed May 5, 2018
2 parents f07cbf7 + c589559 commit f54338f
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 2 deletions.
2 changes: 1 addition & 1 deletion dobifiles/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

ARG GOLANG_VERSION
FROM golang:${GOLANG_VERSION:-1.10-alpine} as golang
FROM golang:${GOLANG_VERSION:-1.10.2-alpine} as golang
RUN apk add -U curl git bash
WORKDIR /go/src/gotest.tools/gotestsum
ENV CGO_ENABLED=0
Expand Down
11 changes: 11 additions & 0 deletions internal/junitxml/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,17 @@ func packageProperties() []JUnitProperty {
func packageTestCases(pkg *testjson.Package) []JUnitTestCase {
cases := []JUnitTestCase{}

if pkg.TestMainFailed() {
jtc := newJUnitTestCase(testjson.TestCase{
Test: "TestMain",
})
jtc.Failure = &JUnitFailure{
Message: "Failed",
Contents: pkg.Output(""),
}
cases = append(cases, jtc)
}

for _, tc := range pkg.Failed {
jtc := newJUnitTestCase(tc)
jtc.Failure = &JUnitFailure{
Expand Down
47 changes: 47 additions & 0 deletions internal/junitxml/report_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package junitxml

import (
"bytes"
"io"
"io/ioutil"
"testing"

"gotest.tools/assert"
"gotest.tools/golden"
"gotest.tools/gotestsum/testjson"
)

func TestWrite(t *testing.T) {
out := new(bytes.Buffer)
exec := createExecution(t)

err := Write(out, exec)
assert.NilError(t, err)
golden.Assert(t, out.String(), "junitxml-report.golden")
}

func createExecution(t *testing.T) *testjson.Execution {
exec, err := testjson.ScanTestOutput(testjson.ScanConfig{
Stdout: readTestData(t, "out"),
Stderr: readTestData(t, "err"),
Handler: &noopHandler{},
})
assert.NilError(t, err)
return exec
}

func readTestData(t *testing.T, stream string) io.Reader {
raw, err := ioutil.ReadFile("../../testjson/testdata/go-test-json." + stream)
assert.NilError(t, err)
return bytes.NewReader(raw)
}

type noopHandler struct{}

func (s *noopHandler) Event(testjson.TestEvent, *testjson.Execution) error {
return nil
}

func (s *noopHandler) Err(string) error {
return nil
}
83 changes: 83 additions & 0 deletions internal/junitxml/testdata/junitxml-report.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?xml version="1.0" encoding="UTF-8"?>
<testsuites>
<testsuite tests="0" failures="0" time="0.000s" name="github.com/gotestyourself/gotestyourself/testjson/internal/badmain">
<properties>
<property name="go.version" value="go1.10.2"></property>
</properties>
<testcase classname="." name="TestMain" time="0.000s">
<failure message="Failed" type="">sometimes main can exit 2&#xA;FAIL&#x9;github.com/gotestyourself/gotestyourself/testjson/internal/badmain&#x9;0.010s&#xA;</failure>
</testcase>
</testsuite>
<testsuite tests="18" failures="0" time="0.020s" name="github.com/gotestyourself/gotestyourself/testjson/internal/good">
<properties>
<property name="go.version" value="go1.10.2"></property>
</properties>
<testcase classname="good" name="TestSkipped" time="0.000s">
<skipped message="=== RUN TestSkipped&#xA;--- SKIP: TestSkipped (0.00s)&#xA;&#x9;good_test.go:23: &#xA;"></skipped>
</testcase>
<testcase classname="good" name="TestSkippedWitLog" time="0.000s">
<skipped message="=== RUN TestSkippedWitLog&#xA;--- SKIP: TestSkippedWitLog (0.00s)&#xA;&#x9;good_test.go:27: the skip message&#xA;"></skipped>
</testcase>
<testcase classname="good" name="TestPassed" time="0.000s"></testcase>
<testcase classname="good" name="TestPassedWithLog" time="0.000s"></testcase>
<testcase classname="good" name="TestPassedWithStdout" time="0.000s"></testcase>
<testcase classname="good" name="TestWithStderr" time="0.000s"></testcase>
<testcase classname="good" name="TestNestedSuccess/a/sub" time="0.000s"></testcase>
<testcase classname="good" name="TestNestedSuccess/a" time="0.000s"></testcase>
<testcase classname="good" name="TestNestedSuccess/b/sub" time="0.000s"></testcase>
<testcase classname="good" name="TestNestedSuccess/b" time="0.000s"></testcase>
<testcase classname="good" name="TestNestedSuccess/c/sub" time="0.000s"></testcase>
<testcase classname="good" name="TestNestedSuccess/c" time="0.000s"></testcase>
<testcase classname="good" name="TestNestedSuccess/d/sub" time="0.000s"></testcase>
<testcase classname="good" name="TestNestedSuccess/d" time="0.000s"></testcase>
<testcase classname="good" name="TestNestedSuccess" time="0.000s"></testcase>
<testcase classname="good" name="TestParallelTheThird" time="0.000s"></testcase>
<testcase classname="good" name="TestParallelTheSecond" time="0.010s"></testcase>
<testcase classname="good" name="TestParallelTheFirst" time="0.010s"></testcase>
</testsuite>
<testsuite tests="28" failures="4" time="0.020s" name="github.com/gotestyourself/gotestyourself/testjson/internal/stub">
<properties>
<property name="go.version" value="go1.10.2"></property>
</properties>
<testcase classname="stub" name="TestFailed" time="0.000s">
<failure message="Failed" type="">=== RUN TestFailed&#xA;--- FAIL: TestFailed (0.00s)&#xA;&#x9;stub_test.go:34: this failed&#xA;</failure>
</testcase>
<testcase classname="stub" name="TestFailedWithStderr" time="0.000s">
<failure message="Failed" type="">=== RUN TestFailedWithStderr&#xA;this is stderr&#xA;--- FAIL: TestFailedWithStderr (0.00s)&#xA;&#x9;stub_test.go:43: also failed&#xA;</failure>
</testcase>
<testcase classname="stub" name="TestNestedWithFailure/c" time="0.000s">
<failure message="Failed" type="">=== RUN TestNestedWithFailure/c&#xA; --- FAIL: TestNestedWithFailure/c (0.00s)&#xA; &#x9;stub_test.go:65: failed&#xA;</failure>
</testcase>
<testcase classname="stub" name="TestNestedWithFailure" time="0.000s">
<failure message="Failed" type="">=== RUN TestNestedWithFailure&#xA;--- FAIL: TestNestedWithFailure (0.00s)&#xA;</failure>
</testcase>
<testcase classname="stub" name="TestSkipped" time="0.000s">
<skipped message="=== RUN TestSkipped&#xA;--- SKIP: TestSkipped (0.00s)&#xA;&#x9;stub_test.go:26: &#xA;"></skipped>
</testcase>
<testcase classname="stub" name="TestSkippedWitLog" time="0.000s">
<skipped message="=== RUN TestSkippedWitLog&#xA;--- SKIP: TestSkippedWitLog (0.00s)&#xA;&#x9;stub_test.go:30: the skip message&#xA;"></skipped>
</testcase>
<testcase classname="stub" name="TestPassed" time="0.000s"></testcase>
<testcase classname="stub" name="TestPassedWithLog" time="0.000s"></testcase>
<testcase classname="stub" name="TestPassedWithStdout" time="0.000s"></testcase>
<testcase classname="stub" name="TestWithStderr" time="0.000s"></testcase>
<testcase classname="stub" name="TestNestedWithFailure/a/sub" time="0.000s"></testcase>
<testcase classname="stub" name="TestNestedWithFailure/a" time="0.000s"></testcase>
<testcase classname="stub" name="TestNestedWithFailure/b/sub" time="0.000s"></testcase>
<testcase classname="stub" name="TestNestedWithFailure/b" time="0.000s"></testcase>
<testcase classname="stub" name="TestNestedWithFailure/d/sub" time="0.000s"></testcase>
<testcase classname="stub" name="TestNestedWithFailure/d" time="0.000s"></testcase>
<testcase classname="stub" name="TestNestedSuccess/a/sub" time="0.000s"></testcase>
<testcase classname="stub" name="TestNestedSuccess/a" time="0.000s"></testcase>
<testcase classname="stub" name="TestNestedSuccess/b/sub" time="0.000s"></testcase>
<testcase classname="stub" name="TestNestedSuccess/b" time="0.000s"></testcase>
<testcase classname="stub" name="TestNestedSuccess/c/sub" time="0.000s"></testcase>
<testcase classname="stub" name="TestNestedSuccess/c" time="0.000s"></testcase>
<testcase classname="stub" name="TestNestedSuccess/d/sub" time="0.000s"></testcase>
<testcase classname="stub" name="TestNestedSuccess/d" time="0.000s"></testcase>
<testcase classname="stub" name="TestNestedSuccess" time="0.000s"></testcase>
<testcase classname="stub" name="TestParallelTheThird" time="0.000s"></testcase>
<testcase classname="stub" name="TestParallelTheSecond" time="0.010s"></testcase>
<testcase classname="stub" name="TestParallelTheFirst" time="0.010s"></testcase>
</testsuite>
</testsuites>
8 changes: 7 additions & 1 deletion testjson/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ func (p Package) Output(test string) string {
return strings.Join(p.output[test], "")
}

// TestMainFailed returns true if the package failed, but there were no tests.
// This may occur if the package init() or TestMain exited non-zero.
func (p Package) TestMainFailed() bool {
return p.action == ActionFail && len(p.Failed) == 0
}

// TestCase stores the name and elapsed time for a test case.
type TestCase struct {
Package string
Expand Down Expand Up @@ -200,7 +206,7 @@ func (e *Execution) Failed() []TestCase {
pkg := e.packages[name]

// Add package-level failure output if there were no failed tests.
if pkg.action == ActionFail && len(pkg.Failed) == 0 {
if pkg.TestMainFailed() {
failed = append(failed, TestCase{Package: name})
} else {
failed = append(failed, pkg.Failed...)
Expand Down

0 comments on commit f54338f

Please sign in to comment.