From 9f06414d18830954a2ed1573b445dff992a8c538 Mon Sep 17 00:00:00 2001 From: buyuxiang <347586493@qq.com> Date: Thu, 23 Jun 2022 11:46:26 +0800 Subject: [PATCH] add upload unittest --- hrp/tests/test.env | 4 ++++ hrp/tests/upload_test.go | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 hrp/tests/test.env create mode 100644 hrp/tests/upload_test.go diff --git a/hrp/tests/test.env b/hrp/tests/test.env new file mode 100644 index 00000000..74d5d9ec --- /dev/null +++ b/hrp/tests/test.env @@ -0,0 +1,4 @@ +UserName=test +Password=654321 +PROJECT_KEY=AAABBBCCC +content_type=application/json; charset=UTF-8 \ No newline at end of file diff --git a/hrp/tests/upload_test.go b/hrp/tests/upload_test.go new file mode 100644 index 00000000..d9273d17 --- /dev/null +++ b/hrp/tests/upload_test.go @@ -0,0 +1,38 @@ +package tests + +import ( + "testing" + + "github.com/httprunner/httprunner/v4/hrp" +) + +func TestCaseUploadFile(t *testing.T) { + testcase := &hrp.TestCase{ + Config: hrp.NewConfig("test upload file to httpbin"). + SetBaseURL("https://httpbin.org"), + TestSteps: []hrp.IStep{ + hrp.NewStep("upload file"). + WithVariables(map[string]interface{}{ + "m_encoder": "${multipart_encoder($m_upload)}", + "m_upload": map[string]interface{}{"file": "test.env"}, + }). + POST("/post"). + WithHeaders(map[string]string{"Content-Type": "${multipart_content_type($m_encoder)}"}). + WithBody("$m_encoder"). + Validate(). + AssertEqual("status_code", 200, "check status code"). + AssertStartsWith("body.files.file", "UserName=test", "check uploaded file"), + hrp.NewStep("upload file with keyword"). + POST("/post"). + WithUpload(map[string]interface{}{"file": "test.env"}). + Validate(). + AssertEqual("status_code", 200, "check status code"). + AssertStartsWith("body.files.file", "UserName=test", "check uploaded file"), + }, + } + + err := hrp.NewRunner(t).Run(testcase) + if err != nil { + t.Fatalf("run testcase error: %v", err) + } +}