fix: unittest with failfast

This commit is contained in:
debugtalk
2022-04-16 00:36:38 +08:00
parent fe2c2f170e
commit 8ebb3634fb
14 changed files with 154 additions and 147 deletions

View File

@@ -109,6 +109,6 @@ func TestLoopCount(t *testing.T) {
go runner.start()
<-runner.stopChan
if !assert.Equal(t, runner.loop.loopCount, atomic.LoadInt64(&runner.loop.finishedCount)) {
t.Fail()
t.Fatal()
}
}

View File

@@ -155,7 +155,7 @@ func TestSerializeStats(t *testing.T) {
first := serialized[0]
entry, err := deserializeStatsEntry(first)
if err != nil {
t.Fail()
t.Fatal()
}
if entry.Name != "success" {

View File

@@ -20,7 +20,7 @@ func TestStartsWith(t *testing.T) {
for _, data := range testData {
if !assert.True(t, StartsWith(t, data.raw, data.expected)) {
t.Fail()
t.Fatal()
}
}
}
@@ -38,7 +38,7 @@ func TestEndsWith(t *testing.T) {
for _, data := range testData {
if !assert.True(t, EndsWith(t, data.raw, data.expected)) {
t.Fail()
t.Fatal()
}
}
}
@@ -58,7 +58,7 @@ func TestEqualLength(t *testing.T) {
for _, data := range testData {
if !assert.True(t, EqualLength(t, data.raw, data.expected)) {
t.Fail()
t.Fatal()
}
}
}
@@ -78,7 +78,7 @@ func TestLessThanLength(t *testing.T) {
for _, data := range testData {
if !assert.True(t, LessThanLength(t, data.raw, data.expected)) {
t.Fail()
t.Fatal()
}
}
}
@@ -98,7 +98,7 @@ func TestLessOrEqualsLength(t *testing.T) {
for _, data := range testData {
if !assert.True(t, LessOrEqualsLength(t, data.raw, data.expected)) {
t.Fail()
t.Fatal()
}
}
}
@@ -115,7 +115,7 @@ func TestGreaterThanLength(t *testing.T) {
for _, data := range testData {
if !assert.True(t, GreaterThanLength(t, data.raw, data.expected)) {
t.Fail()
t.Fatal()
}
}
}
@@ -135,7 +135,7 @@ func TestGreaterOrEqualsLength(t *testing.T) {
for _, data := range testData {
if !assert.True(t, GreaterOrEqualsLength(t, data.raw, data.expected)) {
t.Fail()
t.Fatal()
}
}
}
@@ -152,7 +152,7 @@ func TestContainedBy(t *testing.T) {
for _, data := range testData {
if !assert.True(t, ContainedBy(t, data.raw, data.expected)) {
t.Fail()
t.Fatal()
}
}
}
@@ -169,7 +169,7 @@ func TestStringEqual(t *testing.T) {
for _, data := range testData {
if !assert.True(t, StringEqual(t, data.raw, data.expected)) {
t.Fail()
t.Fatal()
}
}
}
@@ -185,7 +185,7 @@ func TestRegexMatch(t *testing.T) {
for _, data := range testData {
if !assert.True(t, RegexMatch(t, data.raw, data.expected)) {
t.Fail()
t.Fatal()
}
}
}

View File

@@ -270,11 +270,12 @@ func loadFromCSV(path string) []map[string]interface{} {
log.Error().Err(err).Msg("parse csv file failed")
os.Exit(1)
}
firstLine := content[0] // parameter names
var result []map[string]interface{}
for i := 1; i < len(content); i++ {
row := make(map[string]interface{})
for j := 0; j < len(content[i]); j++ {
row[content[0][j]] = content[i][j]
row[firstLine[j]] = content[i][j]
}
result = append(result, row)
}

View File

@@ -17,20 +17,20 @@ var (
func TestGenJSON(t *testing.T) {
jsonPath, err := NewHAR(harPath).GenJSON()
if !assert.NoError(t, err) {
t.Fail()
t.Fatal()
}
if !assert.NotEmpty(t, jsonPath) {
t.Fail()
t.Fatal()
}
}
func TestGenYAML(t *testing.T) {
yamlPath, err := NewHAR(harPath2).GenYAML()
if !assert.NoError(t, err) {
t.Fail()
t.Fatal()
}
if !assert.NotEmpty(t, yamlPath) {
t.Fail()
t.Fatal()
}
}
@@ -38,13 +38,13 @@ func TestLoadHAR(t *testing.T) {
har := NewHAR(harPath)
h, err := har.load()
if !assert.NoError(t, err) {
t.Fail()
t.Fatal()
}
if !assert.Equal(t, "GET", h.Log.Entries[0].Request.Method) {
t.Fail()
t.Fatal()
}
if !assert.Equal(t, "POST", h.Log.Entries[1].Request.Method) {
t.Fail()
t.Fatal()
}
}
@@ -53,18 +53,18 @@ func TestLoadHARWithProfile(t *testing.T) {
har.SetProfile(profilePath)
_, err := har.load()
if !assert.NoError(t, err) {
t.Fail()
t.Fatal()
}
if !assert.Equal(t,
map[string]interface{}{"Content-Type": "application/x-www-form-urlencoded"},
har.profile["headers"]) {
t.Fail()
t.Fatal()
}
if !assert.Equal(t,
map[string]interface{}{"UserName": "debugtalk"},
har.profile["cookies"]) {
t.Fail()
t.Fatal()
}
}
@@ -72,73 +72,73 @@ func TestMakeTestCase(t *testing.T) {
har := NewHAR(harPath)
tCase, err := har.makeTestCase()
if !assert.NoError(t, err) {
t.Fail()
t.Fatal()
}
// make request method
if !assert.EqualValues(t, "GET", tCase.TestSteps[0].Request.Method) {
t.Fail()
t.Fatal()
}
if !assert.EqualValues(t, "POST", tCase.TestSteps[1].Request.Method) {
t.Fail()
t.Fatal()
}
// make request url
if !assert.Equal(t, "https://postman-echo.com/get", tCase.TestSteps[0].Request.URL) {
t.Fail()
t.Fatal()
}
if !assert.Equal(t, "https://postman-echo.com/post", tCase.TestSteps[1].Request.URL) {
t.Fail()
t.Fatal()
}
// make request params
if !assert.Equal(t, "HDnY8", tCase.TestSteps[0].Request.Params["foo1"]) {
t.Fail()
t.Fatal()
}
// make request cookies
if !assert.NotEmpty(t, tCase.TestSteps[1].Request.Cookies["sails.sid"]) {
t.Fail()
t.Fatal()
}
// make request headers
if !assert.Equal(t, "HttpRunnerPlus", tCase.TestSteps[0].Request.Headers["User-Agent"]) {
t.Fail()
t.Fatal()
}
if !assert.Equal(t, "postman-echo.com", tCase.TestSteps[0].Request.Headers["Host"]) {
t.Fail()
t.Fatal()
}
// make request data
if !assert.Equal(t, nil, tCase.TestSteps[0].Request.Body) {
t.Fail()
t.Fatal()
}
if !assert.Equal(t, map[string]interface{}{"foo1": "HDnY8", "foo2": 12.3}, tCase.TestSteps[1].Request.Body) {
t.Fail()
t.Fatal()
}
if !assert.Equal(t, "foo1=HDnY8&foo2=12.3", tCase.TestSteps[2].Request.Body) {
t.Fail()
t.Fatal()
}
// make validators
validator, ok := tCase.TestSteps[0].Validators[0].(hrp.Validator)
if !ok || !assert.Equal(t, "status_code", validator.Check) {
t.Fail()
t.Fatal()
}
validator, ok = tCase.TestSteps[0].Validators[1].(hrp.Validator)
if !ok || !assert.Equal(t, "headers.\"Content-Type\"", validator.Check) {
t.Fail()
t.Fatal()
}
validator, ok = tCase.TestSteps[0].Validators[2].(hrp.Validator)
if !ok || !assert.Equal(t, "body.url", validator.Check) {
t.Fail()
t.Fatal()
}
}
func TestGetFilenameWithoutExtension(t *testing.T) {
filename := getFilenameWithoutExtension(harPath2)
if !assert.Equal(t, "postman-echo", filename) {
t.Fail()
t.Fatal()
}
}
@@ -154,13 +154,13 @@ func TestMakeRequestHeaders(t *testing.T) {
}
step, err := har.prepareTestStep(entry)
if !assert.NoError(t, err) {
t.Fail()
t.Fatal()
}
if !assert.Equal(t, map[string]string{
"Content-Type": "application/json; charset=utf-8",
}, step.Request.Headers) {
t.Fail()
t.Fatal()
}
}
@@ -177,13 +177,13 @@ func TestMakeRequestHeadersWithProfile(t *testing.T) {
}
step, err := har.prepareTestStep(entry)
if !assert.NoError(t, err) {
t.Fail()
t.Fatal()
}
if !assert.Equal(t, map[string]string{
"Content-Type": "application/x-www-form-urlencoded",
}, step.Request.Headers) {
t.Fail()
t.Fatal()
}
}
@@ -200,14 +200,14 @@ func TestMakeRequestCookies(t *testing.T) {
}
step, err := har.prepareTestStep(entry)
if !assert.NoError(t, err) {
t.Fail()
t.Fatal()
}
if !assert.Equal(t, map[string]string{
"abc": "123",
"UserName": "leolee",
}, step.Request.Cookies) {
t.Fail()
t.Fatal()
}
}
@@ -225,13 +225,13 @@ func TestMakeRequestCookiesWithProfile(t *testing.T) {
}
step, err := har.prepareTestStep(entry)
if !assert.NoError(t, err) {
t.Fail()
t.Fatal()
}
if !assert.Equal(t, map[string]string{
"UserName": "debugtalk",
}, step.Request.Cookies) {
t.Fail()
t.Fatal()
}
}
@@ -251,11 +251,11 @@ func TestMakeRequestDataParams(t *testing.T) {
}
step, err := har.prepareTestStep(entry)
if !assert.NoError(t, err) {
t.Fail()
t.Fatal()
}
if !assert.Equal(t, "a=1&b=2", step.Request.Body) {
t.Fail()
t.Fatal()
}
}
@@ -272,11 +272,11 @@ func TestMakeRequestDataJSON(t *testing.T) {
}
step, err := har.prepareTestStep(entry)
if !assert.NoError(t, err) {
t.Fail()
t.Fatal()
}
if !assert.Equal(t, map[string]interface{}{"a": "1", "b": "2"}, step.Request.Body) {
t.Fail()
t.Fatal()
}
}
@@ -293,11 +293,11 @@ func TestMakeRequestDataTextEmpty(t *testing.T) {
}
step, err := har.prepareTestStep(entry)
if !assert.NoError(t, err) {
t.Fail()
t.Fatal()
}
if !assert.Equal(t, nil, step.Request.Body) { // TODO
t.Fail()
t.Fatal()
}
}
@@ -320,11 +320,11 @@ func TestMakeValidate(t *testing.T) {
}
step, err := har.prepareTestStep(entry)
if !assert.NoError(t, err) {
t.Fail()
t.Fatal()
}
validator, ok := step.Validators[0].(hrp.Validator)
if !ok {
t.Fail()
t.Fatal()
}
if !assert.Equal(t, validator,
hrp.Validator{
@@ -332,12 +332,12 @@ func TestMakeValidate(t *testing.T) {
Expect: 200,
Assert: "equals",
Message: "assert response status code"}) {
t.Fail()
t.Fatal()
}
validator, ok = step.Validators[1].(hrp.Validator)
if !ok {
t.Fail()
t.Fatal()
}
if !assert.Equal(t, validator,
hrp.Validator{
@@ -345,12 +345,12 @@ func TestMakeValidate(t *testing.T) {
Expect: "application/json; charset=utf-8",
Assert: "equals",
Message: "assert response header Content-Type"}) {
t.Fail()
t.Fatal()
}
validator, ok = step.Validators[2].(hrp.Validator)
if !ok {
t.Fail()
t.Fatal()
}
if !assert.Equal(t, validator,
hrp.Validator{
@@ -358,6 +358,6 @@ func TestMakeValidate(t *testing.T) {
Expect: float64(200), // TODO
Assert: "equals",
Message: "assert response body Code"}) {
t.Fail()
t.Fatal()
}
}

View File

@@ -10,20 +10,20 @@ func TestGenDemoExamples(t *testing.T) {
os.RemoveAll(dir)
err := CreateScaffold(dir, Go)
if err != nil {
t.Fail()
t.Fatal()
}
dir = "../../../examples/demo-with-py-plugin"
os.RemoveAll(dir)
err = CreateScaffold(dir, Py)
if err != nil {
t.Fail()
t.Fatal()
}
dir = "../../../examples/demo-without-plugin"
os.RemoveAll(dir)
err = CreateScaffold(dir, Ignore)
if err != nil {
t.Fail()
t.Fatal()
}
}

View File

@@ -0,0 +1,6 @@
[pytest]
addopts = -s
# https://docs.pytest.org/en/latest/how-to/output.html
junit_logging = all
junit_duration_report = total
log_cli = False

View File

@@ -25,6 +25,6 @@ func TestStructToUrlValues(t *testing.T) {
}
val := structToUrlValues(event)
if val.Encode() != "ea=convert&ec=unittest&el=v0.3.0&ev=123" {
t.Fail()
t.Fatal()
}
}