From 8f659effa74241cd8cbda9eaf3ab51765f8b324c Mon Sep 17 00:00:00 2001 From: debugtalk Date: Fri, 22 Apr 2022 19:28:16 +0800 Subject: [PATCH] refactor: run tests with pytest --- docs/cmd/hrp.md | 3 ++- docs/cmd/hrp_boom.md | 2 +- docs/cmd/hrp_convert.md | 21 +++++++++++++++++++++ docs/cmd/hrp_har2case.md | 2 +- docs/cmd/hrp_pytest.md | 2 +- docs/cmd/hrp_run.md | 2 +- docs/cmd/hrp_startproject.md | 2 +- go.mod | 2 +- go.sum | 4 ++-- hrp/internal/builtin/utils.go | 16 ++++++++++++++++ hrp/internal/pytest/main.go | 10 +++++++++- hrp/internal/scaffold/main.go | 8 +------- 12 files changed, 57 insertions(+), 17 deletions(-) create mode 100644 docs/cmd/hrp_convert.md diff --git a/docs/cmd/hrp.md b/docs/cmd/hrp.md index 1a3960a4..ebdeb949 100644 --- a/docs/cmd/hrp.md +++ b/docs/cmd/hrp.md @@ -30,9 +30,10 @@ Copyright 2021 debugtalk ### SEE ALSO * [hrp boom](hrp_boom.md) - run load test with boomer +* [hrp convert](hrp_convert.md) - convert JSON/YAML testcases to pytest/gotest scripts * [hrp har2case](hrp_har2case.md) - convert HAR to json/yaml testcase files * [hrp pytest](hrp_pytest.md) - run API test with pytest * [hrp run](hrp_run.md) - run API test with go engine * [hrp startproject](hrp_startproject.md) - create a scaffold project -###### Auto generated by spf13/cobra on 17-Apr-2022 +###### Auto generated by spf13/cobra on 22-Apr-2022 diff --git a/docs/cmd/hrp_boom.md b/docs/cmd/hrp_boom.md index 7be66d74..506f675e 100644 --- a/docs/cmd/hrp_boom.md +++ b/docs/cmd/hrp_boom.md @@ -41,4 +41,4 @@ hrp boom [flags] * [hrp](hrp.md) - Next-Generation API Testing Solution. -###### Auto generated by spf13/cobra on 17-Apr-2022 +###### Auto generated by spf13/cobra on 22-Apr-2022 diff --git a/docs/cmd/hrp_convert.md b/docs/cmd/hrp_convert.md new file mode 100644 index 00000000..b62216bb --- /dev/null +++ b/docs/cmd/hrp_convert.md @@ -0,0 +1,21 @@ +## hrp convert + +convert JSON/YAML testcases to pytest/gotest scripts + +``` +hrp convert $path... [flags] +``` + +### Options + +``` + --gotest convert to gotest scripts + -h, --help help for convert + --pytest convert to pytest scripts (default true) +``` + +### SEE ALSO + +* [hrp](hrp.md) - Next-Generation API Testing Solution. + +###### Auto generated by spf13/cobra on 22-Apr-2022 diff --git a/docs/cmd/hrp_har2case.md b/docs/cmd/hrp_har2case.md index 729b1226..7183b3a3 100644 --- a/docs/cmd/hrp_har2case.md +++ b/docs/cmd/hrp_har2case.md @@ -24,4 +24,4 @@ hrp har2case $har_path... [flags] * [hrp](hrp.md) - Next-Generation API Testing Solution. -###### Auto generated by spf13/cobra on 17-Apr-2022 +###### Auto generated by spf13/cobra on 22-Apr-2022 diff --git a/docs/cmd/hrp_pytest.md b/docs/cmd/hrp_pytest.md index 5a2363be..a07f1867 100644 --- a/docs/cmd/hrp_pytest.md +++ b/docs/cmd/hrp_pytest.md @@ -16,4 +16,4 @@ hrp pytest $path ... [flags] * [hrp](hrp.md) - Next-Generation API Testing Solution. -###### Auto generated by spf13/cobra on 17-Apr-2022 +###### Auto generated by spf13/cobra on 22-Apr-2022 diff --git a/docs/cmd/hrp_run.md b/docs/cmd/hrp_run.md index 7ad8d4d4..bf7543a1 100644 --- a/docs/cmd/hrp_run.md +++ b/docs/cmd/hrp_run.md @@ -34,4 +34,4 @@ hrp run $path... [flags] * [hrp](hrp.md) - Next-Generation API Testing Solution. -###### Auto generated by spf13/cobra on 17-Apr-2022 +###### Auto generated by spf13/cobra on 22-Apr-2022 diff --git a/docs/cmd/hrp_startproject.md b/docs/cmd/hrp_startproject.md index e33d46d6..a06d5ee5 100644 --- a/docs/cmd/hrp_startproject.md +++ b/docs/cmd/hrp_startproject.md @@ -19,4 +19,4 @@ hrp startproject $project_name [flags] * [hrp](hrp.md) - Next-Generation API Testing Solution. -###### Auto generated by spf13/cobra on 17-Apr-2022 +###### Auto generated by spf13/cobra on 22-Apr-2022 diff --git a/go.mod b/go.mod index 52540f0c..49c7f16b 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,7 @@ require ( github.com/getsentry/sentry-go v0.13.0 github.com/google/uuid v1.3.0 github.com/gorilla/websocket v1.4.1 - github.com/httprunner/funplugin v0.4.2 + github.com/httprunner/funplugin v0.4.3 github.com/jinzhu/copier v0.3.2 github.com/jmespath/go-jmespath v0.4.0 github.com/json-iterator/go v1.1.12 diff --git a/go.sum b/go.sum index ad484835..9647213c 100644 --- a/go.sum +++ b/go.sum @@ -241,8 +241,8 @@ github.com/hashicorp/memberlist v0.1.3/go.mod h1:ajVTdAv/9Im8oMAAj5G31PhhMCZJV2p github.com/hashicorp/serf v0.8.2/go.mod h1:6hOLApaqBFA1NXqRQAsxw9QxuDEvNxSQRwA/JwenrHc= github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb h1:b5rjCoWHc7eqmAS4/qyk21ZsHyb6Mxv/jykxvNTkU4M= github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM= -github.com/httprunner/funplugin v0.4.2 h1:iDeg3GVCKdimgZQ40xq0kxHqhL/DQmRxs3DRjzOpUuo= -github.com/httprunner/funplugin v0.4.2/go.mod h1:vPyeJIfbpGe0epZZtAV0wCn16gLY9+imSw/zfxq0Lcc= +github.com/httprunner/funplugin v0.4.3 h1:mxdxQh54NZLQnK/FXZxpZV0rhqZQzckrWKEnBW5w2Vg= +github.com/httprunner/funplugin v0.4.3/go.mod h1:vPyeJIfbpGe0epZZtAV0wCn16gLY9+imSw/zfxq0Lcc= github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= github.com/imkira/go-interpol v1.1.0/go.mod h1:z0h2/2T3XF8kyEPpRgJ3kmNv+C43p+I/CoI+jC3w2iA= diff --git a/hrp/internal/builtin/utils.go b/hrp/internal/builtin/utils.go index 7bf7a943..f328f1c4 100644 --- a/hrp/internal/builtin/utils.go +++ b/hrp/internal/builtin/utils.go @@ -17,6 +17,7 @@ import ( "github.com/rs/zerolog/log" "gopkg.in/yaml.v3" + "github.com/httprunner/funplugin/shared" "github.com/httprunner/httprunner/hrp/internal/json" ) @@ -76,6 +77,21 @@ func FormatResponse(raw interface{}) interface{} { return formattedResponse } +func EnsurePython3Venv(packages ...string) (string, error) { + // create python venv + home, err := os.UserHomeDir() + if err != nil { + return "", errors.Wrap(err, "get user home dir failed") + } + venvDir := filepath.Join(home, ".hrp", "venv") + python3, err := shared.EnsurePython3Venv(venvDir, packages...) + if err != nil { + return "", errors.Wrap(err, "ensure python venv failed") + } + + return python3, nil +} + func ExecCommand(cmd *exec.Cmd, cwd string) error { log.Info().Str("cmd", cmd.String()).Str("cwd", cwd).Msg("exec command") cmd.Dir = cwd diff --git a/hrp/internal/pytest/main.go b/hrp/internal/pytest/main.go index 7304de99..667213d0 100644 --- a/hrp/internal/pytest/main.go +++ b/hrp/internal/pytest/main.go @@ -6,10 +6,18 @@ import ( "github.com/pkg/errors" "github.com/rs/zerolog/log" + + "github.com/httprunner/httprunner/hrp/internal/builtin" ) func RunPytest(args []string) error { - cmd := exec.Command("pytest", args...) + python3, err := builtin.EnsurePython3Venv("httprunner") + if err != nil { + return errors.Wrap(err, "ensure python venv failed") + } + + args = append([]string{"-m", "httprunner", "run"}, args...) + cmd := exec.Command(python3, args...) log.Info().Str("cmd", cmd.String()).Msg("run pytest") output, err := cmd.CombinedOutput() diff --git a/hrp/internal/scaffold/main.go b/hrp/internal/scaffold/main.go index b7c1c0ad..808f3440 100644 --- a/hrp/internal/scaffold/main.go +++ b/hrp/internal/scaffold/main.go @@ -178,13 +178,7 @@ func createPythonPlugin(projectName string) error { return errors.Wrap(err, "copy file failed") } - // create python venv - home, err := os.UserHomeDir() - if err != nil { - return errors.Wrap(err, "get user home dir failed") - } - venvDir := filepath.Join(home, ".hrp", "venv") - _, err = shared.EnsurePython3Venv(venvDir) + _, err = builtin.EnsurePython3Venv(fmt.Sprintf("funppy==%s", shared.Version)) if err != nil { return errors.Wrap(err, "ensure python venv failed") }