From f071c0251318a74c59fbe48b7d20113b022554aa Mon Sep 17 00:00:00 2001 From: debugtalk Date: Thu, 2 Aug 2018 11:29:01 +0800 Subject: [PATCH] define httpbin host and port in one place --- tests/api/basic.yml | 2 +- tests/base.py | 15 ++++-- tests/debugtalk.py | 2 + tests/httpbin/basic.yml | 93 ++++++++++++++++++++++++++++++++++++ tests/httpbin/hooks.yml | 4 +- tests/httpbin/load_image.yml | 2 +- tests/httpbin/upload.yml | 2 +- tests/httpbin/validate.yml | 13 +++++ tests/test_httprunner.py | 4 +- tests/test_response.py | 34 ++++++------- tests/test_runner.py | 12 ++--- 11 files changed, 150 insertions(+), 33 deletions(-) create mode 100644 tests/httpbin/basic.yml create mode 100644 tests/httpbin/validate.yml diff --git a/tests/api/basic.yml b/tests/api/basic.yml index e38296c3..75d3c236 100644 --- a/tests/api/basic.yml +++ b/tests/api/basic.yml @@ -94,4 +94,4 @@ - ${teardown_hook_sleep_N_secs($response, $n_secs)} validate: - eq: ["status_code", 200] - - eq: [content.headers.Host, "127.0.0.1:3458"] + - contained_by: [content.headers.Host, $HTTPBIN_SERVER] diff --git a/tests/base.py b/tests/base.py index ca7c96be..745bd222 100644 --- a/tests/base.py +++ b/tests/base.py @@ -3,18 +3,27 @@ import time import unittest import requests -from httpbin import app as httpbin_app from httprunner import utils from tests.api_server import app as flask_app +try: + from httpbin import app as httpbin_app + HTTPBIN_HOST = "127.0.0.1" + HTTPBIN_PORT = 3458 +except ImportError: + HTTPBIN_HOST = "httpbin.org" + HTTPBIN_PORT = 80 + FLASK_APP_PORT = 5000 -HTTPBIN_APP_PORT = 3458 +HTTPBIN_SERVER = "http://{}:{}".format(HTTPBIN_HOST, HTTPBIN_PORT) + def run_flask(): flask_app.run(port=FLASK_APP_PORT) def run_httpbin(): - httpbin_app.run(port=HTTPBIN_APP_PORT) + if HTTPBIN_HOST == "127.0.0.1": + httpbin_app.run(host=HTTPBIN_HOST, port=HTTPBIN_PORT) class ApiServerUnittest(unittest.TestCase): diff --git a/tests/debugtalk.py b/tests/debugtalk.py index da6d41ae..1e965360 100644 --- a/tests/debugtalk.py +++ b/tests/debugtalk.py @@ -6,6 +6,8 @@ import random import string import time +from tests.base import HTTPBIN_SERVER + try: import urllib except NameError: diff --git a/tests/httpbin/basic.yml b/tests/httpbin/basic.yml new file mode 100644 index 00000000..87ead257 --- /dev/null +++ b/tests/httpbin/basic.yml @@ -0,0 +1,93 @@ +- config: + name: basic test with httpbin + request: + base_url: http://httpbin.org/ + +- test: + name: index + request: + url: / + method: GET + validate: + - eq: ["status_code", 200] + - contains: [content, "HTTP Request & Response Service"] + +- test: + name: headers + request: + url: /headers + method: GET + validate: + - eq: ["status_code", 200] + - eq: [content.headers.Host, "httpbin.org"] + +- test: + name: user-agent + request: + url: /user-agent + method: GET + validate: + - eq: ["status_code", 200] + - startswith: [content.user-agent, "python-requests"] + +- test: + name: get without params + request: + url: /get + method: GET + validate: + - eq: ["status_code", 200] + - eq: [content.args, {}] + +- test: + name: get with params in url + request: + url: /get?a=1&b=2 + method: GET + validate: + - eq: ["status_code", 200] + - eq: [content.args, {'a': '1', 'b': '2'}] + +- test: + name: get with params in params field + request: + url: /get + params: + a: 1 + b: 2 + method: GET + validate: + - eq: ["status_code", 200] + - eq: [content.args, {'a': '1', 'b': '2'}] + +- test: + name: set cookie + request: + url: /cookies/set?name=value + method: GET + validate: + - eq: ["status_code", 200] + # - eq: [cookies.name, "value"] + +- test: + name: extract cookie + request: + url: /cookies + method: GET + validate: + - eq: ["status_code", 200] + - eq: [cookies.name, "value"] + +- test: + name: post data + request: + url: /post + method: POST + headers: + Content-Type: application/json + data: abc + validate: + - eq: ["status_code", 200] + + + diff --git a/tests/httpbin/hooks.yml b/tests/httpbin/hooks.yml index c35c306c..7152f799 100644 --- a/tests/httpbin/hooks.yml +++ b/tests/httpbin/hooks.yml @@ -1,7 +1,7 @@ - config: name: basic test with httpbin request: - base_url: http://127.0.0.1:3458/ + base_url: $HTTPBIN_SERVER setup_hooks: - ${hook_print(setup)} teardown_hooks: @@ -19,7 +19,7 @@ - ${teardown_hook_sleep_N_secs($response, 1)} validate: - eq: ["status_code", 200] - - eq: [content.headers.Host, "127.0.0.1:3458"] + - contained_by: [content.headers.Host, $HTTPBIN_SERVER] - test: name: alter response diff --git a/tests/httpbin/load_image.yml b/tests/httpbin/load_image.yml index 78c97a52..545a12e7 100644 --- a/tests/httpbin/load_image.yml +++ b/tests/httpbin/load_image.yml @@ -1,7 +1,7 @@ - config: name: load images request: - base_url: http://127.0.0.1:3458 + base_url: $HTTPBIN_SERVER - test: name: get png image diff --git a/tests/httpbin/upload.yml b/tests/httpbin/upload.yml index 27728949..e604a5cb 100644 --- a/tests/httpbin/upload.yml +++ b/tests/httpbin/upload.yml @@ -1,7 +1,7 @@ - config: name: test upload file with httpbin request: - base_url: http://127.0.0.1:3458 + base_url: $HTTPBIN_SERVER - test: name: upload file diff --git a/tests/httpbin/validate.yml b/tests/httpbin/validate.yml new file mode 100644 index 00000000..310b826b --- /dev/null +++ b/tests/httpbin/validate.yml @@ -0,0 +1,13 @@ +- config: + name: basic test with httpbin + request: + base_url: http://httpbin.org/ + +- test: + name: headers + request: + url: /headers + method: GET + validate: + - eq: ["status_code", 200] + - assert_status_code_is_200: ["status_code"] diff --git a/tests/test_httprunner.py b/tests/test_httprunner.py index 2b5ad47c..fbf9fa07 100644 --- a/tests/test_httprunner.py +++ b/tests/test_httprunner.py @@ -3,7 +3,7 @@ import shutil from httprunner import HttpRunner from httprunner.exceptions import FileNotFound -from tests.base import ApiServerUnittest +from tests.base import HTTPBIN_SERVER, ApiServerUnittest class TestHttpRunner(ApiServerUnittest): @@ -118,7 +118,7 @@ class TestHttpRunner(ApiServerUnittest): { "name": "post data", "request": { - "url": "http://127.0.0.1:3458/post", + "url": "{}/post".format(HTTPBIN_SERVER), "method": "POST", "headers": { "Content-Type": "application/json" diff --git a/tests/test_response.py b/tests/test_response.py index 71d74583..490488e6 100644 --- a/tests/test_response.py +++ b/tests/test_response.py @@ -1,7 +1,7 @@ import requests from httprunner import exceptions, response, utils from httprunner.compat import bytes, str -from tests.base import ApiServerUnittest +from tests.base import HTTPBIN_SERVER, ApiServerUnittest class TestResponse(ApiServerUnittest): @@ -28,7 +28,7 @@ class TestResponse(ApiServerUnittest): self.assertEqual(bytes, type(resp_obj.content)) def test_extract_response_status_code(self): - resp = requests.get(url="http://127.0.0.1:3458/status/200") + resp = requests.get(url="{}/status/200".format(HTTPBIN_SERVER)) resp_obj = response.ResponseObject(resp) extract_binds_list = [ @@ -48,7 +48,7 @@ class TestResponse(ApiServerUnittest): resp_obj.extract_response(extract_binds_list) def test_extract_response_encoding_ok_reason_url(self): - resp = requests.get(url="http://127.0.0.1:3458/status/200") + resp = requests.get(url="{}/status/200".format(HTTPBIN_SERVER)) resp_obj = response.ResponseObject(resp) extract_binds_list = [ @@ -62,7 +62,7 @@ class TestResponse(ApiServerUnittest): self.assertEqual(extract_binds_dict["resp_encoding"], "utf-8") self.assertEqual(extract_binds_dict["resp_ok"], True) self.assertEqual(extract_binds_dict["resp_reason"], "OK") - self.assertEqual(extract_binds_dict["resp_url"], "http://127.0.0.1:3458/status/200") + self.assertEqual(extract_binds_dict["resp_url"], "{}/status/200".format(HTTPBIN_SERVER)) extract_binds_list = [{"resp_encoding": "encoding.xx"}] with self.assertRaises(exceptions.ParamsError): @@ -82,7 +82,7 @@ class TestResponse(ApiServerUnittest): def test_extract_response_cookies(self): resp = requests.get( - url="http://127.0.0.1:3458/cookies", + url="{}/cookies".format(HTTPBIN_SERVER), headers={ "accept": "application/json" } @@ -106,7 +106,7 @@ class TestResponse(ApiServerUnittest): def test_extract_response_elapsed(self): resp = requests.post( - url="http://127.0.0.1:3458/anything", + url="{}/anything".format(HTTPBIN_SERVER), json={ 'success': False, "person": { @@ -146,7 +146,7 @@ class TestResponse(ApiServerUnittest): resp_obj.extract_response(extract_binds_list) def test_extract_response_headers(self): - resp = requests.get(url="http://127.0.0.1:3458/status/200") + resp = requests.get(url="{}/status/200".format(HTTPBIN_SERVER)) resp_obj = response.ResponseObject(resp) extract_binds_list = [ @@ -167,7 +167,7 @@ class TestResponse(ApiServerUnittest): def test_extract_response_body_json(self): resp = requests.post( - url="http://127.0.0.1:3458/anything", + url="{}/anything".format(HTTPBIN_SERVER), json={ 'success': False, "person": { @@ -192,7 +192,7 @@ class TestResponse(ApiServerUnittest): # "Connection": "keep-alive", # "Content-Length": "129", # "Content-Type": "application/json", - # "Host": "127.0.0.1:3458", + # "Host": HTTPBIN_SERVER, # "User-Agent": "python-requests/2.18.4" # }, # "json": { @@ -211,7 +211,7 @@ class TestResponse(ApiServerUnittest): # }, # "method": "POST", # "origin": "127.0.0.1", - # "url": "http://127.0.0.1:3458/anything" + # "url": "{}/anything".format(HTTPBIN_SERVER) # } extract_binds_list = [ @@ -251,7 +251,7 @@ class TestResponse(ApiServerUnittest): ) def test_extract_response_body_html(self): - resp = requests.get(url="http://127.0.0.1:3458/") + resp = requests.get(url=HTTPBIN_SERVER) resp_obj = response.ResponseObject(resp) extract_binds_list = [ @@ -269,7 +269,7 @@ class TestResponse(ApiServerUnittest): resp_obj.extract_response(extract_binds_list) def test_extract_response_others(self): - resp = requests.get(url="http://127.0.0.1:3458/status/200") + resp = requests.get(url="{}/status/200".format(HTTPBIN_SERVER)) resp_obj = response.ResponseObject(resp) extract_binds_list = [ @@ -281,7 +281,7 @@ class TestResponse(ApiServerUnittest): def test_extract_response_fail(self): resp = requests.post( - url="http://127.0.0.1:3458/anything", + url="{}/anything".format(HTTPBIN_SERVER), json={ 'success': False, "person": { @@ -313,7 +313,7 @@ class TestResponse(ApiServerUnittest): def test_extract_response_json_string(self): resp = requests.post( - url="http://127.0.0.1:3458/anything", + url="{}/anything".format(HTTPBIN_SERVER), data="abc" ) @@ -330,7 +330,7 @@ class TestResponse(ApiServerUnittest): def test_extract_text_response(self): resp = requests.post( - url="http://127.0.0.1:3458/anything", + url="{}/anything".format(HTTPBIN_SERVER), data="LB123abcRB789" ) @@ -357,7 +357,7 @@ class TestResponse(ApiServerUnittest): def test_extract_text_response_exception(self): resp = requests.post( - url="http://127.0.0.1:3458/anything", + url="{}/anything".format(HTTPBIN_SERVER), data="LB123abcRB789" ) extract_binds_list = [ @@ -369,7 +369,7 @@ class TestResponse(ApiServerUnittest): def test_extract_response_empty(self): resp = requests.post( - url="http://127.0.0.1:3458/anything", + url="{}/anything".format(HTTPBIN_SERVER), data="abc" ) diff --git a/tests/test_runner.py b/tests/test_runner.py index a1ad5194..65dd10e5 100644 --- a/tests/test_runner.py +++ b/tests/test_runner.py @@ -4,7 +4,7 @@ import time from httprunner import HttpRunner, exceptions, runner from httprunner.testcase import TestcaseLoader from httprunner.utils import FileUtils, deep_update_dict -from tests.base import ApiServerUnittest +from tests.base import HTTPBIN_SERVER, ApiServerUnittest class TestRunner(ApiServerUnittest): @@ -76,7 +76,7 @@ class TestRunner(ApiServerUnittest): "path": os.path.join(os.getcwd(), __file__), "name": "basic test with httpbin", "request": { - "base_url": "http://127.0.0.1:3458/" + "base_url": HTTPBIN_SERVER }, "setup_hooks": [ "${sleep_N_secs(0.5)}" @@ -124,7 +124,7 @@ class TestRunner(ApiServerUnittest): "path": os.path.join(os.getcwd(), __file__), "name": "basic test with httpbin", "request": { - "base_url": "http://127.0.0.1:3458/" + "base_url": HTTPBIN_SERVER } } test = { @@ -174,7 +174,7 @@ class TestRunner(ApiServerUnittest): { "name": "test teardown hooks", "request": { - "url": "http://127.0.0.1:3458/headers", + "url": "{}/headers".format(HTTPBIN_SERVER), "method": "GET", "data": "abc" }, @@ -209,7 +209,7 @@ class TestRunner(ApiServerUnittest): { "name": "test teardown hooks", "request": { - "url": "http://127.0.0.1:3458/headers", + "url": "{}/headers".format(HTTPBIN_SERVER), "method": "GET", "data": "abc" }, @@ -239,7 +239,7 @@ class TestRunner(ApiServerUnittest): { "name": "test teardown hooks", "request": { - "url": "http://127.0.0.1:3458/headers", + "url": "{}/headers".format(HTTPBIN_SERVER), "method": "GET", "data": "abc" },