refactor: add extract_binds and validators

This commit is contained in:
debugtalk
2017-06-29 15:27:26 +08:00
parent 997e9d642b
commit 6854607cf0
16 changed files with 327 additions and 546 deletions

View File

@@ -13,7 +13,6 @@
- random: {"func": "gen_random_string", "args": [5]}
- data: '{"name": "user", "password": "123456"}'
- authorization: {"func": "gen_md5", "args": ["${TOKEN}", "${data}", "${random}"]}
- expected_status_code: 201
request:
url: http://127.0.0.1:5000/api/users/1000
method: POST
@@ -22,13 +21,12 @@
authorization: "${authorization}"
random: "${random}"
data: "${data}"
response:
status_code: "${expected_status_code}"
headers:
Content-Type: application/json
body:
success: true
msg: user created successfully.
extract_binds:
resp_status_code: status_code
resp_body_success: content.success
validators:
resp_status_code: {"comparator": "eq", "expected": 201}
resp_body_success: {"comparator": "eq", "expected": true}
- test:
name: create user which does not exist
@@ -44,7 +42,6 @@
- random: {"func": "gen_random_string", "args": [5]}
- data: '{"name": "user", "password": "123456"}'
- authorization: {"func": "gen_md5", "args": ["${TOKEN}", "${data}", "${random}"]}
- expected_status_code: 500
request:
url: http://127.0.0.1:5000/api/users/1000
method: POST
@@ -53,10 +50,9 @@
authorization: "${authorization}"
random: "${random}"
data: "${data}"
response:
status_code: "${expected_status_code}"
headers:
Content-Type: application/json
body:
success: false
msg: user already existed.
extract_binds:
resp_status_code: status_code
resp_body_success: content.success
validators:
resp_status_code: {"comparator": "eq", "expected": 500}
resp_body_success: {"comparator": "eq", "expected": false}

View File

@@ -17,7 +17,6 @@
name: create user which does not exist
variable_binds:
- data: '{"name": "user", "password": "123456"}'
- expected_status_code: 201
request:
url: http://127.0.0.1:5000/api/users/1000
method: POST
@@ -26,13 +25,12 @@
authorization: "${authorization}"
random: "${random}"
data: "${data}"
response:
status_code: "${expected_status_code}"
headers:
Content-Type: application/json
body:
success: true
msg: user created successfully.
extract_binds:
resp_status_code: status_code
resp_body_success: content.success
validators:
resp_status_code: {"comparator": "eq", "expected": 201}
resp_body_success: {"comparator": "eq", "expected": true}
- test:
name: create user which does not exist
@@ -47,10 +45,9 @@
authorization: "${authorization}"
random: "${random}"
data: "${data}"
response:
status_code: "${expected_status_code}"
headers:
Content-Type: application/json
body:
success: false
msg: user already existed.
extract_binds:
resp_status_code: status_code
resp_body_success: content.success
validators:
resp_status_code: {"comparator": "eq", "expected": 500}
resp_body_success: {"comparator": "eq", "expected": false}

View File

@@ -15,15 +15,14 @@
"password": "123456"
}
},
"response": {
"status_code": 201,
"headers": {
"Content-Type": "application/json"
},
"body": {
"success": true,
"msg": "user created successfully."
}
"extract_binds": {
"resp_status_code": "status_code",
"resp_body_success": "content.success",
"resp_body_msg": "content.msg"
},
"validators": {
"resp_status_code": {"comparator": "eq", "expected": 201},
"resp_body_success": {"comparator": "eq", "expected": true}
}
}
},
@@ -43,15 +42,13 @@
"password": "123456"
}
},
"response": {
"status_code": 500,
"headers": {
"Content-Type": "application/json"
},
"body":{
"success": false,
"msg": "user already existed."
}
"extract_binds": {
"resp_status_code": "status_code",
"resp_body_success": "content.success"
},
"validators": {
"resp_status_code": {"comparator": "eq", "expected": 500},
"resp_body_success": {"comparator": "eq", "expected": false}
}
}
}

