mirror of
https://github.com/httprunner/httprunner.git
synced 2026-06-05 07:49:37 +08:00
feat: add function step
This commit is contained in:
79
hrp/step_function.go
Normal file
79
hrp/step_function.go
Normal file
@@ -0,0 +1,79 @@
|
||||
package hrp
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/httprunner/httprunner/v4/hrp/pkg/uixt"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
// StepFunction implements IStep interface.
|
||||
type StepFunction struct {
|
||||
StepConfig
|
||||
Fn func()
|
||||
}
|
||||
|
||||
func (s *StepFunction) Name() string {
|
||||
return s.StepName
|
||||
}
|
||||
|
||||
func (s *StepFunction) Type() StepType {
|
||||
return stepTypeFunction
|
||||
}
|
||||
|
||||
func (s *StepFunction) Config() *StepConfig {
|
||||
return &StepConfig{
|
||||
StepName: s.StepName,
|
||||
Variables: s.Variables,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *StepFunction) Run(r *SessionRunner) (*StepResult, error) {
|
||||
return runStepFunction(r, s)
|
||||
}
|
||||
|
||||
func runStepFunction(r *SessionRunner, step IStep) (stepResult *StepResult, err error) {
|
||||
var fn func()
|
||||
switch stepFn := step.(type) {
|
||||
case *StepFunction:
|
||||
fn = stepFn.Fn
|
||||
default:
|
||||
return nil, errors.New("unexpected function step type")
|
||||
}
|
||||
|
||||
log.Info().
|
||||
Str("name", step.Name()).
|
||||
Str("type", string(stepTypeFunction)).
|
||||
Msg("run function")
|
||||
|
||||
start := time.Now()
|
||||
stepResult = &StepResult{
|
||||
Name: step.Name(),
|
||||
StepType: stepTypeFunction,
|
||||
Success: false,
|
||||
ContentSize: 0,
|
||||
StartTime: start.Unix(),
|
||||
}
|
||||
defer func() {
|
||||
attachments := uixt.Attachments{}
|
||||
if err != nil {
|
||||
attachments["error"] = err.Error()
|
||||
}
|
||||
stepResult.Attachments = attachments
|
||||
stepResult.Elapsed = time.Since(start).Milliseconds()
|
||||
}()
|
||||
|
||||
vars := r.caseRunner.Config.Get().Variables
|
||||
for key, value := range vars {
|
||||
os.Setenv(key, fmt.Sprintf("%v", value))
|
||||
}
|
||||
|
||||
// exec function
|
||||
fn()
|
||||
|
||||
stepResult.Success = true
|
||||
return stepResult, nil
|
||||
}
|
||||
Reference in New Issue
Block a user