How to use ReportAfterSuite method of ginkgo Package

Best Ginkgo code snippet using ginkgo.ReportAfterSuite

spec_runner.go

Source:spec_runner.go Github

copy

Full Screen

1package specrunner2import (3	"fmt"4	"os"5	"os/signal"6	"sync"7	"syscall"8	"github.com/onsi/ginkgo/config"9	"github.com/onsi/ginkgo/internal/leafnodes"10	"github.com/onsi/ginkgo/internal/spec"11	Writer "github.com/onsi/ginkgo/internal/writer"12	"github.com/onsi/ginkgo/reporters"13	"github.com/onsi/ginkgo/types"14	"time"15)16type SpecRunner struct {17	description     string18	beforeSuiteNode leafnodes.SuiteNode19	specs           *spec.Specs20	afterSuiteNode  leafnodes.SuiteNode21	reporters       []reporters.Reporter22	startTime       time.Time23	suiteID         string24	runningSpec     *spec.Spec25	writer          Writer.WriterInterface26	config          config.GinkgoConfigType27	interrupted     bool28	lock            *sync.Mutex29}30func New(description string, beforeSuiteNode leafnodes.SuiteNode, specs *spec.Specs, afterSuiteNode leafnodes.SuiteNode, reporters []reporters.Reporter, writer Writer.WriterInterface, config config.GinkgoConfigType) *SpecRunner {31	return &SpecRunner{32		description:     description,33		beforeSuiteNode: beforeSuiteNode,34		specs:           specs,35		afterSuiteNode:  afterSuiteNode,36		reporters:       reporters,37		writer:          writer,38		config:          config,39		suiteID:         randomID(),40		lock:            &sync.Mutex{},41	}42}43func (runner *SpecRunner) Run() bool {44	if runner.config.DryRun {45		runner.performDryRun()46		return true47	}48	runner.reportSuiteWillBegin()49	go runner.registerForInterrupts()50	suitePassed := runner.runBeforeSuite()51	if suitePassed {52		suitePassed = runner.runSpecs()53	}54	runner.blockForeverIfInterrupted()55	suitePassed = runner.runAfterSuite() && suitePassed56	runner.reportSuiteDidEnd(suitePassed)57	return suitePassed58}59func (runner *SpecRunner) performDryRun() {60	runner.reportSuiteWillBegin()61	if runner.beforeSuiteNode != nil {62		summary := runner.beforeSuiteNode.Summary()63		summary.State = types.SpecStatePassed64		runner.reportBeforeSuite(summary)65	}66	for _, spec := range runner.specs.Specs() {67		summary := spec.Summary(runner.suiteID)68		runner.reportSpecWillRun(summary)69		if summary.State == types.SpecStateInvalid {70			summary.State = types.SpecStatePassed71		}72		runner.reportSpecDidComplete(summary, false)73	}74	if runner.afterSuiteNode != nil {75		summary := runner.afterSuiteNode.Summary()76		summary.State = types.SpecStatePassed77		runner.reportAfterSuite(summary)78	}79	runner.reportSuiteDidEnd(true)80}81func (runner *SpecRunner) runBeforeSuite() bool {82	if runner.beforeSuiteNode == nil || runner.wasInterrupted() {83		return true84	}85	runner.writer.Truncate()86	conf := runner.config87	passed := runner.beforeSuiteNode.Run(conf.ParallelNode, conf.ParallelTotal, conf.SyncHost)88	if !passed {89		runner.writer.DumpOut()90	}91	runner.reportBeforeSuite(runner.beforeSuiteNode.Summary())92	return passed93}94func (runner *SpecRunner) runAfterSuite() bool {95	if runner.afterSuiteNode == nil {96		return true97	}98	runner.writer.Truncate()99	conf := runner.config100	passed := runner.afterSuiteNode.Run(conf.ParallelNode, conf.ParallelTotal, conf.SyncHost)101	if !passed {102		runner.writer.DumpOut()103	}104	runner.reportAfterSuite(runner.afterSuiteNode.Summary())105	return passed106}107func (runner *SpecRunner) runSpecs() bool {108	suiteFailed := false109	skipRemainingSpecs := false110	for _, spec := range runner.specs.Specs() {111		if runner.wasInterrupted() {112			return suiteFailed113		}114		if skipRemainingSpecs {115			spec.Skip()116		}117		runner.reportSpecWillRun(spec.Summary(runner.suiteID))118		if !spec.Skipped() && !spec.Pending() {119			runner.runningSpec = spec120			spec.Run(runner.writer)121			runner.runningSpec = nil122			if spec.Failed() {123				suiteFailed = true124			}125		} else if spec.Pending() && runner.config.FailOnPending {126			suiteFailed = true127		}128		runner.reportSpecDidComplete(spec.Summary(runner.suiteID), spec.Failed())129		if spec.Failed() && runner.config.FailFast {130			skipRemainingSpecs = true131		}132	}133	return !suiteFailed134}135func (runner *SpecRunner) CurrentSpecSummary() (*types.SpecSummary, bool) {136	if runner.runningSpec == nil {137		return nil, false138	}139	return runner.runningSpec.Summary(runner.suiteID), true140}141func (runner *SpecRunner) registerForInterrupts() {142	c := make(chan os.Signal, 1)143	signal.Notify(c, os.Interrupt, syscall.SIGTERM)144	<-c145	signal.Stop(c)146	runner.markInterrupted()147	go runner.registerForHardInterrupts()148	runner.writer.DumpOutWithHeader(`149Received interrupt.  Emitting contents of GinkgoWriter...150---------------------------------------------------------151`)152	if runner.afterSuiteNode != nil {153		fmt.Fprint(os.Stderr, `154---------------------------------------------------------155Received interrupt.  Running AfterSuite...156^C again to terminate immediately157`)158		runner.runAfterSuite()159	}160	runner.reportSuiteDidEnd(false)161	os.Exit(1)162}163func (runner *SpecRunner) registerForHardInterrupts() {164	c := make(chan os.Signal, 1)165	signal.Notify(c, os.Interrupt, syscall.SIGTERM)166	<-c167	fmt.Fprintln(os.Stderr, "\nReceived second interrupt.  Shutting down.")168	os.Exit(1)169}170func (runner *SpecRunner) blockForeverIfInterrupted() {171	runner.lock.Lock()172	interrupted := runner.interrupted173	runner.lock.Unlock()174	if interrupted {175		select {}176	}177}178func (runner *SpecRunner) markInterrupted() {179	runner.lock.Lock()180	defer runner.lock.Unlock()181	runner.interrupted = true182}183func (runner *SpecRunner) wasInterrupted() bool {184	runner.lock.Lock()185	defer runner.lock.Unlock()186	return runner.interrupted187}188func (runner *SpecRunner) reportSuiteWillBegin() {189	runner.startTime = time.Now()190	summary := runner.summary(true)191	for _, reporter := range runner.reporters {192		reporter.SpecSuiteWillBegin(runner.config, summary)193	}194}195func (runner *SpecRunner) reportBeforeSuite(summary *types.SetupSummary) {196	for _, reporter := range runner.reporters {197		reporter.BeforeSuiteDidRun(summary)198	}199}200func (runner *SpecRunner) reportAfterSuite(summary *types.SetupSummary) {201	for _, reporter := range runner.reporters {202		reporter.AfterSuiteDidRun(summary)203	}204}205func (runner *SpecRunner) reportSpecWillRun(summary *types.SpecSummary) {206	runner.writer.Truncate()207	for _, reporter := range runner.reporters {208		reporter.SpecWillRun(summary)209	}210}211func (runner *SpecRunner) reportSpecDidComplete(summary *types.SpecSummary, failed bool) {212	for i := len(runner.reporters) - 1; i >= 1; i-- {213		runner.reporters[i].SpecDidComplete(summary)214	}215	if failed {216		runner.writer.DumpOut()217	}218	runner.reporters[0].SpecDidComplete(summary)219}220func (runner *SpecRunner) reportSuiteDidEnd(success bool) {221	summary := runner.summary(success)222	summary.RunTime = time.Since(runner.startTime)223	for _, reporter := range runner.reporters {224		reporter.SpecSuiteDidEnd(summary)225	}226}227func (runner *SpecRunner) countSpecsSatisfying(filter func(ex *spec.Spec) bool) (count int) {228	count = 0229	for _, spec := range runner.specs.Specs() {230		if filter(spec) {231			count++232		}233	}234	return count235}236func (runner *SpecRunner) summary(success bool) *types.SuiteSummary {237	numberOfSpecsThatWillBeRun := runner.countSpecsSatisfying(func(ex *spec.Spec) bool {238		return !ex.Skipped() && !ex.Pending()239	})240	numberOfPendingSpecs := runner.countSpecsSatisfying(func(ex *spec.Spec) bool {241		return ex.Pending()242	})243	numberOfSkippedSpecs := runner.countSpecsSatisfying(func(ex *spec.Spec) bool {244		return ex.Skipped()245	})246	numberOfPassedSpecs := runner.countSpecsSatisfying(func(ex *spec.Spec) bool {247		return ex.Passed()248	})249	numberOfFailedSpecs := runner.countSpecsSatisfying(func(ex *spec.Spec) bool {250		return ex.Failed()251	})252	if runner.beforeSuiteNode != nil && !runner.beforeSuiteNode.Passed() && !runner.config.DryRun {253		numberOfFailedSpecs = numberOfSpecsThatWillBeRun254	}255	return &types.SuiteSummary{256		SuiteDescription: runner.description,257		SuiteSucceeded:   success,258		SuiteID:          runner.suiteID,259		NumberOfSpecsBeforeParallelization: runner.specs.NumberOfOriginalSpecs(),260		NumberOfTotalSpecs:                 len(runner.specs.Specs()),261		NumberOfSpecsThatWillBeRun:         numberOfSpecsThatWillBeRun,262		NumberOfPendingSpecs:               numberOfPendingSpecs,263		NumberOfSkippedSpecs:               numberOfSkippedSpecs,264		NumberOfPassedSpecs:                numberOfPassedSpecs,265		NumberOfFailedSpecs:                numberOfFailedSpecs,266	}267}...