View File

@@ -10,13 +10,13 @@
json:
name: "user1"
password: "123456"
response:
status_code: 201
headers:
Content-Type: application/json
body:
success: true
msg: user created successfully.
extract_binds:
resp_status_code: status_code
resp_body_success: content.success
resp_body_msg: content.msg
validators:
resp_status_code: {"comparator": "eq", "expected": 201}
resp_body_success: {"comparator": "eq", "expected": true}
- test:
name: create user which existed
@@ -30,10 +30,9 @@
json:
name: "user1"
password: "123456"
response:
status_code: 500
headers:
Content-Type: application/json
body:
success: false
msg: user already existed.
extract_binds:
resp_status_code: status_code
resp_body_success: content.success
validators:
resp_status_code: {"comparator": "eq", "expected": 500}
resp_body_success: {"comparator": "eq", "expected": false}

View File

@@ -14,15 +14,14 @@
"password": "123456"
}
},
"response": {
"status_code": 201,
"headers": {
"Content-Type": "application/json"
},
"body": {
"success": true,
"msg": "user created successfully."
}
"extract_binds": {
"resp_status_code": "status_code",
"resp_body_success": "content.success",
"resp_body_msg": "content.msg"
},
"validators": {
"resp_status_code": {"comparator": "eq", "expected": 201},
"resp_body_success": {"comparator": "eq", "expected": true}
}
}
},
@@ -40,15 +39,13 @@
"password": "123456"
}
},
"response": {
"status_code": 500,
"headers": {
"Content-Type": "application/json"
},
"body":{
"success": false,
"msg": "user already existed."
}
"extract_binds": {
"resp_status_code": "status_code",
"resp_body_success": "content.success"
},
"validators": {
"resp_status_code": {"comparator": "eq", "expected": 500},
"resp_body_success": {"comparator": "eq", "expected": false}
}
}
}

View File

@@ -8,13 +8,14 @@
json:
name: user1
password: 123456
response:
status_code: 201
headers:
Content-Type: application/json
body:
success: true
msg: user created successfully.
extract_binds:
resp_status_code: status_code
resp_body_success: content.success
resp_headers_content_type: headers.content-type
validators:
resp_status_code: {"comparator": "eq", "expected": 201}
resp_headers_content_type: {"comparator": "eq", "expected": "application/json"}
resp_body_success: {"comparator": "eq", "expected": true}
- test:
name: create user which existed
@@ -26,10 +27,11 @@
json:
name: user1
password: 123456
response:
status_code: 500
headers:
Content-Type: application/json
body:
success: false
msg: user already existed.
extract_binds:
resp_status_code: status_code
resp_body_success: content.success
resp_headers_content_type: headers.content-type
validators:
resp_status_code: {"comparator": "eq", "expected": 500}
resp_headers_content_type: {"comparator": "eq", "expected": "application/json"}
resp_body_success: {"comparator": "eq", "expected": false}

View File

