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

View File

@@ -4,51 +4,15 @@ import (
_ "embed"
"os"
"github.com/httprunner/funplugin/myexec"
"github.com/rs/zerolog/log"
"github.com/httprunner/httprunner/v4/hrp"
)
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 := 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
return myexec.ExecPython3Command("black", pytestPaths...)
return nil
}
//go:embed testcase.tmpl

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
if rendezvous.isReleased() || !isPreRendezvousAllReleased(rendezvous, r.caseRunner.testCase.ToTCase()) || !rendezvous.isSpawnDone() {
if rendezvous.isReleased() || !isPreRendezvousAllReleased(rendezvous, r.caseRunner.testCase) || !rendezvous.isSpawnDone() {
return stepResult, nil
}
@@ -69,8 +69,8 @@ func (s *StepRendezvous) Run(r *SessionRunner) (*StepResult, error) {
return stepResult, nil
}
func isPreRendezvousAllReleased(rendezvous *Rendezvous, testCase *TCase) bool {
for _, step := range testCase.TestSteps {
func isPreRendezvousAllReleased(rendezvous *Rendezvous, testCase *TestCase) bool {
for _, step := range testCase.TSteps {
preRendezvous := step.Rendezvous
if preRendezvous == nil {
continue

View File

@@ -24,8 +24,9 @@ type ITestCase interface {
// TestCase is a container for one testcase, which is used for testcase runner.
// TestCase implements ITestCase interface.
type TestCase struct {
Config *TConfig
TestSteps []IStep
Config *TConfig `json:"config" yaml:"config"`
TSteps []*TStep `json:"teststeps" yaml:"teststeps"`
TestSteps []IStep `json:"-" yaml:"-"`
}
func (tc *TestCase) GetPath() string {
@@ -36,24 +37,21 @@ func (tc *TestCase) ToTestCase() (*TestCase, error) {
return tc, nil
}
func (tc *TestCase) ToTCase() *TCase {
tCase := &TCase{
Config: tc.Config,
}
func (tc *TestCase) loadStruct() {
tc.TSteps = make([]*TStep, 0)
for _, step := range tc.TestSteps {
if step.Type() == stepTypeTestCase {
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 {
tCase := tc.ToTCase()
err := builtin.Dump2JSON(tCase, targetPath)
tc.loadStruct()
err := builtin.Dump2JSON(tc, targetPath)
if err != nil {
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 {
tCase := tc.ToTCase()
err := builtin.Dump2YAML(tCase, targetPath)
tc.loadStruct()
err := builtin.Dump2YAML(tc, targetPath)
if err != nil {
return errors.Wrap(err, "dump testcase to yaml failed")
}

View File

@@ -156,30 +156,28 @@ var demoTestCaseWithoutPlugin = &TestCase{
}
func TestGenDemoTestCase(t *testing.T) {
tCase := demoTestCaseWithPlugin.ToTCase()
err := builtin.Dump2JSON(tCase, demoTestCaseWithPluginJSONPath)
err := demoTestCaseWithPlugin.Dump2JSON(demoTestCaseWithPluginJSONPath)
if err != nil {
t.Fatal()
}
err = builtin.Dump2YAML(tCase, demoTestCaseWithPluginYAMLPath)
err = demoTestCaseWithPlugin.Dump2YAML(demoTestCaseWithPluginYAMLPath)
if err != nil {
t.Fatal()
}
tCase = demoTestCaseWithoutPlugin.ToTCase()
err = builtin.Dump2JSON(tCase, demoTestCaseWithoutPluginJSONPath)
err = demoTestCaseWithoutPlugin.Dump2JSON(demoTestCaseWithoutPluginJSONPath)
if err != nil {
t.Fatal()
}
err = builtin.Dump2YAML(tCase, demoTestCaseWithoutPluginYAMLPath)
err = demoTestCaseWithoutPlugin.Dump2YAML(demoTestCaseWithoutPluginYAMLPath)
if err != nil {
t.Fatal()
}
}
func TestLoadCase(t *testing.T) {
tcJSON := &TCase{}
tcYAML := &TCase{}
tcJSON := &TestCase{}
tcYAML := &TestCase{}
err := builtin.LoadFile(demoTestCaseWithPluginJSONPath, tcJSON)
if !assert.NoError(t, err) {
t.Fatal()
@@ -195,10 +193,10 @@ func TestLoadCase(t *testing.T) {
if !assert.Equal(t, tcJSON.Config.BaseURL, tcYAML.Config.BaseURL) {
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()
}
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()
}
}