mirror of
https://github.com/httprunner/httprunner.git
synced 2026-06-07 16:59:34 +08:00
feat: record execution data for report #25
This commit is contained in:
@@ -16,7 +16,7 @@ var Assertions = map[string]func(t assert.TestingT, expected interface{}, actual
|
||||
"greater_or_equals": assert.GreaterOrEqual,
|
||||
"less_or_equals": assert.LessOrEqual,
|
||||
"not_equal": assert.NotEqual,
|
||||
"contained_by": assert.Contains,
|
||||
"contained_by": assert.Contains,
|
||||
"regex_match": assert.Regexp,
|
||||
"type_match": assert.IsType,
|
||||
// custom assertions
|
||||
@@ -28,7 +28,7 @@ var Assertions = map[string]func(t assert.TestingT, expected interface{}, actual
|
||||
"length_less_or_equals": LessOrEqualsLength,
|
||||
"length_greater_than": GreaterThanLength,
|
||||
"length_greater_or_equals": GreaterOrEqualsLength,
|
||||
"contains": Contains,
|
||||
"contains": Contains,
|
||||
"string_equals": EqualString,
|
||||
}
|
||||
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
package builtin
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/md5"
|
||||
"encoding/csv"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"math"
|
||||
"math/rand"
|
||||
@@ -11,6 +13,8 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
@@ -19,7 +23,7 @@ var Functions = map[string]interface{}{
|
||||
"sleep": sleep, // call with one argument
|
||||
"gen_random_string": genRandomString, // call with one argument
|
||||
"max": math.Max, // call with two arguments
|
||||
"md5": MD5, // call with one argument
|
||||
"md5": MD5, // call with one argument
|
||||
"parameterize": loadFromCSV,
|
||||
"P": loadFromCSV,
|
||||
}
|
||||
@@ -82,3 +86,46 @@ func loadFromCSV(path string) []map[string]interface{} {
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func Dump2JSON(data interface{}, path string) error {
|
||||
path, err := filepath.Abs(path)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("convert absolute path failed")
|
||||
return err
|
||||
}
|
||||
log.Info().Str("path", path).Msg("dump data to json")
|
||||
file, _ := json.MarshalIndent(data, "", " ")
|
||||
err = ioutil.WriteFile(path, file, 0644)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("dump json path failed")
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func Dump2YAML(data interface{}, path string) error {
|
||||
path, err := filepath.Abs(path)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("convert absolute path failed")
|
||||
return err
|
||||
}
|
||||
log.Info().Str("path", path).Msg("dump data to yaml")
|
||||
|
||||
// init yaml encoder
|
||||
buffer := new(bytes.Buffer)
|
||||
encoder := yaml.NewEncoder(buffer)
|
||||
encoder.SetIndent(4)
|
||||
|
||||
// encode
|
||||
err = encoder.Encode(data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = ioutil.WriteFile(path, buffer.Bytes(), 0644)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("dump yaml path failed")
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@ import (
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
"github.com/httprunner/hrp"
|
||||
"github.com/httprunner/hrp/internal/builtin"
|
||||
"github.com/httprunner/hrp/internal/ga"
|
||||
)
|
||||
|
||||
@@ -55,7 +56,7 @@ func (h *har) GenJSON() (jsonPath string, err error) {
|
||||
return "", err
|
||||
}
|
||||
jsonPath = h.genOutputPath(suffixJSON)
|
||||
err = tCase.Dump2JSON(jsonPath)
|
||||
err = builtin.Dump2JSON(tCase, jsonPath)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -74,7 +75,7 @@ func (h *har) GenYAML() (yamlPath string, err error) {
|
||||
return "", err
|
||||
}
|
||||
yamlPath = h.genOutputPath(suffixYAML)
|
||||
err = tCase.Dump2YAML(yamlPath)
|
||||
err = builtin.Dump2YAML(tCase, yamlPath)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
"github.com/httprunner/hrp"
|
||||
"github.com/httprunner/hrp/internal/builtin"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -32,11 +33,11 @@ func removeHashicorpPlugin() {
|
||||
|
||||
func TestGenDemoTestCase(t *testing.T) {
|
||||
tCase, _ := demoTestCase.ToTCase()
|
||||
err := tCase.Dump2JSON(demoTestCaseJSONPath)
|
||||
err := builtin.Dump2JSON(tCase, demoTestCaseJSONPath)
|
||||
if err != nil {
|
||||
t.Fail()
|
||||
}
|
||||
err = tCase.Dump2YAML(demoTestCaseYAMLPath)
|
||||
err = builtin.Dump2YAML(tCase, demoTestCaseYAMLPath)
|
||||
if err != nil {
|
||||
t.Fail()
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"github.com/httprunner/hrp/internal/builtin"
|
||||
"github.com/httprunner/hrp/internal/ga"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
@@ -48,12 +49,12 @@ func CreateScaffold(projectName string) error {
|
||||
|
||||
// create demo testcases
|
||||
tCase, _ := demoTestCase.ToTCase()
|
||||
err := tCase.Dump2JSON(path.Join(projectName, "testcases", "demo.json"))
|
||||
err := builtin.Dump2JSON(tCase, path.Join(projectName, "testcases", "demo.json"))
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("create demo.json testcase failed")
|
||||
return err
|
||||
}
|
||||
err = tCase.Dump2YAML(path.Join(projectName, "testcases", "demo.yaml"))
|
||||
err = builtin.Dump2YAML(tCase, path.Join(projectName, "testcases", "demo.yaml"))
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("create demo.yml testcase failed")
|
||||
return err
|
||||
|
||||
Reference in New Issue
Block a user