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