refactor: relocate convert

This commit is contained in:
debugtalk
2022-04-23 23:56:47 +08:00
parent 5073308b03
commit 813284110e
8 changed files with 166 additions and 69 deletions

View File

@@ -46,7 +46,7 @@ func (b *HRPBoomer) Run(testcases ...ITestCase) {
var taskSlice []*boomer.Task
// load all testcases
testCases, err := loadTestCases(testcases...)
testCases, err := LoadTestCases(testcases...)
if err != nil {
log.Error().Err(err).Msg("failed to load testcases")
os.Exit(1)

View File

@@ -7,7 +7,7 @@ import (
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/httprunner/httprunner/hrp"
"github.com/httprunner/httprunner/hrp/internal/convert"
)
var convertCmd = &cobra.Command{
@@ -24,9 +24,9 @@ var convertCmd = &cobra.Command{
var err error
if gotestFlag {
err = hrp.Convert2TestScripts("gotest", args...)
err = convert.Convert2TestScripts("gotest", args...)
} else {
err = hrp.Convert2TestScripts("pytest", args...)
err = convert.Convert2TestScripts("pytest", args...)
}
if err != nil {
log.Error().Err(err).Msg("convert test scripts failed")

View File

@@ -1,59 +0,0 @@
package hrp
import (
"github.com/rs/zerolog/log"
"github.com/httprunner/httprunner/hrp/internal/builtin"
"github.com/httprunner/httprunner/hrp/internal/sdk"
)
func Convert2TestScripts(destType string, paths ...string) error {
if destType == "gotest" {
return convert2GoTestScripts(paths...)
} else {
return convert2PyTestScripts(paths...)
}
}
func convert2PyTestScripts(paths ...string) error {
sdk.SendEvent(sdk.EventTracking{
Category: "ConvertTests",
Action: "hrp convert --pytest",
})
python3, err := builtin.EnsurePython3Venv("httprunner")
if err != nil {
return err
}
args := append([]string{"-m", "httprunner", "make"}, paths...)
return builtin.ExecCommand(python3, args...)
}
func convert2GoTestScripts(paths ...string) error {
log.Warn().Msg("convert to gotest scripts is not supported yet")
sdk.SendEvent(sdk.EventTracking{
Category: "ConvertTests",
Action: "hrp convert --gotest",
})
// 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
}

View File

@@ -0,0 +1,118 @@
package convert
import (
_ "embed"
"fmt"
"os"
"github.com/rs/zerolog/log"
"github.com/httprunner/httprunner/hrp"
"github.com/httprunner/httprunner/hrp/internal/builtin"
"github.com/httprunner/httprunner/hrp/internal/sdk"
)
func Convert2TestScripts(destType string, paths ...string) error {
// report event
sdk.SendEvent(sdk.EventTracking{
Category: "ConvertTests",
Action: fmt.Sprintf("hrp convert --%s", destType),
})
if destType == "gotest" {
return convert2GoTestScripts(paths...)
} else {
// default to pytest
return convert2PyTestScripts(paths...)
}
}
func convert2PyTestScripts(paths ...string) error {
python3, err := builtin.EnsurePython3Venv("httprunner")
if err != nil {
return err
}
args := append([]string{"-m", "httprunner", "make"}, paths...)
return builtin.ExecCommand(python3, args...)
}
func convert2GoTestScripts(paths ...string) error {
log.Warn().Msg("convert to gotest scripts is not supported yet")
os.Exit(1)
// TODO
var testCasePaths []hrp.ITestCase
for _, path := range paths {
testCasePath := hrp.TestCasePath(path)
testCasePaths = append(testCasePaths, &testCasePath)
}
testCases, err := hrp.LoadTestCases(testCasePaths...)
if err != nil {
log.Error().Err(err).Msg("failed to load testcases")
return err
}
var pytestPaths []string
for _, testCase := range testCases {
tc := testCase.ToTCase()
converter := CaseConverter{
TCase: tc,
}
pytestPath, err := converter.ToPyTest()
if err != nil {
log.Error().Err(err).
Str("originPath", tc.Config.Path).
Msg("convert to pytest failed")
continue
}
log.Info().
Str("pytestPath", pytestPath).
Str("originPath", tc.Config.Path).
Msg("convert to pytest success")
pytestPaths = append(pytestPaths, pytestPath)
}
// format pytest scripts with black
python3, err := builtin.EnsurePython3Venv("black")
if err != nil {
return err
}
args := append([]string{"-m", "black"}, pytestPaths...)
return builtin.ExecCommand(python3, args...)
}
//go:embed testcase.tmpl
var testcaseTemplate string
type CaseConverter struct {
*hrp.TCase
}
func (c *CaseConverter) ToPyTest() (string, error) {
script := convertConfig(c.TCase.Config)
println(script)
return script, nil
}
func (c *CaseConverter) ToGoTest() (string, error) {
return "", nil
}
func convertConfig(config *hrp.TConfig) string {
script := fmt.Sprintf("Config('%s')", config.Name)
if config.Variables != nil {
script += fmt.Sprintf(".variables(**{%v})", config.Variables)
}
if config.BaseURL != "" {
script += fmt.Sprintf(".base_url('%s')", config.BaseURL)
}
if config.Export != nil {
script += fmt.Sprintf(".export(*%v)", config.Export)
}
script += fmt.Sprintf(".verify(%v)", config.Verify)
return script
}

View File

@@ -0,0 +1,38 @@
# NOTE: Generated By HttpRunner v{{ version }}
# FROM: {{ testcase_path }}
{% if imports_list and diff_levels > 0 %}
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__){% for _ in range(diff_levels) %}.parent{% endfor %}))
{% endif %}
{% if parameters %}
import pytest
from httprunner import Parameters
{% endif %}
from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase
{% for import_str in imports_list %}
{{ import_str }}
{% endfor %}
class {{ class_name }}(HttpRunner):
{% if parameters %}
@pytest.mark.parametrize("param", Parameters({{parameters}}))
def test_start(self, param):
super().test_start(param)
{% endif %}
config = {{ config_chain_style }}
teststeps = [
{% for step_chain_style in teststeps_chain_style %}
{{ step_chain_style }},
{% endfor %}
]
if __name__ == "__main__":
{{ class_name }}().test_start()

View File

@@ -151,7 +151,7 @@ func (r *HRPRunner) Run(testcases ...ITestCase) error {
s := newOutSummary()
// load all testcases
testCases, err := loadTestCases(testcases...)
testCases, err := LoadTestCases(testcases...)
if err != nil {
log.Error().Err(err).Msg("failed to load testcases")
return err

View File

@@ -203,7 +203,7 @@ func TestRunCaseWithRefAPI(t *testing.T) {
func TestLoadTestCases(t *testing.T) {
// load test cases from folder path
tc := TestCasePath("../examples/demo-with-py-plugin/testcases/")
testCases, err := loadTestCases(&tc)
testCases, err := LoadTestCases(&tc)
if !assert.Nil(t, err) {
t.Fatal()
}
@@ -213,7 +213,7 @@ func TestLoadTestCases(t *testing.T) {
// load test cases from folder path, including sub folders
tc = TestCasePath("../examples/demo-with-py-plugin/")
testCases, err = loadTestCases(&tc)
testCases, err = LoadTestCases(&tc)
if !assert.Nil(t, err) {
t.Fatal()
}
@@ -223,7 +223,7 @@ func TestLoadTestCases(t *testing.T) {
// load test cases from single file path
tc = demoTestCaseWithPluginJSONPath
testCases, err = loadTestCases(&tc)
testCases, err = LoadTestCases(&tc)
if !assert.Nil(t, err) {
t.Fatal()
}
@@ -235,7 +235,7 @@ func TestLoadTestCases(t *testing.T) {
testcase := &TestCase{
Config: NewConfig("TestCase").SetWeight(3),
}
testCases, err = loadTestCases(testcase)
testCases, err = LoadTestCases(testcase)
if !assert.Nil(t, err) {
t.Fatal()
}

View File

@@ -240,7 +240,7 @@ func convertCheckExpr(checkExpr string) string {
return strings.Join(checkItems, ".")
}
func loadTestCases(iTestCases ...ITestCase) ([]*TestCase, error) {
func LoadTestCases(iTestCases ...ITestCase) ([]*TestCase, error) {
testCases := make([]*TestCase, 0)
for _, iTestCase := range iTestCases {