How to use SetFailure method of result Package

Best Gauge code snippet using result.SetFailure

testcases.go

Source:testcases.go Github

copy

Full Screen

1// package testcases defines the Case data structure, and a public function for2// instantiating slices of Cases from JSON test specifications in directory3// trees on the filesystem.4package testcases5import (6 "bytes"7 "context"8 "encoding/json"9 "fmt"10 "strings"11 "github.com/INFURA/eth2-comply/pkg/eth2spec"12 "github.com/INFURA/eth2-comply/pkg/oapi"13 "github.com/INFURA/eth2-comply/pkg/target"14)15// Case is an executable test case. The Config property can be accessed to get16// information about the case scenario.17type Case struct {18 Config CaseConfig19 Result Result20 Skipped bool21 Done chan struct{}22}23// CaseConfig describes a test scenario.24type CaseConfig struct {25 Method string26 Route string27 AwaitSlot int28 QueryParams map[string]string29 ReqBody interface{}30 ExpectedRespStatus int31 ExpectedRespBody interface{}32}33// Result describes the result of a test. Error is nil is success is true.34type Result struct {35 Success bool36 Error error37}38type OapiError struct {39 Err error40 ServerResponse []byte41}42func (e OapiError) Error() string {43 return fmt.Sprintf("OpenAPI client error!\nError: %s\nServer message: %s", e.Err.Error(), string(e.ServerResponse))44}45// NewCase instantiates and returns a Case struct.46func NewCase(config CaseConfig) *Case {47 c := &Case{}48 c.Config = config49 c.Result = Result{}50 c.Done = make(chan struct{})51 return c52}53// Exec executes a test Case and populates the Case's Result struct. The Result54// is unsuccessful and an error is stored in any of three cases:55//56// 1. The response was invalid for the request according to the OAPI schema.57// 2. The response contents did not satisfy the Case's expectations (if any).58// 3. Other reasons pertaining to the case specification or the environment.59// For example, the case contains an invalid route or cannot be unmarshaled60// (is ill-formed), or a network condition prevented contacting the target.61//62// Otherwise, the Result is marked as a Success and the Error is left nil.63func (c *Case) Exec(ctx context.Context, pathsRoot string) {64 defer close(c.Done)65 // If a test should be excluded because it is not beneath the paths root,66 // skip it here.67 if !strings.HasPrefix(c.Config.Route, pathsRoot) {68 c.Skipped = true69 return70 }71 // If a test specifies an await slot, wait for the node to sync that slot.72 if c.Config.AwaitSlot > 0 {73 if err := target.HasSlot(ctx, c.Config.AwaitSlot); err != nil {74 c.setFailure(err)75 return76 }77 }78 result, err := c.execOperation(ctx)79 if err != nil {80 // If the response is invalid in the OAPI schema, set that error here.81 if oapiErr, ok := err.(eth2spec.GenericOpenAPIError); ok {82 if len(oapiErr.Body()) > 0 {83 c.setFailure(OapiError{Err: oapiErr, ServerResponse: oapiErr.Body()})84 return85 }86 }87 // If an environmental error like a network failure occurred, set that88 // failure here.89 c.setFailure(err)90 return91 }92 err = c.assertExpectations(result)93 if err != nil {94 c.setFailure(err)95 return96 }97 c.Result.Success = true98}99// ResultsPretty returns human-readable test results output suitable for100// printing to a CLI.101func (c Case) ResultsPretty() string {102 // Build up a route string with the query params appended to the end103 routeString := fmt.Sprintf("%s %s", c.Config.Method, c.Config.Route)104 if len(c.Config.QueryParams) > 0 {105 routeString = fmt.Sprintf("%s%s", routeString, "?")106 for i, x := range c.Config.QueryParams {107 routeString = fmt.Sprintf("%s%s=%s&", routeString, i, x)108 }109 // Remove the trailing ampersand110 routeString = routeString[:len(routeString)-1]111 }112 var resultString string113 if c.Skipped {114 resultString = fmt.Sprintf("%s skipped\n", routeString)115 } else if !c.Result.Success {116 resultString = fmt.Sprintf("%s ❌\n%s", routeString, c.Result.Error.Error())117 } else {118 resultString = fmt.Sprintf("%s ✅\n", routeString)119 }120 return resultString121}122// setFailure marks a test case as having failed and records a corresponding123// error.124func (c *Case) setFailure(err error) {125 c.Result.Success = false126 c.Result.Error = err127}128// assertExpectations does expectations checking for the Case and returns an129// error if any stated expectations are not satisfied by actual results.130func (c Case) assertExpectations(result *oapi.ExecutorResult) error {131 // If the config has an expected resonse status, evaluate that.132 if c.Config.ExpectedRespStatus != 0 {133 if c.Config.ExpectedRespStatus != *result.StatusCode {134 return fmt.Errorf("Expected status code: %d\nReceived status code: %d", c.Config.ExpectedRespStatus, *result.StatusCode)135 }136 }137 // If the config has an expected response body, evaluate that.138 if c.Config.ExpectedRespBody != nil {139 // Get serialized JSON bytes for the expected response body, since Go140 // only has an `interface{}` type for the user-specified response body.141 data, err := json.Marshal(c.Config.ExpectedRespBody)142 if err != nil {143 return err144 }145 // Unmarshal the JSON bytes into the appropriate Go type, provided by146 // the result.ResponseDS.147 err = json.Unmarshal(data, &result.ResponseDS)148 if err != nil {149 return err150 }151 // Re-serialize the expected response back out into JSON bytes. This152 // time, the JSON bytes will be in a canonicalized form for their type.153 // Users may specify object keys in any order, but marshaling JSON from154 // a Go type will give you a canonical JSON encoding for that type.155 canonicalizedExpected, err := json.Marshal(result.ResponseDS)156 if err != nil {157 return err158 }159 // Serialize the actual received response into JSON bytes. Because the160 // result.Response is already a specific Go type (the same type as the161 // result.ResponseDS), serializing should produce a canonical form162 // identical to the canonical form of the expected response.163 canonicalizedActual, err := json.Marshal(result.Response)164 if err != nil {165 return err166 }167 // Because the serialized JSON bytes are canonicalized, we can just do168 // a bytes comparison to check equality.169 if !bytes.Equal(canonicalizedExpected, canonicalizedActual) {170 return fmt.Errorf("Expected response body:\n%s\n\nReceived response body:\n%s", canonicalizedExpected, canonicalizedActual)171 }172 }173 return nil174}...

