feat: add pytest sub-command to run pytest scripts

This commit is contained in:
debugtalk
2022-04-09 00:08:18 +08:00
parent 3e404da4b4
commit 7259d12562
6 changed files with 56 additions and 6 deletions

View File

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

View File

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

View File

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

24
hrp/cmd/pytest.go Normal file
View File

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

View File

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

View File

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