From 7259d12562f224fb9c0a3889f17e640852532fd8 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Sat, 9 Apr 2022 00:08:18 +0800 Subject: [PATCH] feat: add pytest sub-command to run pytest scripts --- README.en.md | 5 +++-- README.md | 5 +++-- docs/CHANGELOG.md | 3 ++- hrp/cmd/pytest.go | 24 ++++++++++++++++++++++++ hrp/cmd/run.go | 2 +- hrp/internal/pytest/main.go | 23 +++++++++++++++++++++++ 6 files changed, 56 insertions(+), 6 deletions(-) create mode 100644 hrp/cmd/pytest.go create mode 100644 hrp/internal/pytest/main.go diff --git a/README.en.md b/README.en.md index 6c743720..47eab79d 100644 --- a/README.en.md +++ b/README.en.md @@ -66,7 +66,7 @@ $ bash -c "$(curl -ksSL https://httprunner.oss-cn-beijing.aliyuncs.com/install.s Then you will get a `hrp` CLI tool. -```bash +```text $ hrp -h ██╗ ██╗████████╗████████╗██████╗ ██████╗ ██╗ ██╗███╗ ██╗███╗ ██╗███████╗██████╗ @@ -93,7 +93,8 @@ Available Commands: completion generate the autocompletion script for the specified shell har2case convert HAR to json/yaml testcase files help Help about any command - run run API test + pytest run API test with pytest + run run API test with go engine startproject create a scaffold project Flags: diff --git a/README.md b/README.md index 26714dc7..c623efa0 100644 --- a/README.md +++ b/README.md @@ -59,7 +59,7 @@ $ bash -c "$(curl -ksSL https://httprunner.oss-cn-beijing.aliyuncs.com/install.s 安装成功后,你将获得一个 `hrp` 命令行工具,执行 `hrp -h` 即可查看到参数帮助说明。 -```bash +```text $ hrp -h ██╗ ██╗████████╗████████╗██████╗ ██████╗ ██╗ ██╗███╗ ██╗███╗ ██╗███████╗██████╗ @@ -86,7 +86,8 @@ Available Commands: completion generate the autocompletion script for the specified shell har2case convert HAR to json/yaml testcase files help Help about any command - run run API test + pytest run API test with pytest + run run API test with go engine startproject create a scaffold project Flags: diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index ed378ce4..59d6af5d 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -17,12 +17,13 @@ **python version** +- feat: support retry when test step failed +- feat: add `pytest` sub-command to run pytest scripts - change: remove startproject, move all features to go version, replace with `hrp startproject` - change: remove har2case, move all features to go version, replace with `hrp run` - change: remove locust, you should run load tests with go version, replace with `hrp boom` - change: remove fastapi and uvicorn dependencies - change: add pytest.ini to make log colorful -- feat: support retry when test step failed - fix: ignore exceptions when reporting GA events - fix: remove misuse of NoReturn in Python typing diff --git a/hrp/cmd/pytest.go b/hrp/cmd/pytest.go new file mode 100644 index 00000000..8a5b4c5a --- /dev/null +++ b/hrp/cmd/pytest.go @@ -0,0 +1,24 @@ +package cmd + +import ( + "github.com/spf13/cobra" + + "github.com/httprunner/httprunner/hrp/internal/pytest" +) + +var pytestCmd = &cobra.Command{ + Use: "pytest $path ...", + Short: "run API test with pytest", + Args: cobra.MinimumNArgs(1), + PreRun: func(cmd *cobra.Command, args []string) { + setLogLevel(logLevel) + }, + DisableFlagParsing: true, // allow to pass any args to pytest + RunE: func(cmd *cobra.Command, args []string) error { + return pytest.RunPytest(args) + }, +} + +func init() { + rootCmd.AddCommand(pytestCmd) +} diff --git a/hrp/cmd/run.go b/hrp/cmd/run.go index ca537149..17f54014 100644 --- a/hrp/cmd/run.go +++ b/hrp/cmd/run.go @@ -11,7 +11,7 @@ import ( // runCmd represents the run command var runCmd = &cobra.Command{ Use: "run $path...", - Short: "run API test", + Short: "run API test with go engine", Long: `run yaml/json testcase files for API test`, Example: ` $ hrp run demo.json # run specified json testcase file $ hrp run demo.yaml # run specified yaml testcase file diff --git a/hrp/internal/pytest/main.go b/hrp/internal/pytest/main.go new file mode 100644 index 00000000..7304de99 --- /dev/null +++ b/hrp/internal/pytest/main.go @@ -0,0 +1,23 @@ +package pytest + +import ( + "os/exec" + "strings" + + "github.com/pkg/errors" + "github.com/rs/zerolog/log" +) + +func RunPytest(args []string) error { + cmd := exec.Command("pytest", args...) + log.Info().Str("cmd", cmd.String()).Msg("run pytest") + + output, err := cmd.CombinedOutput() + if err != nil { + return errors.Wrap(err, "pytest running failed") + } + out := strings.TrimSpace(string(output)) + println(out) + + return nil +}