refactor: run tests with pytest

This commit is contained in:
debugtalk
2022-04-22 19:28:16 +08:00
parent 2681d4c815
commit 8f659effa7
12 changed files with 57 additions and 17 deletions

View File

@@ -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

View File

@@ -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

21
docs/cmd/hrp_convert.md Normal file
View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

2
go.mod
View File

@@ -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

4
go.sum
View File

@@ -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=

View File

@@ -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

View File

@@ -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()

View File

@@ -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")
}