From cb0984aadb92d99056f65fb6d9e25883c29c890d Mon Sep 17 00:00:00 2001 From: debugtalk Date: Wed, 3 Jun 2020 18:32:31 +0800 Subject: [PATCH] refactor: builtin upload file method --- examples/httpbin/upload_test.py | 61 ++++++++++++--------------------- httprunner/loader.py | 1 - httprunner/make.py | 4 +++ httprunner/testcase.py | 4 +++ 4 files changed, 29 insertions(+), 41 deletions(-) diff --git a/examples/httpbin/upload_test.py b/examples/httpbin/upload_test.py index 986b3746..bb67ec5e 100644 --- a/examples/httpbin/upload_test.py +++ b/examples/httpbin/upload_test.py @@ -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") ), ] diff --git a/httprunner/loader.py b/httprunner/loader.py index 27ea6124..e34d4993 100644 --- a/httprunner/loader.py +++ b/httprunner/loader.py @@ -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 diff --git a/httprunner/make.py b/httprunner/make.py index 94dfa6aa..c90956e9 100644 --- a/httprunner/make.py +++ b/httprunner/make.py @@ -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 diff --git a/httprunner/testcase.py b/httprunner/testcase.py index 3828c2a8..bcbfb07d 100644 --- a/httprunner/testcase.py +++ b/httprunner/testcase.py @@ -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