refactor: builtin upload file method

This commit is contained in:
debugtalk
2020-06-03 18:32:31 +08:00
parent 5869b37d50
commit 6410aa726d
4 changed files with 29 additions and 41 deletions

View File

@@ -1,54 +1,35 @@
# NOTICE: Generated By HttpRunner. DO NOT EDIT! # NOTICE: Generated By HttpRunner. DO NOT EDIT!
# FROM: examples/httpbin/upload.yml # FROM: examples/httpbin/upload.yml
from httprunner import HttpRunner, TConfig, TStep from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase
class TestCaseUpload(HttpRunner): class TestCaseUpload(HttpRunner):
config = TConfig( config = Config("test upload file with httpbin").base_url("${get_httpbin_server()}")
**{
"name": "test upload file with httpbin",
"base_url": "${get_httpbin_server()}",
"path": "examples/httpbin/upload_test.py",
"variables": {},
}
)
teststeps = [ teststeps = [
TStep( Step(
**{ RunRequest("upload file")
"name": "upload file", .with_variables(
"variables": { **{
"file_path": "test.env", "file_path": "test.env",
"m_encoder": "${multipart_encoder(file=$file_path)}", "m_encoder": "${multipart_encoder(file=$file_path)}",
}, }
"request": { )
"url": "/post", .post("/post")
"method": "POST", .with_headers(**{"Content-Type": "${multipart_content_type($m_encoder)}"})
"headers": { .with_data("$m_encoder")
"Content-Type": "${multipart_content_type($m_encoder)}" .validate()
}, .assert_equal("status_code", 200)
"data": "$m_encoder", .assert_startswith("body.files.file", "UserName=test")
},
"validate": [
{"eq": ["status_code", 200]},
{"startswith": ["body.files.file", "UserName=test"]},
],
}
), ),
TStep( Step(
**{ RunRequest("upload file with keyword")
"name": "upload file with keyword", .post("/post")
"request": { .upload(**{"file": "test.env"})
"url": "/post", .validate()
"method": "POST", .assert_equal("status_code", 200)
"upload": {"file": "test.env"}, .assert_startswith("body.files.file", "UserName=test")
},
"validate": [
{"eq": ["status_code", 200]},
{"startswith": ["body.files.file", "UserName=test"]},
],
}
), ),
] ]

View File

@@ -48,7 +48,6 @@ def _load_json_file(json_file: Text) -> Dict:
json_content = json.load(data_file) json_content = json.load(data_file)
except json.JSONDecodeError as ex: except json.JSONDecodeError as ex:
err_msg = f"JSONDecodeError:\nfile: {json_file}\nerror: {ex}" err_msg = f"JSONDecodeError:\nfile: {json_file}\nerror: {ex}"
logger.error(err_msg)
raise exceptions.FileFormatError(err_msg) raise exceptions.FileFormatError(err_msg)
return json_content return json_content

View File

@@ -184,6 +184,10 @@ def make_request_chain_style(request: Dict) -> Text:
allow_redirects = request["allow_redirects"] allow_redirects = request["allow_redirects"]
request_chain_style += f".set_allow_redirects({allow_redirects})" request_chain_style += f".set_allow_redirects({allow_redirects})"
if "upload" in request:
upload = request["upload"]
request_chain_style += f".upload(**{upload})"
return request_chain_style return request_chain_style

View File

@@ -227,6 +227,10 @@ class RequestWithOptionalArgs(object):
self.__t_step.request.allow_redirects = allow_redirects self.__t_step.request.allow_redirects = allow_redirects
return self return self
def upload(self, **file_info) -> "RequestWithOptionalArgs":
self.__t_step.request.upload.update(file_info)
return self
# def hooks(self): # def hooks(self):
# pass # pass