Full Screen

Full Screen

reporting_dsl.go

Source:reporting_dsl.go Github

copy

Full Screen

...75func ReportAfterEach(body func(SpecReport)) bool {76	return pushNode(internal.NewReportAfterEachNode(body, types.NewCodeLocation(1)))77}78/*79ReportAfterSuite nodes are run at the end of the suite.  ReportAfterSuite nodes take a function that receives a suite Report.80They are called at the end of the suite, after all specs have run and any AfterSuite or SynchronizedAfterSuite nodes, and are passed in the final report for the suite.81ReportAftersuite nodes must be created at the top-level (i.e. not nested in a Context/Describe/When node)82When running in parallel, Ginkgo ensures that only one of the parallel nodes runs the ReportAfterSuite and that it is passed a report that is aggregated across83all parallel nodes84In addition to using ReportAfterSuite to programmatically generate suite reports, you can also generate JSON, JUnit, and Teamcity formatted reports using the --json-report, --junit-report, and --teamcity-report ginkgo CLI flags.85You cannot nest any other Ginkgo nodes within a ReportAfterSuite node's closure.86You can learn more about ReportAfterSuite here: https://onsi.github.io/ginkgo/#generating-reports-programmatically87You can learn more about Ginkgo's reporting infrastructure, including generating reports with the CLI here: https://onsi.github.io/ginkgo/#generating-machine-readable-reports88*/89func ReportAfterSuite(text string, body func(Report)) bool {90	return pushNode(internal.NewReportAfterSuiteNode(text, body, types.NewCodeLocation(1)))91}92func registerReportAfterSuiteNodeForAutogeneratedReports(reporterConfig types.ReporterConfig) {93	body := func(report Report) {94		if reporterConfig.JSONReport != "" {95			err := reporters.GenerateJSONReport(report, reporterConfig.JSONReport)96			if err != nil {97				Fail(fmt.Sprintf("Failed to generate JSON report:\n%s", err.Error()))98			}99		}100		if reporterConfig.JUnitReport != "" {101			err := reporters.GenerateJUnitReport(report, reporterConfig.JUnitReport)102			if err != nil {103				Fail(fmt.Sprintf("Failed to generate JUnit report:\n%s", err.Error()))104			}105		}106		if reporterConfig.TeamcityReport != "" {107			err := reporters.GenerateTeamcityReport(report, reporterConfig.TeamcityReport)108			if err != nil {109				Fail(fmt.Sprintf("Failed to generate Teamcity report:\n%s", err.Error()))110			}111		}112	}113	flags := []string{}114	if reporterConfig.JSONReport != "" {115		flags = append(flags, "--json-report")116	}117	if reporterConfig.JUnitReport != "" {118		flags = append(flags, "--junit-report")119	}120	if reporterConfig.TeamcityReport != "" {121		flags = append(flags, "--teamcity-report")122	}123	pushNode(internal.NewReportAfterSuiteNode(124		fmt.Sprintf("Autogenerated ReportAfterSuite for %s", strings.Join(flags, " ")),125		body,126		types.NewCustomCodeLocation("autogenerated by Ginkgo"),127	))128}...

