From fbba7f483291bee93584cd6896c0d2d1ab6e0e51 Mon Sep 17 00:00:00 2001 From: guowei shieh Date: Tue, 10 May 2022 09:45:56 -0700 Subject: [PATCH 1/3] feat: add default value for var Signed-off-by: guowei shieh --- process_testcase.go | 9 ++++++--- types.go | 5 +++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/process_testcase.go b/process_testcase.go index 274c522d..22d420ca 100644 --- a/process_testcase.go +++ b/process_testcase.go @@ -432,9 +432,12 @@ func processVariableAssigments(ctx context.Context, tcName string, tcVars H, raw if !has { varValue, has = tcVars[tcName+"."+assigment.From] if !has { - err := fmt.Errorf("%s reference not found in %s", assigment.From, strings.Join(tcVarsKeys, "\n")) - Info(ctx, "%v", err) - return nil, true, err + if assigment.Default == "" { + err := fmt.Errorf("%s reference not found in %s", assigment.From, strings.Join(tcVarsKeys, "\n")) + Info(ctx, "%v", err) + return nil, true, err + } + varValue = assigment.Default } } if assigment.Regex == "" { diff --git a/types.go b/types.go index 4a88e227..3998fa43 100644 --- a/types.go +++ b/types.go @@ -250,8 +250,9 @@ type AssignStep struct { } type Assignment struct { - From string `json:"from" yaml:"from"` - Regex string `json:"regex" yaml:"regex"` + From string `json:"from" yaml:"from"` + Regex string `json:"regex" yaml:"regex"` + Default interface{} `json:"default" yaml:"default"` } // RemoveNotPrintableChar removes not printable chararacter from a string From ced3a3294d6a219d0b323817910306c4c09a1385 Mon Sep 17 00:00:00 2001 From: guowei shieh Date: Tue, 10 May 2022 09:51:56 -0700 Subject: [PATCH 2/3] change to nil Signed-off-by: guowei shieh --- process_testcase.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/process_testcase.go b/process_testcase.go index 22d420ca..2a2ba818 100644 --- a/process_testcase.go +++ b/process_testcase.go @@ -432,7 +432,7 @@ func processVariableAssigments(ctx context.Context, tcName string, tcVars H, raw if !has { varValue, has = tcVars[tcName+"."+assigment.From] if !has { - if assigment.Default == "" { + if assigment.Default == nil { err := fmt.Errorf("%s reference not found in %s", assigment.From, strings.Join(tcVarsKeys, "\n")) Info(ctx, "%v", err) return nil, true, err From 0b1c80d4a4d3aa97622477552e790e943532142e Mon Sep 17 00:00:00 2001 From: guowei shieh Date: Wed, 11 May 2022 11:48:27 -0700 Subject: [PATCH 3/3] change Signed-off-by: guowei shieh --- README.md | 3 ++- tests/exec.yml | 14 +++++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 8c410218..a8718f00 100644 --- a/README.md +++ b/README.md @@ -486,7 +486,7 @@ Available helpers and some examples: To be able to reuse a property from a teststep in a following testcase or step, you have to extract the variable, as the following example. After the first step execution, `venom` extracts a value using a regular expression `foo with a ([a-z]+) here` from the content of the `result.systemout` property returned by the `executor`. -Then this variable can be reused in another test, with the name `testA.myvariable` with `testA` corresponding to the name of the testcase. +Then this variable can be reused in another test, with the name `testA.myvariable` with `testA` corresponding to the name of the testcase. A default value could also be supplied if the variable can't be extracted from the output, which can commonly happen when parsing json output. ```yaml name: MyTestSuite @@ -499,6 +499,7 @@ testcases: myvariable: from: result.systemout regex: foo with a ([a-z]+) here + default: "somevalue" - name: testB steps: diff --git a/tests/exec.yml b/tests/exec.yml index 77643434..fda77633 100644 --- a/tests/exec.yml +++ b/tests/exec.yml @@ -33,4 +33,16 @@ testcases: - script: cat exec/testa.json info: "the value of result.systemoutjson is {{.result.systemoutjson}}" assertions: - - result.systemoutjson.foo ShouldContainSubstring bar \ No newline at end of file + - result.systemoutjson.foo ShouldContainSubstring bar + vars: + foo: + from: result.systemoutjson.foo + bar: + from: result.systemoutjson.notexisting + default: "test" + +- name: verify default + steps: + - script: echo "{{.cat-json.foo}} {{.cat-json.bar}}" + assertions: + - result.systemout ShouldEqual "bar test"