MCPcopy
hub / github.com/httprunner/httprunner / toTestCase

Method toTestCase

hrp/testcase.go:141–290  ·  view source on GitHub ↗

toTestCase converts *TCase to *TestCase

()

Source from the content-addressed store, hash-verified

139
140// toTestCase converts *TCase to *TestCase
141func (tc *TCase) toTestCase() (*TestCase, error) {
142 testCase := &TestCase{
143 Config: tc.Config,
144 }
145
146 err := tc.MakeCompat()
147 if err != nil {
148 return nil, err
149 }
150
151 // locate project root dir by plugin path
152 projectRootDir, err := GetProjectRootDirPath(tc.Config.Path)
153 if err != nil {
154 return nil, errors.Wrap(err, "failed to get project root dir")
155 }
156
157 // load .env file
158 dotEnvPath := filepath.Join(projectRootDir, ".env")
159 if builtin.IsFilePathExists(dotEnvPath) {
160 envVars := make(map[string]string)
161 err = builtin.LoadFile(dotEnvPath, envVars)
162 if err != nil {
163 return nil, errors.Wrap(err, "failed to load .env file")
164 }
165
166 // override testcase config env with variables loaded from .env file
167 // priority: .env file > testcase config env
168 if testCase.Config.Environs == nil {
169 testCase.Config.Environs = make(map[string]string)
170 }
171 for key, value := range envVars {
172 testCase.Config.Environs[key] = value
173 }
174 }
175
176 for _, step := range tc.TestSteps {
177 if step.API != nil {
178 apiPath, ok := step.API.(string)
179 if ok {
180 path := filepath.Join(projectRootDir, apiPath)
181 if !builtin.IsFilePathExists(path) {
182 return nil, errors.Wrap(code.ReferencedFileNotFound,
183 fmt.Sprintf("referenced api file not found: %s", path))
184 }
185
186 refAPI := APIPath(path)
187 apiContent, err := refAPI.ToAPI()
188 if err != nil {
189 return nil, err
190 }
191 step.API = apiContent
192 } else {
193 apiMap, ok := step.API.(map[string]interface{})
194 if !ok {
195 return nil, errors.Wrap(code.InvalidCaseFormat,
196 fmt.Sprintf("referenced api should be map or path(string), got %v", step.API))
197 }
198 api := &API{}

Callers 2

ToTestCaseMethod · 0.95
parseTCasesMethod · 0.80

Calls 9

MakeCompatMethod · 0.95
IsFilePathExistsFunction · 0.92
LoadFileFunction · 0.92
GetProjectRootDirPathFunction · 0.85
APIPathTypeAlias · 0.85
TestCasePathTypeAlias · 0.85
initUploadFunction · 0.85
ToAPIMethod · 0.65
ToTestCaseMethod · 0.65

Tested by

no test coverage detected