refactor: merge TestCase with TCase, loadStruct

This commit is contained in:
lilong.129
2024-08-19 19:24:00 +08:00
parent ff4aa816da
commit 448a0bbc67
4 changed files with 23 additions and 63 deletions
+1 -37
View File
@@ -4,51 +4,15 @@ import (
_ "embed" _ "embed"
"os" "os"
"github.com/httprunner/funplugin/myexec"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/httprunner/httprunner/v4/hrp"
) )
func convert2GoTestScripts(paths ...string) error { func convert2GoTestScripts(paths ...string) error {
log.Warn().Msg("convert to gotest scripts is not supported yet") log.Warn().Msg("convert to gotest scripts is not supported yet")
os.Exit(1) 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 := TCaseConverter{
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 // format pytest scripts with black
return myexec.ExecPython3Command("black", pytestPaths...) return nil
} }
//go:embed testcase.tmpl //go:embed testcase.tmpl
+3 -3
View File
@@ -44,7 +44,7 @@ func (s *StepRendezvous) Run(r *SessionRunner) (*StepResult, error) {
} }
// pass current rendezvous if already released, activate rendezvous sequentially after spawn done // pass current rendezvous if already released, activate rendezvous sequentially after spawn done
if rendezvous.isReleased() || !isPreRendezvousAllReleased(rendezvous, r.caseRunner.testCase.ToTCase()) || !rendezvous.isSpawnDone() { if rendezvous.isReleased() || !isPreRendezvousAllReleased(rendezvous, r.caseRunner.testCase) || !rendezvous.isSpawnDone() {
return stepResult, nil return stepResult, nil
} }
@@ -69,8 +69,8 @@ func (s *StepRendezvous) Run(r *SessionRunner) (*StepResult, error) {
return stepResult, nil return stepResult, nil
} }
func isPreRendezvousAllReleased(rendezvous *Rendezvous, testCase *TCase) bool { func isPreRendezvousAllReleased(rendezvous *Rendezvous, testCase *TestCase) bool {
for _, step := range testCase.TestSteps { for _, step := range testCase.TSteps {
preRendezvous := step.Rendezvous preRendezvous := step.Rendezvous
if preRendezvous == nil { if preRendezvous == nil {
continue continue
+11 -13
View File
@@ -24,8 +24,9 @@ type ITestCase interface {
// TestCase is a container for one testcase, which is used for testcase runner. // TestCase is a container for one testcase, which is used for testcase runner.
// TestCase implements ITestCase interface. // TestCase implements ITestCase interface.
type TestCase struct { type TestCase struct {
Config *TConfig Config *TConfig `json:"config" yaml:"config"`
TestSteps []IStep TSteps []*TStep `json:"teststeps" yaml:"teststeps"`
TestSteps []IStep `json:"-" yaml:"-"`
} }
func (tc *TestCase) GetPath() string { func (tc *TestCase) GetPath() string {
@@ -36,24 +37,21 @@ func (tc *TestCase) ToTestCase() (*TestCase, error) {
return tc, nil return tc, nil
} }
func (tc *TestCase) ToTCase() *TCase { func (tc *TestCase) loadStruct() {
tCase := &TCase{ tc.TSteps = make([]*TStep, 0)
Config: tc.Config,
}
for _, step := range tc.TestSteps { for _, step := range tc.TestSteps {
if step.Type() == stepTypeTestCase { if step.Type() == stepTypeTestCase {
if testcase, ok := step.Struct().TestCase.(*TestCase); ok { if testcase, ok := step.Struct().TestCase.(*TestCase); ok {
step.Struct().TestCase = testcase.ToTCase() step.Struct().TestCase = testcase
} }
} }
tCase.TestSteps = append(tCase.TestSteps, step.Struct()) tc.TSteps = append(tc.TSteps, step.Struct())
} }
return tCase
} }
func (tc *TestCase) Dump2JSON(targetPath string) error { func (tc *TestCase) Dump2JSON(targetPath string) error {
tCase := tc.ToTCase() tc.loadStruct()
err := builtin.Dump2JSON(tCase, targetPath) err := builtin.Dump2JSON(tc, targetPath)
if err != nil { if err != nil {
return errors.Wrap(err, "dump testcase to json failed") return errors.Wrap(err, "dump testcase to json failed")
} }
@@ -61,8 +59,8 @@ func (tc *TestCase) Dump2JSON(targetPath string) error {
} }
func (tc *TestCase) Dump2YAML(targetPath string) error { func (tc *TestCase) Dump2YAML(targetPath string) error {
tCase := tc.ToTCase() tc.loadStruct()
err := builtin.Dump2YAML(tCase, targetPath) err := builtin.Dump2YAML(tc, targetPath)
if err != nil { if err != nil {
return errors.Wrap(err, "dump testcase to yaml failed") return errors.Wrap(err, "dump testcase to yaml failed")
} }
+8 -10
View File
@@ -156,30 +156,28 @@ var demoTestCaseWithoutPlugin = &TestCase{
} }
func TestGenDemoTestCase(t *testing.T) { func TestGenDemoTestCase(t *testing.T) {
tCase := demoTestCaseWithPlugin.ToTCase() err := demoTestCaseWithPlugin.Dump2JSON(demoTestCaseWithPluginJSONPath)
err := builtin.Dump2JSON(tCase, demoTestCaseWithPluginJSONPath)
if err != nil { if err != nil {
t.Fatal() t.Fatal()
} }
err = builtin.Dump2YAML(tCase, demoTestCaseWithPluginYAMLPath) err = demoTestCaseWithPlugin.Dump2YAML(demoTestCaseWithPluginYAMLPath)
if err != nil { if err != nil {
t.Fatal() t.Fatal()
} }
tCase = demoTestCaseWithoutPlugin.ToTCase() err = demoTestCaseWithoutPlugin.Dump2JSON(demoTestCaseWithoutPluginJSONPath)
err = builtin.Dump2JSON(tCase, demoTestCaseWithoutPluginJSONPath)
if err != nil { if err != nil {
t.Fatal() t.Fatal()
} }
err = builtin.Dump2YAML(tCase, demoTestCaseWithoutPluginYAMLPath) err = demoTestCaseWithoutPlugin.Dump2YAML(demoTestCaseWithoutPluginYAMLPath)
if err != nil { if err != nil {
t.Fatal() t.Fatal()
} }
} }
func TestLoadCase(t *testing.T) { func TestLoadCase(t *testing.T) {
tcJSON := &TCase{} tcJSON := &TestCase{}
tcYAML := &TCase{} tcYAML := &TestCase{}
err := builtin.LoadFile(demoTestCaseWithPluginJSONPath, tcJSON) err := builtin.LoadFile(demoTestCaseWithPluginJSONPath, tcJSON)
if !assert.NoError(t, err) { if !assert.NoError(t, err) {
t.Fatal() t.Fatal()
@@ -195,10 +193,10 @@ func TestLoadCase(t *testing.T) {
if !assert.Equal(t, tcJSON.Config.BaseURL, tcYAML.Config.BaseURL) { if !assert.Equal(t, tcJSON.Config.BaseURL, tcYAML.Config.BaseURL) {
t.Fatal() t.Fatal()
} }
if !assert.Equal(t, tcJSON.TestSteps[1].Name, tcYAML.TestSteps[1].Name) { if !assert.Equal(t, tcJSON.TSteps[1].Name, tcYAML.TSteps[1].Name) {
t.Fatal() t.Fatal()
} }
if !assert.Equal(t, tcJSON.TestSteps[1].Request, tcYAML.TestSteps[1].Request) { if !assert.Equal(t, tcJSON.TSteps[1].Request, tcYAML.TSteps[1].Request) {
t.Fatal() t.Fatal()
} }
} }