Full Screen

Full Screen

ReportAfterSuite

Using AI Code Generation

copy

Full Screen

1import (2func TestMain(m *testing.M) {3    gomega.RegisterFailHandler(ginkgo.Fail)4    ginkgo.RunSpecs(t, "Test Suite")5}6var _ = ginkgo.Describe("Test Suite", func() {7    ginkgo.It("Test Case", func() {8        ginkgo.By("Test Step")9        fmt.Println("Test Step")10    })11})12func init() {13    ginkgo.BeforeSuite(func() {14        fmt.Println("Before Suite")15    })16    ginkgo.AfterSuite(func() {17        fmt.Println("After Suite")18    })19}20func ReportAfterSuite() {21    if config.GinkgoConfig.ParallelTotal > 1 {22        ginkgo.GinkgoRecover()23    }24    ginkgo.DefaultReporterConfig.Prefixes = &types.Prefixes{25    }26    ginkgo.DefaultReporterConfig.Suffixes = &types.Suffixes{27    }28    ginkgo.RunSpecsWithDefaultAndCustomReporters(t, "Test Suite", []ginkgo.Reporter{ginkgo.NewDefaultReporter(ginkgo.DefaultReporterConfig)})29}30import (

Full Screen

Full Screen

ReportAfterSuite

Using AI Code Generation

copy

Full Screen

1import (2func TestMain(m *testing.M) {3    gomega.RegisterFailHandler(ginkgo.Fail)4    ginkgo.RunSpecs(t, "Test Suite")5    os.Exit(m.Run())6}7import (8func TestMain(m *testing.M) {9    gomega.RegisterFailHandler(ginkgo.Fail)10    ginkgo.RunSpecs(t, "Test Suite")11    os.Exit(m.Run())12}13import (14func TestMain(m *testing.M) {15    gomega.RegisterFailHandler(ginkgo.Fail)16    ginkgo.RunSpecs(t, "Test Suite")17    os.Exit(m.Run())18}19import (20func TestMain(m *testing.M) {21    gomega.RegisterFailHandler(ginkgo.Fail)22    ginkgo.RunSpecs(t, "Test Suite")23    os.Exit(m.Run())24}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful