mirror of
https://github.com/httprunner/httprunner.git
synced 2026-06-07 08:49:37 +08:00
feat: add function step
This commit is contained in:
@@ -1 +1 @@
|
||||
v5.0.0+2412232041
|
||||
v5.0.0+2412232123
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package hrp
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
@@ -176,6 +178,44 @@ func TestRunCaseWithShell(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunCaseWithFunction(t *testing.T) {
|
||||
fn1 := func() {
|
||||
fmt.Println("call function1 without return")
|
||||
}
|
||||
num := 0
|
||||
fn2 := func() {
|
||||
num++
|
||||
fmt.Println("call function2 with return value")
|
||||
}
|
||||
var err3 error
|
||||
fn3 := func() {
|
||||
num++
|
||||
err3 = errors.New("func3 error")
|
||||
fmt.Println("call function3 with return value and error")
|
||||
}
|
||||
testcase1 := &TestCase{
|
||||
Config: NewConfig("call function"),
|
||||
TestSteps: []IStep{
|
||||
NewStep("fn1").Function(fn1),
|
||||
NewStep("fn2").Function(fn2),
|
||||
NewStep("fn3").Function(fn3),
|
||||
},
|
||||
}
|
||||
|
||||
r := NewRunner(t)
|
||||
err := r.Run(testcase1)
|
||||
if err != nil {
|
||||
t.Fatal()
|
||||
}
|
||||
|
||||
if !assert.Equal(t, num, 2) {
|
||||
t.Fatal()
|
||||
}
|
||||
if !assert.NotNil(t, err3) {
|
||||
t.Fatal()
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunCaseWithPluginJSON(t *testing.T) {
|
||||
buildHashicorpGoPlugin()
|
||||
defer removeHashicorpGoPlugin()
|
||||
|
||||
@@ -16,6 +16,7 @@ const (
|
||||
stepTypeHarmony StepType = "harmony"
|
||||
stepTypeIOS StepType = "ios"
|
||||
stepTypeShell StepType = "shell"
|
||||
stepTypeFunction StepType = "function"
|
||||
|
||||
stepTypeSuffixExtraction StepType = "_extraction"
|
||||
stepTypeSuffixValidation StepType = "_validation"
|
||||
|
||||
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
|
||||
}
|
||||
@@ -811,6 +811,14 @@ func (s *StepRequest) Shell(content string) *StepShell {
|
||||
}
|
||||
}
|
||||
|
||||
// Function creates a new function step session
|
||||
func (s *StepRequest) Function(fn func()) *StepFunction {
|
||||
return &StepFunction{
|
||||
StepConfig: s.StepConfig,
|
||||
Fn: fn,
|
||||
}
|
||||
}
|
||||
|
||||
// StepRequestWithOptionalArgs implements IStep interface.
|
||||
type StepRequestWithOptionalArgs struct {
|
||||
*StepRequest
|
||||
|
||||
Reference in New Issue
Block a user