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!
# FROM: examples/httpbin/upload.yml
from httprunner import HttpRunner, TConfig, TStep
from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase
class TestCaseUpload(HttpRunner):
config = TConfig(
**{
"name": "test upload file with httpbin",
"base_url": "${get_httpbin_server()}",
"path": "examples/httpbin/upload_test.py",
"variables": {},
}
)
config = Config("test upload file with httpbin").base_url("${get_httpbin_server()}")
teststeps = [
TStep(
**{
"name": "upload file",
"variables": {
Step(
RunRequest("upload file")
.with_variables(
**{
"file_path": "test.env",
"m_encoder": "${multipart_encoder(file=$file_path)}",
},
"request": {
"url": "/post",
"method": "POST",
"headers": {
"Content-Type": "${multipart_content_type($m_encoder)}"
},
"data": "$m_encoder",
},
"validate": [
{"eq": ["status_code", 200]},
{"startswith": ["body.files.file", "UserName=test"]},
],
}
}
)
.post("/post")
.with_headers(**{"Content-Type": "${multipart_content_type($m_encoder)}"})
.with_data("$m_encoder")
.validate()
.assert_equal("status_code", 200)
.assert_startswith("body.files.file", "UserName=test")
),
TStep(
**{
"name": "upload file with keyword",
"request": {
"url": "/post",
"method": "POST",
"upload": {"file": "test.env"},
},
"validate": [
{"eq": ["status_code", 200]},
{"startswith": ["body.files.file", "UserName=test"]},
],
}
Step(
RunRequest("upload file with keyword")
.post("/post")
.upload(**{"file": "test.env"})
.validate()
.assert_equal("status_code", 200)
.assert_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)
except json.JSONDecodeError as ex:
err_msg = f"JSONDecodeError:\nfile: {json_file}\nerror: {ex}"
logger.error(err_msg)
raise exceptions.FileFormatError(err_msg)
return json_content

View File

@@ -184,6 +184,10 @@ def make_request_chain_style(request: Dict) -> Text:
allow_redirects = request["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

View File

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