Best Gauge code snippet using rerun.addFailedMetadata
rerun.go
Source:rerun.go
...84 switch e.Topic {85 case event.ScenarioEnd:86 prepareScenarioFailedMetadata(e.Result.(*result.ScenarioResult), e.Item.(*gauge.Scenario), e.ExecutionInfo)87 case event.SpecEnd:88 addFailedMetadata(e.Result, addSpecFailedMetadata)89 case event.SuiteEnd:90 addFailedMetadata(e.Result, addSuiteFailedMetadata)91 failedMeta.aggregateFailedItems()92 writeFailedMeta(getJSON(failedMeta))93 }94 }95 }()96}97func prepareScenarioFailedMetadata(res *result.ScenarioResult, sce *gauge.Scenario, executionInfo gauge_messages.ExecutionInfo) {98 if res.GetFailed() {99 specPath := executionInfo.GetCurrentSpec().GetFileName()100 failedScenario := util.RelPathToProjectRoot(specPath)101 failedMeta.addFailedItem(specPath, fmt.Sprintf("%s:%v", failedScenario, sce.Span.Start))102 }103}104func addSpecFailedMetadata(res result.Result) {105 fileName := util.RelPathToProjectRoot(res.(*result.SpecResult).ProtoSpec.GetFileName())106 if _, ok := failedMeta.failedItemsMap[fileName]; ok {107 delete(failedMeta.failedItemsMap, fileName)108 }109 failedMeta.addFailedItem(fileName, fileName)110}111func addSuiteFailedMetadata(res result.Result) {112 failedMeta.failedItemsMap = make(map[string]map[string]bool)113 for _, arg := range flag.Args() {114 path, err := filepath.Abs(arg)115 path = util.RelPathToProjectRoot(path)116 if err == nil {117 failedMeta.addFailedItem(path, path)118 }119 }120}121func addFailedMetadata(res result.Result, add func(res result.Result)) {122 if (res.GetPostHook() != nil && *res.GetPostHook() != nil) || (res.GetPreHook() != nil && *res.GetPreHook() != nil) {123 add(res)124 }125}126func writeFailedMeta(contents string) {127 failuresFile := filepath.Join(config.ProjectRoot, dotGauge, failedFile)128 dotGaugeDir := filepath.Join(config.ProjectRoot, dotGauge)129 if err := os.MkdirAll(dotGaugeDir, common.NewDirectoryPermissions); err != nil {130 logger.Fatalf("Failed to create directory in %s. Reason: %s", dotGaugeDir, err.Error())131 }132 err := ioutil.WriteFile(failuresFile, []byte(contents), common.NewFilePermissions)133 if err != nil {134 logger.Fatalf("Failed to write to %s. Reason: %s", failuresFile, err.Error())135 }...
rerun_test.go
Source:rerun_test.go
...48func (s *MySuite) TestAddSpecPreHookFailedMetadata(c *C) {49 spec1Rel := filepath.Join("specs", "example1.spec")50 spec1Abs := filepath.Join(config.ProjectRoot, spec1Rel)51 spec1 := &result.SpecResult{ProtoSpec: &gauge_messages.ProtoSpec{PreHookFailures: []*gauge_messages.ProtoHookFailure{{ErrorMessage: "error"}}, FileName: spec1Abs}}52 addFailedMetadata(spec1, []string{}, addSpecFailedMetadata)53 c.Assert(len(failedMeta.failedItemsMap[spec1Rel]), Equals, 1)54 c.Assert(failedMeta.failedItemsMap[spec1Rel][spec1Rel], Equals, true)55}56func (s *MySuite) TestAddSpecPostHookFailedMetadata(c *C) {57 spec1Rel := filepath.Join("specs", "example1.spec")58 spec1Abs := filepath.Join(config.ProjectRoot, spec1Rel)59 spec1 := &result.SpecResult{ProtoSpec: &gauge_messages.ProtoSpec{PostHookFailures: []*gauge_messages.ProtoHookFailure{{ErrorMessage: "error"}}, FileName: spec1Abs}}60 addFailedMetadata(spec1, []string{}, addSpecFailedMetadata)61 c.Assert(len(failedMeta.failedItemsMap[spec1Rel]), Equals, 1)62 c.Assert(failedMeta.failedItemsMap[spec1Rel][spec1Rel], Equals, true)63}64func (s *MySuite) TestAddSpecFailedMetadataOverwritesPreviouslyAddedValues(c *C) {65 spec1Rel := filepath.Join("specs", "example1.spec")66 spec1Abs := filepath.Join(config.ProjectRoot, spec1Rel)67 spec1 := &result.SpecResult{ProtoSpec: &gauge_messages.ProtoSpec{PreHookFailures: []*gauge_messages.ProtoHookFailure{{ErrorMessage: "error"}}, FileName: spec1Abs}}68 failedMeta.failedItemsMap[spec1Rel] = make(map[string]bool)69 failedMeta.failedItemsMap[spec1Rel]["scn1"] = true70 failedMeta.failedItemsMap[spec1Rel]["scn2"] = true71 addSpecFailedMetadata(spec1, []string{})72 c.Assert(len(failedMeta.failedItemsMap[spec1Rel]), Equals, 1)73 c.Assert(failedMeta.failedItemsMap[spec1Rel][spec1Rel], Equals, true)74}...
addFailedMetadata
Using AI Code Generation
1import (2type rerun struct {3}4func (r *rerun) addFailedMetadata(metadata string) {5 r.FailedMetadata = append(r.FailedMetadata, metadata)6}7func main() {8 r := &rerun{}9 r.addFailedMetadata("test")10 fmt.Println(r.FailedMetadata)11}
addFailedMetadata
Using AI Code Generation
1import (2type rerun struct {3}4func main() {5 r := rerun{}6 r.failedMetadata = make(map[string][]string)7 r.addFailedMetadata("abc", "def")8 r.addFailedMetadata("ghi", "jkl")9 r.addFailedMetadata("abc", "mno")10 r.addFailedMetadata("pqr", "stu")11 fmt.Println(r.failedMetadata)12}13func (r *rerun) addFailedMetadata(key string, value string) {14 if _, ok := r.failedMetadata[key]; ok {15 r.failedMetadata[key] = append(r.failedMetadata[key], value)16 } else {17 r.failedMetadata[key] = []string{value}18 }19}20import (21type rerun struct {22}23func main() {24 r := rerun{}25 r.failedMetadata = make(map[string][]string)26 r.addFailedMetadata("abc", "def")27 r.addFailedMetadata("ghi", "jkl")28 r.addFailedMetadata("abc", "mno")29 r.addFailedMetadata("pqr", "stu")30 fmt.Println(r.failedMetadata)31}32func (r *rerun) addFailedMetadata(key string, value string) {33 if _, ok := r.failedMetadata[key]; ok {34 r.failedMetadata[key] = append(r.failedMetadata[key], value)35 } else {36 r.failedMetadata[key] = []string{value}37 }38}
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!