mirror of
https://github.com/httprunner/httprunner.git
synced 2026-09-08 00:47:36 +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
|
package hrp
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"testing"
|
"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) {
|
func TestRunCaseWithPluginJSON(t *testing.T) {
|
||||||
buildHashicorpGoPlugin()
|
buildHashicorpGoPlugin()
|
||||||
defer removeHashicorpGoPlugin()
|
defer removeHashicorpGoPlugin()
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ const (
|
|||||||
stepTypeHarmony StepType = "harmony"
|
stepTypeHarmony StepType = "harmony"
|
||||||
stepTypeIOS StepType = "ios"
|
stepTypeIOS StepType = "ios"
|
||||||
stepTypeShell StepType = "shell"
|
stepTypeShell StepType = "shell"
|
||||||
|
stepTypeFunction StepType = "function"
|
||||||
|
|
||||||
stepTypeSuffixExtraction StepType = "_extraction"
|
stepTypeSuffixExtraction StepType = "_extraction"
|
||||||
stepTypeSuffixValidation StepType = "_validation"
|
stepTypeSuffixValidation StepType = "_validation"
|
||||||
|
|||||||
@@ -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.
|
// StepRequestWithOptionalArgs implements IStep interface.
|
||||||
type StepRequestWithOptionalArgs struct {
|
type StepRequestWithOptionalArgs struct {
|
||||||
*StepRequest
|
*StepRequest
|
||||||
|
|||||||
Reference in New Issue
Block a user