MCPcopy
hub / github.com/httprunner/httprunner / Validate

Method Validate

hrp/response.go:164–226  ·  view source on GitHub ↗
(iValidators []interface{}, variablesMapping map[string]interface{})

Source from the content-addressed store, hash-verified

162}
163
164func (v *responseObject) Validate(iValidators []interface{}, variablesMapping map[string]interface{}) (err error) {
165 for _, iValidator := range iValidators {
166 validator, ok := iValidator.(Validator)
167 if !ok {
168 return errors.New("validator type error")
169 }
170 // parse check value
171 checkItem := validator.Check
172 checkValue := v.searchField(checkItem, variablesMapping)
173
174 // get assert method
175 assertMethod := validator.Assert
176 assertFunc, ok := builtin.Assertions[assertMethod]
177 if !ok {
178 return errors.New(fmt.Sprintf("unexpected assertMethod: %v", assertMethod))
179 }
180
181 // parse expected value
182 expectValue, err := v.parser.Parse(validator.Expect, variablesMapping)
183 if err != nil {
184 return err
185 }
186 validResult := &ValidationResult{
187 Validator: Validator{
188 Check: validator.Check,
189 Expect: expectValue,
190 Assert: assertMethod,
191 Message: validator.Message,
192 },
193 CheckValue: checkValue,
194 CheckResult: "fail",
195 }
196
197 // do assertion
198 result := assertFunc(v.t, checkValue, expectValue)
199 if result {
200 validResult.CheckResult = "pass"
201 }
202 v.validationResults = append(v.validationResults, validResult)
203 log.Info().
204 Str("checkExpr", validator.Check).
205 Str("assertMethod", assertMethod).
206 Interface("expectValue", expectValue).
207 Str("expectValueType", builtin.InterfaceType(expectValue)).
208 Interface("checkValue", checkValue).
209 Str("checkValueType", builtin.InterfaceType(checkValue)).
210 Bool("result", result).
211 Msgf("validate %s", checkItem)
212 if !result {
213 v.t.Fail()
214 log.Error().
215 Str("checkExpr", validator.Check).
216 Str("assertMethod", assertMethod).
217 Interface("checkValue", checkValue).
218 Str("checkValueType", builtin.InterfaceType(checkValue)).
219 Interface("expectValue", expectValue).
220 Str("expectValueType", builtin.InterfaceType(expectValue)).
221 Msg("assert failed")

Callers 15

TestBoomerStandaloneRunFunction · 0.45
TestIOSSettingsActionFunction · 0.45
TestIOSSearchAppFunction · 0.45
TestIOSDouyinActionFunction · 0.45
TestAndroidActionFunction · 0.45
TestRunCaseWithTimeoutFunction · 0.45
assertRunTestCasesFunction · 0.45
testcase_test.goFile · 0.45
TestHTTPProtocolFunction · 0.45

Calls 3

searchFieldMethod · 0.95
InterfaceTypeFunction · 0.92
ParseMethod · 0.80

Tested by 15

TestBoomerStandaloneRunFunction · 0.36
TestIOSSettingsActionFunction · 0.36
TestIOSSearchAppFunction · 0.36
TestIOSDouyinActionFunction · 0.36
TestAndroidActionFunction · 0.36
TestRunCaseWithTimeoutFunction · 0.36
assertRunTestCasesFunction · 0.36
TestHTTPProtocolFunction · 0.36
TestWebSocketProtocolFunction · 0.36