github.com/lonnblad/godog@v0.7.14-0.20200306004719-1b0cb3259847/fmt_junit_test.go (about)

     1  package godog
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/xml"
     6  	"fmt"
     7  	"io"
     8  	"strings"
     9  	"testing"
    10  
    11  	"github.com/cucumber/gherkin-go/v9"
    12  	"github.com/cucumber/messages-go/v9"
    13  	"github.com/stretchr/testify/assert"
    14  	"github.com/stretchr/testify/require"
    15  
    16  	"github.com/lonnblad/godog/colors"
    17  )
    18  
    19  var sampleGherkinFeature = `
    20  Feature: junit formatter
    21  
    22    Background:
    23      Given passing
    24  
    25    Scenario: passing scenario
    26      Then passing
    27  
    28    Scenario: failing scenario
    29      When failing
    30      Then passing
    31  
    32    Scenario: pending scenario
    33      When pending
    34      Then passing
    35  
    36    Scenario: undefined scenario
    37      When undefined
    38      Then next undefined
    39  
    40    Scenario Outline: outline
    41      Given <one>
    42      When <two>
    43  
    44      Examples:
    45        | one     | two     |
    46        | passing | passing |
    47        | passing | failing |
    48        | passing | pending |
    49  
    50  	Examples:
    51        | one     | two       |
    52        | passing | undefined |
    53  `
    54  
    55  func TestJUnitFormatterOutput(t *testing.T) {
    56  	const path = "any.feature"
    57  
    58  	gd, err := gherkin.ParseGherkinDocument(strings.NewReader(sampleGherkinFeature), (&messages.Incrementing{}).NewId)
    59  	require.NoError(t, err)
    60  
    61  	pickles := gherkin.Pickles(*gd, path, (&messages.Incrementing{}).NewId)
    62  
    63  	var buf bytes.Buffer
    64  	w := colors.Uncolored(&buf)
    65  	s := &Suite{
    66  		fmt: junitFunc("junit", w),
    67  		features: []*feature{{
    68  			GherkinDocument: gd,
    69  			pickles:         pickles,
    70  			Path:            path,
    71  			Content:         []byte(sampleGherkinFeature),
    72  		}},
    73  	}
    74  
    75  	s.Step(`^passing$`, func() error { return nil })
    76  	s.Step(`^failing$`, func() error { return fmt.Errorf("errored") })
    77  	s.Step(`^pending$`, func() error { return ErrPending })
    78  
    79  	const zeroDuration = "0"
    80  	expected := junitPackageSuite{
    81  		Name:     "junit",
    82  		Tests:    8,
    83  		Skipped:  0,
    84  		Failures: 2,
    85  		Errors:   4,
    86  		Time:     zeroDuration,
    87  		TestSuites: []*junitTestSuite{{
    88  			Name:     "junit formatter",
    89  			Tests:    8,
    90  			Skipped:  0,
    91  			Failures: 2,
    92  			Errors:   4,
    93  			Time:     zeroDuration,
    94  			TestCases: []*junitTestCase{
    95  				{
    96  					Name:   "passing scenario",
    97  					Status: "passed",
    98  					Time:   zeroDuration,
    99  				},
   100  				{
   101  					Name:   "failing scenario",
   102  					Status: "failed",
   103  					Time:   zeroDuration,
   104  					Failure: &junitFailure{
   105  						Message: "Step failing: errored",
   106  					},
   107  					Error: []*junitError{
   108  						{Message: "Step passing", Type: "skipped"},
   109  					},
   110  				},
   111  				{
   112  					Name:   "pending scenario",
   113  					Status: "pending",
   114  					Time:   zeroDuration,
   115  					Error: []*junitError{
   116  						{Message: "Step pending: TODO: write pending definition", Type: "pending"},
   117  						{Message: "Step passing", Type: "skipped"},
   118  					},
   119  				},
   120  				{
   121  					Name:   "undefined scenario",
   122  					Status: "undefined",
   123  					Time:   zeroDuration,
   124  					Error: []*junitError{
   125  						{Message: "Step undefined", Type: "undefined"},
   126  						{Message: "Step next undefined", Type: "undefined"},
   127  					},
   128  				},
   129  				{
   130  					Name:   "outline #1",
   131  					Status: "passed",
   132  					Time:   zeroDuration,
   133  				},
   134  				{
   135  					Name:   "outline #2",
   136  					Status: "failed",
   137  					Time:   zeroDuration,
   138  					Failure: &junitFailure{
   139  						Message: "Step failing: errored",
   140  					},
   141  				},
   142  				{
   143  					Name:   "outline #3",
   144  					Status: "pending",
   145  					Time:   zeroDuration,
   146  					Error: []*junitError{
   147  						{Message: "Step pending: TODO: write pending definition", Type: "pending"},
   148  					},
   149  				},
   150  				{
   151  					Name:   "outline #4",
   152  					Status: "undefined",
   153  					Time:   zeroDuration,
   154  					Error: []*junitError{
   155  						{Message: "Step undefined", Type: "undefined"},
   156  					},
   157  				},
   158  			},
   159  		}},
   160  	}
   161  
   162  	s.run()
   163  	s.fmt.Summary()
   164  
   165  	var exp bytes.Buffer
   166  	_, err = io.WriteString(&exp, xml.Header)
   167  	require.NoError(t, err)
   168  
   169  	enc := xml.NewEncoder(&exp)
   170  	enc.Indent("", "  ")
   171  	err = enc.Encode(expected)
   172  	require.NoError(t, err)
   173  
   174  	assert.Equal(t, exp.String(), buf.String())
   175  }