Full Screen

Full Screen

job.go

Source:job.go Github

copy

Full Screen

...55 Timeout: timeout,56 Retries: retries,57 }58}59func (j *Job) SetFailure(message string) {60 j.fail = true61 j.failMessage = message62}63func (j *Job) Do() {64 oid := snmp.MustParseOid(j.Oid)65 result := []*SwitchResult{}66 wsnmp, err := snmp.NewWapSNMP(j.Host, j.Community, version, time.Duration(j.Timeout)*time.Millisecond, j.Retries)67 defer wsnmp.Close()68 if err != nil {69 Erroc++70 j.SetFailure(err.Error())71 } else {72 if !*Debug {73 table, err := wsnmp.GetTable(oid)74 if err != nil {75 Erroc++76 j.SetFailure(err.Error())77 } else {78 for k, v := range table {79 _, port := SplitData(k)80 flow := fmt.Sprintf("%v", v)81 result = append(result, NewSwitchResult(j.Host, port, flow, j.Oid))82 }83 }84 } else {85 woid, err := wsnmp.Get(oid)86 if err != nil {87 Erroc++88 j.SetFailure(err.Error())89 } else {90 flow := fmt.Sprintf("%v", woid)91 _, port := SplitData(oid.String())92 result = append(result, NewSwitchResult(j.Host, port, flow, j.Oid))93 }94 }95 }96 Wgroutinue++97 j.Result = result98}99//上报方法回调100func GenerateMessageReportMethod(r *Report) core.MF {101 return func(task core.Task) {102 tj := (*task.TargetObj).(*Job)...