@@ -1,6 +1,5 @@
import random
import requests
from ate import response, context, exception
from ate import response, exception
from test.base import ApiServerUnittest
class TestResponse(ApiServerUnittest):
@@ -29,199 +28,6 @@ class TestResponse(ApiServerUnittest):
self.assertIn('Content-Length', parsed_dict['headers'])
self.assertTrue(str, type(parsed_dict['body']))
def test_diff_response_status_code_equal(self):
status_code = random.randint(200, 511)
resp = requests.post(
url="http://127.0.0.1:5000/customize-response",
json={
'status_code': status_code,
}
)
expected_resp_json = {
'status_code': status_code
}
resp_obj = response.ResponseObject(resp)
diff_content = resp_obj.diff_response(expected_resp_json)
self.assertFalse(diff_content)
def test_diff_response_status_code_not_equal(self):
status_code = random.randint(200, 511)
resp = requests.post(
url="http://127.0.0.1:5000/customize-response",
json={
'status_code': status_code,
}
)
expected_resp_json = {
'status_code': 512
}
resp_obj = response.ResponseObject(resp)
diff_content = resp_obj.diff_response(expected_resp_json)
self.assertIn('value', diff_content['status_code'])
self.assertIn('expected', diff_content['status_code'])
self.assertEqual(diff_content['status_code']['value'], status_code)
self.assertEqual(diff_content['status_code']['expected'], 512)
def test_diff_response_headers_equal(self):
resp = requests.post(
url="http://127.0.0.1:5000/customize-response",
json={
'headers': {
'abc': 123,
'def': 456
}
}
)
expected_resp_json = {
'headers': {
'abc': 123,
'def': '456'
}
}
resp_obj = response.ResponseObject(resp)
diff_content = resp_obj.diff_response(expected_resp_json)
self.assertFalse(diff_content)
def test_diff_response_headers_not_equal(self):
resp = requests.post(
url="http://127.0.0.1:5000/customize-response",
json={
'headers': {
'a': 123,
'b': '456',
'c': '789'
}
}
)
expected_resp_json = {
'headers': {
'a': '123',
'b': '457',
'd': 890
}
}
resp_obj = response.ResponseObject(resp)
diff_content = resp_obj.diff_response(expected_resp_json)
self.assertEqual(
diff_content['headers'],
{
'b': {'expected': '457', 'value': '456'},
'd': {'expected': 890, 'value': None}
}
)
def test_diff_response_body_equal(self):
resp = requests.post(
url="http://127.0.0.1:5000/customize-response",
json={
'body': {
'success': True,
'count': 10
}
}
)
# expected response body is not specified
expected_resp_json = {}
resp_obj = response.ResponseObject(resp)
diff_content = resp_obj.diff_response(expected_resp_json)
self.assertFalse(diff_content)
# response body is the same as expected response body
expected_resp_json = {
'body': {
'success': True,
'count': '10'
}
}
resp_obj = response.ResponseObject(resp)
diff_content = resp_obj.diff_response(expected_resp_json)
self.assertFalse(diff_content)
def test_diff_response_body_not_equal_type_unmatch(self):
resp = requests.post(
url="http://127.0.0.1:5000/customize-response",
json={
'body': {
'success': True,
'count': 10
}
}
)
# response body content type not match
expected_resp_json = {
'body': "ok"
}
resp_obj = response.ResponseObject(resp)
diff_content = resp_obj.diff_response(expected_resp_json)
self.assertEqual(
diff_content['body'],
{
'value': {'success': True, 'count': 10},
'expected': 'ok'
}
)
def test_diff_response_body_not_equal_string_unmatch(self):
resp = requests.post(
url="http://127.0.0.1:5000/customize-response",
json={
'body': "success"
}
)
# response body content type matched to be string, while value unmatch
expected_resp_json = {
'body': "ok"
}
resp_obj = response.ResponseObject(resp)
diff_content = resp_obj.diff_response(expected_resp_json)
self.assertEqual(
diff_content['body'],
{
'value': 'success',
'expected': 'ok'
}
)
def test_diff_response_body_not_equal_json_unmatch(self):
resp = requests.post(
url="http://127.0.0.1:5000/customize-response",
json={
'body': {
'success': False
}
}
)
# response body is the same as expected response body
expected_resp_json = {
'body': {
'success': True,
'count': 10
}
}
resp_obj = response.ResponseObject(resp)
diff_content = resp_obj.diff_response(expected_resp_json)
self.assertEqual(
diff_content['body'],
{
'success': {
'value': False,
'expected': True
},
'count': {
'value': None,
'expected': 10
}
}
)
def test_extract_response_json(self):
resp = requests.post(
url="http://127.0.0.1:5000/customize-response",
@@ -252,12 +58,9 @@ class TestResponse(ApiServerUnittest):
"resp_content_person_first_name": "content.person.name.first_name",
"resp_content_cities_1": "content.person.cities.1"
}
resp_obj = response.ResponseObject(resp)
extract_binds_dict = resp_obj.extract_response(extract_binds)
test_context = context.Context()
test_context.bind_extractors(extract_binds)
response.ResponseObject(resp, test_context)
extract_binds_dict = test_context.extractors
self.assertEqual(
extract_binds_dict["resp_status_code"],
200
@@ -283,7 +86,6 @@ class TestResponse(ApiServerUnittest):
"Shenzhen"
)
def test_extract_response_fail(self):
resp = requests.post(
url="http://127.0.0.1:5000/customize-response",
@@ -308,22 +110,18 @@ class TestResponse(ApiServerUnittest):
extract_binds = {
"resp_content_dict_key_error": "content.not_exist"
}
test_context = context.Context()
test_context.bind_extractors(extract_binds)
resp_obj = response.ResponseObject(resp)
with self.assertRaises(exception.ParamsError):
response.ResponseObject(resp, test_context)
resp_obj.extract_response(extract_binds)
extract_binds = {
"resp_content_list_index_error": "content.person.cities.3"
}
test_context = context.Context()
test_context.bind_extractors(extract_binds)
resp_obj = response.ResponseObject(resp)
with self.assertRaises(exception.ParamsError):
response.ResponseObject(resp, test_context)
resp_obj.extract_response(extract_binds)
def test_extract_response_json_string(self):
resp = requests.post(
@@ -339,13 +137,48 @@ class TestResponse(ApiServerUnittest):
extract_binds = {
"resp_content_body": "content"
}
resp_obj = response.ResponseObject(resp)
test_context = context.Context()
test_context.bind_extractors(extract_binds)
resp_obj = response.ResponseObject(resp, test_context)
extract_binds_dict = test_context.extractors
extract_binds_dict = resp_obj.extract_response(extract_binds)
self.assertEqual(
extract_binds_dict["resp_content_body"],
"abc"
)
def test_validate(self):
url = "http://127.0.0.1:5000/"
resp = requests.get(url)
resp_obj = response.ResponseObject(resp)
validators = {
"resp_status_code": {"comparator": "eq", "expected": 201},
"resp_body_success": {"comparator": "eq", "expected": True}
}
variables_mapping = {
"resp_status_code": 200,
"resp_body_success": True
}
diff_content_dict = resp_obj.validate(validators, variables_mapping)
self.assertFalse(resp_obj.success)
self.assertEqual(
diff_content_dict,
{
"resp_status_code": {
"comparator": "eq", "expected": 201, "value": 200
}
}
)
validators = {
"resp_status_code": {"comparator": "eq", "expected": 201},
"resp_body_success": {"comparator": "eq", "expected": True}
}
variables_mapping = {
"resp_status_code": 201,
"resp_body_success": True
}
diff_content_dict = resp_obj.validate(validators, variables_mapping)
self.assertTrue(resp_obj.success)
self.assertEqual(diff_content_dict, {})

View File

@@ -41,39 +41,31 @@ class TestRunner(ApiServerUnittest):
"password": "123456"
}
},
"response": {
"status_code": 200,
"headers": {
"Content-Type": "html/text"
},
"body": {
'success': False,
'msg': "user already existed."
}
"extract_binds": {
"resp_status_code": "status_code",
"resp_body_success": "content.success",
"resp_headers_contenttype": "headers.content-type"
},
"validators": {
"resp_status_code": {"comparator": "eq", "expected": 200},
"resp_body_success": {"comparator": "eq", "expected": False},
"resp_headers_contenttype": {"comparator": "eq", "expected": "html/text"}
}
}
success, diff_content = self.test_runner.run_test(testcase)
self.assertFalse(success)
self.assertEqual(
diff_content['status_code'],
{'expected': 200, 'value': 201}
diff_content['resp_status_code'],
{"comparator": "eq", "expected": 200, 'value': 201}
)
self.assertEqual(
diff_content['headers'],
{'Content-Type': {'expected': 'html/text', 'value': 'application/json'}}
diff_content['resp_body_success'],
{"comparator": "eq", "expected": False, 'value': True}
)
self.assertEqual(
diff_content['body'],
{
'msg': {
'expected': 'user already existed.',
'value': 'user created successfully.'
},
'success': {
'expected': False,
'value': True
}
}
diff_content['resp_headers_contenttype'],
{"comparator": "eq", "expected": "html/text", 'value': "application/json"}
)
def test_run_testset_json_success(self):

