test: parseStepStruct

This commit is contained in:
lilong.129
2024-08-21 13:30:11 +08:00
parent 6b409ce649
commit 88c1d62a76
3 changed files with 57 additions and 5 deletions
+17 -4
View File
@@ -490,8 +490,11 @@ func (r *SessionRunner) Start(givenVars map[string]interface{}) (summary *TestCa
default: default:
// parse step struct // parse step struct
err = r.parseStepStruct(step) err = r.parseStepStruct(step)
if err != nil && r.caseRunner.hrpRunner.failfast { if err != nil {
return summary, errors.Wrap(err, "parse step struct failed") log.Error().Err(err).Msg("parse step struct failed")
if r.caseRunner.hrpRunner.failfast {
return summary, errors.Wrap(err, "parse step struct failed")
}
} }
stepName := step.Name() stepName := step.Name()
@@ -570,8 +573,8 @@ func (r *SessionRunner) Start(givenVars map[string]interface{}) (summary *TestCa
func (r *SessionRunner) parseStepStruct(step IStep) error { func (r *SessionRunner) parseStepStruct(step IStep) error {
stepStruct := step.Struct() stepStruct := step.Struct()
// parse step variables: merges step variables with config variables and session variables // update step variables: merges step variables with config variables and session variables
// override variables, step variables > session variables (extracted variables from previous steps) // variables priority: step variables > session variables (extracted variables from previous steps)
overrideVars := mergeVariables(stepStruct.Variables, r.sessionVariables) overrideVars := mergeVariables(stepStruct.Variables, r.sessionVariables)
// step variables > testcase config variables // step variables > testcase config variables
overrideVars = mergeVariables(overrideVars, r.caseRunner.Config.Variables) overrideVars = mergeVariables(overrideVars, r.caseRunner.Config.Variables)
@@ -600,6 +603,16 @@ func (r *SessionRunner) parseStepStruct(step IStep) error {
if !ok { if !ok {
return errors.New("validator type error") return errors.New("validator type error")
} }
// parse validator check
// FIXME: validate with current step's extracted variables
// check, err := r.caseRunner.parser.Parse(
// validator.Check, stepStruct.Variables)
// if err != nil {
// return errors.Wrap(err, "failed to parse validator check")
// }
// validator.Check, _ = check.(string)
// parse validator expect
validator.Expect, err = r.caseRunner.parser.Parse( validator.Expect, err = r.caseRunner.parser.Parse(
validator.Expect, stepStruct.Variables) validator.Expect, stepStruct.Variables)
if err != nil { if err != nil {
+39
View File
@@ -7,6 +7,7 @@ import (
"time" "time"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/stretchr/testify/assert"
"github.com/httprunner/httprunner/v4/hrp/internal/code" "github.com/httprunner/httprunner/v4/hrp/internal/code"
) )
@@ -222,3 +223,41 @@ func TestRunCaseWithRefAPI(t *testing.T) {
t.Fatal() t.Fatal()
} }
} }
func TestSessionRunner(t *testing.T) {
testcase := TestCase{
Config: NewConfig("TestCase").
WithVariables(map[string]interface{}{
"a": 12.3,
"b": 3.45,
"varFoo": "${max($a, $b)}",
}),
TestSteps: []IStep{
NewStep("check variables").
WithVariables(map[string]interface{}{
"a": 12.3,
"b": 34.5,
"varFoo": "${max($a, $b)}",
}).
GET("/hello").
Validate().
AssertEqual("status_code", 200, "check status code"),
// AssertEqual("$varFoo", "$b", "check varFoo value"),
},
}
caseRunner, _ := NewRunner(t).NewCaseRunner(testcase)
sessionRunner := caseRunner.NewSession()
step := testcase.TestSteps[0]
if !assert.Equal(t, step.Struct().Variables["varFoo"], "${max($a, $b)}") {
t.Fatal()
}
err := sessionRunner.parseStepStruct(step)
if err != nil {
t.Fatal()
}
if !assert.Equal(t, step.Struct().Variables["varFoo"], 34.5) {
t.Fatal()
}
}
+1 -1
View File
@@ -326,7 +326,7 @@ func (s *StepMobile) Sleep(n float64) *StepMobile {
// SleepRandom specify random sleeping seconds after last action // SleepRandom specify random sleeping seconds after last action
// params have two different kinds: // params have two different kinds:
// 1. [min, max] : min and max are float64 time range boudaries // 1. [min, max] : min and max are float64 time range boundaries
// 2. [min1, max1, weight1, min2, max2, weight2, ...] : weight is the probability of the time range // 2. [min1, max1, weight1, min2, max2, weight2, ...] : weight is the probability of the time range
func (s *StepMobile) SleepRandom(params ...float64) *StepMobile { func (s *StepMobile) SleepRandom(params ...float64) *StepMobile {
s.obj().Actions = append(s.obj().Actions, uixt.MobileAction{ s.obj().Actions = append(s.obj().Actions, uixt.MobileAction{