mirror of
https://github.com/httprunner/httprunner.git
synced 2026-08-28 19:47:14 +08:00
feat: hrp convert testcases to pytest scripts
This commit is contained in:
@@ -0,0 +1,48 @@
|
|||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
|
"github.com/httprunner/httprunner/hrp"
|
||||||
|
)
|
||||||
|
|
||||||
|
var convertCmd = &cobra.Command{
|
||||||
|
Use: "convert $path...",
|
||||||
|
Short: "convert JSON/YAML testcases to pytest/gotest scripts",
|
||||||
|
Args: cobra.ExactValidArgs(1),
|
||||||
|
PreRun: func(cmd *cobra.Command, args []string) {
|
||||||
|
setLogLevel(logLevel)
|
||||||
|
},
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
if !pytestFlag && !gotestFlag {
|
||||||
|
return errors.New("please specify convertion type")
|
||||||
|
}
|
||||||
|
|
||||||
|
var err error
|
||||||
|
if gotestFlag {
|
||||||
|
err = hrp.Convert2TestScripts("gotest", args...)
|
||||||
|
} else {
|
||||||
|
err = hrp.Convert2TestScripts("pytest", args...)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
log.Error().Err(err).Msg("convert test scripts failed")
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
pytestFlag bool
|
||||||
|
gotestFlag bool
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
rootCmd.AddCommand(convertCmd)
|
||||||
|
convertCmd.Flags().BoolVar(&pytestFlag, "pytest", true, "convert to pytest scripts")
|
||||||
|
convertCmd.Flags().BoolVar(&gotestFlag, "gotest", false, "convert to gotest scripts (TODO)")
|
||||||
|
}
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
package hrp
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os/exec"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
|
|
||||||
|
"github.com/httprunner/httprunner/hrp/internal/builtin"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Convert2TestScripts(destType string, paths ...string) error {
|
||||||
|
if destType == "gotest" {
|
||||||
|
return convert2GoTestScripts(paths...)
|
||||||
|
} else {
|
||||||
|
return convert2PyTestScripts(paths...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func convert2PyTestScripts(paths ...string) error {
|
||||||
|
python3, err := builtin.EnsurePython3Venv("httprunner")
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, "ensure python venv failed")
|
||||||
|
}
|
||||||
|
|
||||||
|
args := append([]string{"-m", "httprunner", "make"}, paths...)
|
||||||
|
cmd := exec.Command(python3, args...)
|
||||||
|
log.Info().Str("cmd", cmd.String()).Msg("convert to pytest scripts")
|
||||||
|
|
||||||
|
output, err := cmd.CombinedOutput()
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, "pytest running failed")
|
||||||
|
}
|
||||||
|
out := strings.TrimSpace(string(output))
|
||||||
|
println(out)
|
||||||
|
|
||||||
|
log.Info().Msg("convert to pytest scripts successfully")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func convert2GoTestScripts(paths ...string) error {
|
||||||
|
log.Warn().Msg("convert to gotest scripts is not supported yet")
|
||||||
|
// report event
|
||||||
|
// sdk.SendEvent(sdk.EventTracking{
|
||||||
|
// Category: "Convert",
|
||||||
|
// Action: fmt.Sprintf("hrp convert to %s", destType),
|
||||||
|
// })
|
||||||
|
|
||||||
|
// var testCasePaths []ITestCase
|
||||||
|
// for _, path := range paths {
|
||||||
|
// testCasePath := TestCasePath(path)
|
||||||
|
// testCasePaths = append(testCasePaths, &testCasePath)
|
||||||
|
// }
|
||||||
|
|
||||||
|
// _, err := loadTestCases(testCasePaths...)
|
||||||
|
// if err != nil {
|
||||||
|
// log.Error().Err(err).Msg("failed to load testcases")
|
||||||
|
// return err
|
||||||
|
// }
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -108,13 +108,5 @@ def main():
|
|||||||
main_make(args.testcase_path)
|
main_make(args.testcase_path)
|
||||||
|
|
||||||
|
|
||||||
def main_make_alias():
|
|
||||||
""" command alias
|
|
||||||
hmake = httprunner make
|
|
||||||
"""
|
|
||||||
sys.argv.insert(1, "make")
|
|
||||||
main()
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|||||||
@@ -54,7 +54,6 @@ coverage = "^4.5.4"
|
|||||||
|
|
||||||
[tool.poetry.scripts]
|
[tool.poetry.scripts]
|
||||||
httprunner = "httprunner.cli:main"
|
httprunner = "httprunner.cli:main"
|
||||||
hmake = "httprunner.cli:main_make_alias"
|
|
||||||
|
|
||||||
[build-system]
|
[build-system]
|
||||||
requires = ["poetry>=1.0.0"]
|
requires = ["poetry>=1.0.0"]
|
||||||
|
|||||||
Reference in New Issue
Block a user