View File

@@ -1,6 +1,6 @@
import unittest
from ate.testcase import TestcaseParser
from ate.testcase import parse_template
from ate import exception
@@ -18,7 +18,6 @@ class TestcaseParserUnittest(unittest.TestCase):
"expected_status": 201,
"expected_success": True
}
self.testcase_parser = TestcaseParser(self.variables_binds)
def test_parse_testcase_template(self):
testcase = {
@@ -43,7 +42,7 @@ class TestcaseParserUnittest(unittest.TestCase):
}
}
}
parsed_testcase = self.testcase_parser.parse(testcase)
parsed_testcase = parse_template(testcase, self.variables_binds)
self.assertEqual(
parsed_testcase["request"]["url"],
@@ -78,7 +77,7 @@ class TestcaseParserUnittest(unittest.TestCase):
}
}
with self.assertRaises(exception.ParamsError):
self.testcase_parser.parse(testcase)
parse_template(testcase, self.variables_binds)
def test_parse_testcase_with_new_variable_binds(self):
testcase = {
@@ -90,10 +89,9 @@ class TestcaseParserUnittest(unittest.TestCase):
new_variable_binds = {
"method": "GET"
}
self.testcase_parser.update_variables_binds(new_variable_binds)
parsed_testcase = self.testcase_parser.parse(testcase)
self.variables_binds.update(new_variable_binds)
parsed_testcase = parse_template(testcase, self.variables_binds)
self.assertIn("method", self.testcase_parser.variables_binds)
self.assertEqual(
parsed_testcase["request"]["method"],
new_variable_binds["method"]

View File

@@ -18,7 +18,6 @@ class TestUtils(ApiServerUnittest):
testcase = testcases[0]["test"]
self.assertIn('name', testcase)
self.assertIn('request', testcase)
self.assertIn('response', testcase)
self.assertIn('url', testcase['request'])
self.assertIn('method', testcase['request'])
@@ -30,7 +29,6 @@ class TestUtils(ApiServerUnittest):
testcase = testcases[0]["test"]
self.assertIn('name', testcase)
self.assertIn('request', testcase)
self.assertIn('response', testcase)
self.assertIn('url', testcase['request'])
self.assertIn('method', testcase['request'])
@@ -76,7 +74,6 @@ class TestUtils(ApiServerUnittest):
for testcase in testset["testcases"]:
self.assertIn('name', testcase)
self.assertIn('request', testcase)
self.assertIn('response', testcase)
self.assertIn('url', testcase['request'])
self.assertIn('method', testcase['request'])
@@ -179,3 +176,11 @@ class TestUtils(ApiServerUnittest):
query = "person.name.first_name"
result = utils.query_json(json_content, query)
self.assertEqual(result, "Leo")
def test_compare(self):
self.assertEqual(utils.compare(1, 1, "eq"), False)
self.assertEqual(utils.compare("abc", "abc", "eq"), False)
self.assertEqual(utils.compare("abc", "abc"), False)
self.assertEqual(utils.compare(123, "123", "eq"), True)
self.assertEqual(utils.compare(123, "123"), True)