mirror of
https://github.com/httprunner/httprunner.git
synced 2026-09-08 08:57:40 +08:00
refactor: SessionRunner, merge GetSummary into Start
This commit is contained in:
+45
-55
@@ -234,20 +234,11 @@ func (r *HRPRunner) Run(testcases ...ITestCase) (err error) {
|
|||||||
// case runner can run multiple times with different parameters
|
// case runner can run multiple times with different parameters
|
||||||
// each run has its own session runner
|
// each run has its own session runner
|
||||||
sessionRunner := caseRunner.NewSession()
|
sessionRunner := caseRunner.NewSession()
|
||||||
err1 := sessionRunner.Start(it.Next())
|
caseSummary, err := sessionRunner.Start(it.Next())
|
||||||
if err1 != nil {
|
|
||||||
log.Error().Err(err1).Msg("[Run] run testcase failed")
|
|
||||||
runErr = err1
|
|
||||||
}
|
|
||||||
caseSummary, err2 := sessionRunner.GetSummary()
|
|
||||||
s.appendCaseSummary(caseSummary)
|
s.appendCaseSummary(caseSummary)
|
||||||
if err2 != nil {
|
if err != nil {
|
||||||
log.Error().Err(err2).Msg("[Run] get summary failed")
|
log.Error().Err(err).Msg("[Run] run testcase failed")
|
||||||
if err1 != nil {
|
runErr = err
|
||||||
runErr = errors.Wrap(err1, err2.Error())
|
|
||||||
} else {
|
|
||||||
runErr = err2
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if runErr != nil && r.failfast {
|
if runErr != nil && r.failfast {
|
||||||
@@ -438,7 +429,7 @@ type SessionRunner struct {
|
|||||||
|
|
||||||
// Start runs the test steps in sequential order.
|
// Start runs the test steps in sequential order.
|
||||||
// givenVars is used for data driven
|
// givenVars is used for data driven
|
||||||
func (r *SessionRunner) Start(givenVars map[string]interface{}) error {
|
func (r *SessionRunner) Start(givenVars map[string]interface{}) (summary *TestCaseSummary, err error) {
|
||||||
// report GA event
|
// report GA event
|
||||||
sdk.SendGA4Event("hrp_session_runner_start", nil)
|
sdk.SendGA4Event("hrp_session_runner_start", nil)
|
||||||
|
|
||||||
@@ -448,15 +439,50 @@ func (r *SessionRunner) Start(givenVars map[string]interface{}) error {
|
|||||||
// update config variables with given variables
|
// update config variables with given variables
|
||||||
r.InitWithParameters(givenVars)
|
r.InitWithParameters(givenVars)
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
summary = r.summary
|
||||||
|
summary.Name = r.caseRunner.Config.Name
|
||||||
|
summary.Time.Duration = time.Since(summary.Time.StartAt).Seconds()
|
||||||
|
exportVars := make(map[string]interface{})
|
||||||
|
for _, value := range r.caseRunner.Config.Export {
|
||||||
|
exportVars[value] = r.sessionVariables[value]
|
||||||
|
}
|
||||||
|
summary.InOut.ExportVars = exportVars
|
||||||
|
summary.InOut.ConfigVars = r.caseRunner.Config.Variables
|
||||||
|
|
||||||
|
// TODO: move to mobile ui step
|
||||||
|
for uuid, client := range uiClients {
|
||||||
|
// add WDA/UIA logs to summary
|
||||||
|
logs := map[string]interface{}{
|
||||||
|
"uuid": uuid,
|
||||||
|
}
|
||||||
|
|
||||||
|
if client.Device.LogEnabled() {
|
||||||
|
log, err := client.Driver.StopCaptureLog()
|
||||||
|
if err != nil {
|
||||||
|
err = errors.Wrap(err, "get summary failed")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
logs["content"] = log
|
||||||
|
}
|
||||||
|
|
||||||
|
// stop performance monitor
|
||||||
|
logs["performance"] = client.Device.StopPerf()
|
||||||
|
logs["pcap"] = client.Device.StopPcap()
|
||||||
|
|
||||||
|
summary.Logs = append(summary.Logs, logs)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
// run step in sequential order
|
// run step in sequential order
|
||||||
for _, step := range r.caseRunner.TestSteps {
|
for _, step := range r.caseRunner.TestSteps {
|
||||||
select {
|
select {
|
||||||
case <-r.caseRunner.hrpRunner.caseTimeoutTimer.C:
|
case <-r.caseRunner.hrpRunner.caseTimeoutTimer.C:
|
||||||
log.Warn().Msg("timeout in session runner")
|
log.Warn().Msg("timeout in session runner")
|
||||||
return errors.Wrap(code.TimeoutError, "session runner timeout")
|
return summary, errors.Wrap(code.TimeoutError, "session runner timeout")
|
||||||
case <-r.caseRunner.hrpRunner.interruptSignal:
|
case <-r.caseRunner.hrpRunner.interruptSignal:
|
||||||
log.Warn().Msg("interrupted in session runner")
|
log.Warn().Msg("interrupted in session runner")
|
||||||
return errors.Wrap(code.InterruptError, "session runner interrupted")
|
return summary, errors.Wrap(code.InterruptError, "session runner interrupted")
|
||||||
default:
|
default:
|
||||||
// TODO: parse step struct
|
// TODO: parse step struct
|
||||||
// parse step name
|
// parse step name
|
||||||
@@ -523,18 +549,18 @@ func (r *SessionRunner) Start(givenVars map[string]interface{}) error {
|
|||||||
|
|
||||||
// interrupted or timeout, abort running
|
// interrupted or timeout, abort running
|
||||||
if errors.Is(err, code.InterruptError) || errors.Is(err, code.TimeoutError) {
|
if errors.Is(err, code.InterruptError) || errors.Is(err, code.TimeoutError) {
|
||||||
return err
|
return summary, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// check if failfast
|
// check if failfast
|
||||||
if r.caseRunner.hrpRunner.failfast {
|
if r.caseRunner.hrpRunner.failfast {
|
||||||
return errors.Wrap(err, "abort running due to failfast setting")
|
return summary, errors.Wrap(err, "abort running due to failfast setting")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Info().Str("testcase", config.Name).Msg("run testcase end")
|
log.Info().Str("testcase", config.Name).Msg("run testcase end")
|
||||||
return nil
|
return summary, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ParseStepVariables merges step variables with config variables and session variables
|
// ParseStepVariables merges step variables with config variables and session variables
|
||||||
@@ -585,42 +611,6 @@ func (r *SessionRunner) InitWithParameters(parameters map[string]interface{}) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *SessionRunner) GetSummary() (*TestCaseSummary, error) {
|
|
||||||
caseSummary := r.summary
|
|
||||||
caseSummary.Name = r.caseRunner.Config.Name
|
|
||||||
caseSummary.Time.Duration = time.Since(caseSummary.Time.StartAt).Seconds()
|
|
||||||
exportVars := make(map[string]interface{})
|
|
||||||
for _, value := range r.caseRunner.Config.Export {
|
|
||||||
exportVars[value] = r.sessionVariables[value]
|
|
||||||
}
|
|
||||||
caseSummary.InOut.ExportVars = exportVars
|
|
||||||
caseSummary.InOut.ConfigVars = r.caseRunner.Config.Variables
|
|
||||||
|
|
||||||
// TODO: move to mobile ui step
|
|
||||||
// for uuid, client := range r.caseRunner.uiClients {
|
|
||||||
// // add WDA/UIA logs to summary
|
|
||||||
// logs := map[string]interface{}{
|
|
||||||
// "uuid": uuid,
|
|
||||||
// }
|
|
||||||
|
|
||||||
// if client.Device.LogEnabled() {
|
|
||||||
// log, err := client.Driver.StopCaptureLog()
|
|
||||||
// if err != nil {
|
|
||||||
// return caseSummary, err
|
|
||||||
// }
|
|
||||||
// logs["content"] = log
|
|
||||||
// }
|
|
||||||
|
|
||||||
// // stop performance monitor
|
|
||||||
// logs["performance"] = client.Device.StopPerf()
|
|
||||||
// logs["pcap"] = client.Device.StopPcap()
|
|
||||||
|
|
||||||
// caseSummary.Logs = append(caseSummary.Logs, logs)
|
|
||||||
// }
|
|
||||||
|
|
||||||
return caseSummary, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// updateSummary updates summary of StepResult.
|
// updateSummary updates summary of StepResult.
|
||||||
func (r *SessionRunner) updateSummary(stepResult *StepResult) {
|
func (r *SessionRunner) updateSummary(stepResult *StepResult) {
|
||||||
switch stepResult.StepType {
|
switch stepResult.StepType {
|
||||||
|
|||||||
@@ -84,10 +84,10 @@ func TestRunRequestStatOn(t *testing.T) {
|
|||||||
}
|
}
|
||||||
caseRunner, _ := NewRunner(t).SetHTTPStatOn().NewCaseRunner(testcase)
|
caseRunner, _ := NewRunner(t).SetHTTPStatOn().NewCaseRunner(testcase)
|
||||||
sessionRunner := caseRunner.NewSession()
|
sessionRunner := caseRunner.NewSession()
|
||||||
if err := sessionRunner.Start(nil); err != nil {
|
summary, err := sessionRunner.Start(nil)
|
||||||
|
if err != nil {
|
||||||
t.Fatal()
|
t.Fatal()
|
||||||
}
|
}
|
||||||
summary, _ := sessionRunner.GetSummary()
|
|
||||||
|
|
||||||
stat := summary.Records[0].HttpStat
|
stat := summary.Records[0].HttpStat
|
||||||
if !assert.GreaterOrEqual(t, stat["DNSLookup"], int64(0)) {
|
if !assert.GreaterOrEqual(t, stat["DNSLookup"], int64(0)) {
|
||||||
|
|||||||
+2
-10
@@ -91,19 +91,11 @@ func (s *StepTestCaseWithOptionalArgs) Run(r *SessionRunner) (stepResult *StepRe
|
|||||||
sessionRunner := caseRunner.NewSession()
|
sessionRunner := caseRunner.NewSession()
|
||||||
|
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
|
var summary *TestCaseSummary
|
||||||
// run referenced testcase with step variables
|
// run referenced testcase with step variables
|
||||||
err = sessionRunner.Start(stepVariables)
|
summary, err = sessionRunner.Start(stepVariables)
|
||||||
stepResult.Elapsed = time.Since(start).Milliseconds()
|
stepResult.Elapsed = time.Since(start).Milliseconds()
|
||||||
|
|
||||||
summary, err2 := sessionRunner.GetSummary()
|
|
||||||
if err2 != nil {
|
|
||||||
log.Error().Err(err2).Msg("get summary failed")
|
|
||||||
if err != nil {
|
|
||||||
err = errors.Wrap(err, err2.Error())
|
|
||||||
} else {
|
|
||||||
err = err2
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// update step names
|
// update step names
|
||||||
for _, record := range summary.Records {
|
for _, record := range summary.Records {
|
||||||
record.Name = fmt.Sprintf("%s - %s", stepResult.Name, record.Name)
|
record.Name = fmt.Sprintf("%s - %s", stepResult.Name, record.Name)
|
||||||
|
|||||||
Reference in New Issue
Block a user