Skip to content

Commit

Permalink
Ginkgo polarion reporter
Browse files Browse the repository at this point in the history
Produces XML file that can be imported to polarion

Signed-off-by: Lukianov Artyom <[email protected]>
  • Loading branch information
Lukianov Artyom committed Mar 13, 2018
1 parent e555d56 commit 8275fdf
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ cluster/local/certs
**.csr
_out
vendor/**/*_test.go
**/polarion.xml
133 changes: 133 additions & 0 deletions tests/polarion_reporter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
* This file is part of the KubeVirt project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright 2018 Red Hat, Inc.
*
*/

package tests

import (
"encoding/xml"
"flag"
"fmt"
"os"
"strings"

"github.com/onsi/ginkgo/config"
"github.com/onsi/ginkgo/types"
)

var Polarion = PolarionReporter{}

func init() {
flag.BoolVar(&Polarion.Run, "polarion", false, "Run Polarion reporter")
flag.StringVar(&Polarion.projectId, "polarion-project-id", "", "Set the Polarion project ID")
flag.StringVar(&Polarion.filename, "polarion-report-file", "polarion.xml", "Set the Polarion report file path")
}

type PolarionTestCases struct {
XMLName xml.Name `xml:"testcases"`
TestCases []PolarionTestCase `xml:"testcase"`
ProjectID string `xml:"project-id,attr"`
}

type PolarionTestCase struct {
Title Title `xml:"title"`
Description Description `xml:"description"`
TestCaseCustomFields TestCaseCustomFields `xml:"custom-fields"`
}

type Title struct {
Content string `xml:",chardata"`
}

type Description struct {
Content string `xml:",chardata"`
}

type TestCaseCustomFields struct {
CustomFields []TestCaseCustomField `xml:"custom-field"`
}

type TestCaseCustomField struct {
Content string `xml:"content,attr"`
ID string `xml:"id,attr"`
}

type PolarionReporter struct {
suite PolarionTestCases
Run bool
filename string
projectId string
testSuiteName string
}

func (reporter *PolarionReporter) SpecSuiteWillBegin(config config.GinkgoConfigType, summary *types.SuiteSummary) {
reporter.suite = PolarionTestCases{
TestCases: []PolarionTestCase{},
}
reporter.testSuiteName = summary.SuiteDescription
}

func (reporter *PolarionReporter) SpecWillRun(specSummary *types.SpecSummary) {
}

func (reporter *PolarionReporter) BeforeSuiteDidRun(setupSummary *types.SetupSummary) {
}

func (reporter *PolarionReporter) AfterSuiteDidRun(setupSummary *types.SetupSummary) {
}

func (reporter *PolarionReporter) SpecDidComplete(specSummary *types.SpecSummary) {
testName := fmt.Sprintf(
"%s: %s",
specSummary.ComponentTexts[1],
strings.Join(specSummary.ComponentTexts[2:], " "),
)
testCase := PolarionTestCase{
Title: Title{Content: testName},
Description: Description{Content: testName},
}
customFields := TestCaseCustomFields{}
customFields.CustomFields = append(customFields.CustomFields, TestCaseCustomField{
Content: "automated",
ID: "caseautomation",
})
testCase.TestCaseCustomFields = customFields

reporter.suite.TestCases = append(reporter.suite.TestCases, testCase)
}

func (reporter *PolarionReporter) SpecSuiteDidEnd(summary *types.SuiteSummary) {
if reporter.projectId == "" {
fmt.Println("Can not create Polarion report without project ID")
return
}
reporter.suite.ProjectID = reporter.projectId
file, err := os.Create(reporter.filename)
if err != nil {
fmt.Printf("Failed to create Polarion report file: %s\n\t%s", reporter.filename, err.Error())
return
}
defer file.Close()
file.WriteString(xml.Header)
encoder := xml.NewEncoder(file)
encoder.Indent(" ", " ")
err = encoder.Encode(reporter.suite)
if err != nil {
fmt.Printf("Failed to generate Polarion report\n\t%s", err.Error())
}
}
6 changes: 5 additions & 1 deletion tests/tests_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ import (

func TestTests(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Tests Suite")
reporters := make([]Reporter, 0)
if tests.Polarion.Run {
reporters = append(reporters, &tests.Polarion)
}
RunSpecsWithDefaultAndCustomReporters(t, "Tests Suite", reporters)
}

var _ = BeforeSuite(func() {
Expand Down

0 comments on commit 8275fdf

Please sign in to comment.