Full Screen

Full Screen

result.go

Source:result.go Github

copy

Full Screen

...18 GetPostHook() []*gauge_messages.ProtoHookFailure19 GetFailed() bool20 AddPreHook(...*gauge_messages.ProtoHookFailure)21 AddPostHook(...*gauge_messages.ProtoHookFailure)22 SetFailure()23 Item() interface{}24 ExecTime() int6425}26// ExecTimeTracker is an interface for tracking execution time27type ExecTimeTracker interface {28 AddExecTime(int64)29}30// GetProtoHookFailure returns the failure result of hook execution31func GetProtoHookFailure(executionResult *gauge_messages.ProtoExecutionResult) *(gauge_messages.ProtoHookFailure) {32 return &gauge_messages.ProtoHookFailure{StackTrace: executionResult.StackTrace, ErrorMessage: executionResult.ErrorMessage, FailureScreenshot: executionResult.ScreenShot, TableRowIndex: -1}33}34// AddPreHook adds the before hook execution result to the actual result object35func AddPreHook(result Result, executionResult *gauge_messages.ProtoExecutionResult) {36 if executionResult.GetFailed() {37 result.AddPreHook(GetProtoHookFailure(executionResult))38 result.SetFailure()39 }40}41// AddPostHook adds the after hook execution result to the actual result object42func AddPostHook(result Result, executionResult *gauge_messages.ProtoExecutionResult) {43 if executionResult.GetFailed() {44 result.AddPostHook(GetProtoHookFailure(executionResult))45 result.SetFailure()46 }47}...

Full Screen

Full Screen

SetFailure

Using AI Code Generation

copy

Full Screen

1import "fmt"2type result struct {3}4func (r *result) SetFailure(err error) {5}6func main() {7 r := result{}8 r.SetFailure(fmt.Errorf("Error"))9 fmt.Println(r.err)10}11import "fmt"12type result struct {13}14func (r result) GetValue() int {15}16func main() {17 r := result{value: 10}18 fmt.Println(r.GetValue())19}20import "fmt"21type result struct {22}23func (r *result) SetValue(value int) {24}25func main() {26 r := result{}27 r.SetValue(10)28 fmt.Println(r.value)29}30import "fmt"31type result struct {32}33func (r result) SetValue(value int) {34}35func main() {36 r := result{}37 r.SetValue(10)38 fmt.Println(r.value)39}

Full Screen

Full Screen

SetFailure

Using AI Code Generation

copy

Full Screen

1import "fmt"2func main() {3 result.SetFailure("Error: Unable to connect to server")4 fmt.Println(result)5}6import "fmt"7func main() {8 result.SetFailure("Error: Unable to connect to server")9 fmt.Println(result)10}11import "fmt"12func main() {13 result.SetFailure("Error: Unable to connect to server")14 fmt.Println(result)15}16import "fmt"17func main() {18 result.SetFailure("Error: Unable to connect to server")19 fmt.Println(result)20}21import "fmt"22func main() {23 result.SetFailure("Error: Unable to connect to server")24 fmt.Println(result)25}26import "fmt"27func main() {28 result.SetFailure("Error: Unable to connect to server")29 fmt.Println(result)30}31import "fmt"32func main() {33 result.SetFailure("Error: Unable to connect to server")34 fmt.Println(result)35}36import "fmt"37func main() {38 result.SetFailure("Error: Unable to connect to server")39 fmt.Println(result)40}41import "fmt"42func main() {43 result.SetFailure("Error: Unable to connect to server")44 fmt.Println(result)45}46import "fmt"47func main() {48 result.SetFailure("

Full Screen

Full Screen

SetFailure

Using AI Code Generation

copy

Full Screen

1func TestSetFailure(t *testing.T) {2 t.Log("This is a log message")3 t.Fatal("This will mark the test as failed and stop the execution")4 t.Skip("This will mark the test as skipped")5}6func TestSetError(t *testing.T) {7 t.Log("This is a log message")8 t.Error("This will m

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