change testcase data structure, in order to distinguish config and test

This commit is contained in:
httprunner
2017-06-26 15:27:27 +08:00
parent 139036dbad
commit 5ac8491c2d
9 changed files with 244 additions and 224 deletions

View File

@@ -1,38 +1,38 @@
- -
variable_binds: variable_binds:
- TOKEN: "debugtalk" - TOKEN: "debugtalk"
- -
variable_binds: variable_binds:
- var: [1, 2, 3] - var: [1, 2, 3]
- -
variable_binds: variable_binds:
- data: {'name': 'user', 'password': '123456'} - data: {'name': 'user', 'password': '123456'}
- -
variable_binds: variable_binds:
- TOKEN: "debugtalk" - TOKEN: "debugtalk"
- token: $TOKEN - token: $TOKEN
- -
function_binds: function_binds:
add_one: "lambda x: x + 1" add_one: "lambda x: x + 1"
add_two_nums: "lambda x, y: x + y" add_two_nums: "lambda x, y: x + y"
variable_binds: variable_binds:
- add1: {"func": "add_one", "args": [2]} - add1: {"func": "add_one", "args": [2]}
- sum2nums: {"func": "add_two_nums", "args": [2, 3]} - sum2nums: {"func": "add_two_nums", "args": [2, 3]}
- -
requires: requires:
- random - random
- string - string
- hashlib - hashlib
function_binds: function_binds:
gen_random_string: "lambda str_len: ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(str_len))" gen_random_string: "lambda str_len: ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(str_len))"
gen_md5: "lambda *str_args: hashlib.md5(''.join(str_args).encode('utf-8')).hexdigest()" gen_md5: "lambda *str_args: hashlib.md5(''.join(str_args).encode('utf-8')).hexdigest()"
variable_binds: variable_binds:
- TOKEN: debugtalk - TOKEN: debugtalk
- random: {"func": "gen_random_string", "args": [5]} - random: {"func": "gen_random_string", "args": [5]}
- data: "{'name': 'user', 'password': '123456'}" - data: "{'name': 'user', 'password': '123456'}"
- authorization: {"func": "gen_md5", "args": [$TOKEN, $data, $random]} - authorization: {"func": "gen_md5", "args": [$TOKEN, $data, $random]}

View File

@@ -1,62 +1,62 @@
- - test:
name: create user which does not exist name: create user which does not exist
requires: requires:
- random - random
- string - string
- hashlib - hashlib
function_binds: function_binds:
gen_random_string: "lambda str_len: ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(str_len))" gen_random_string: "lambda str_len: ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(str_len))"
gen_md5: "lambda *str_args: hashlib.md5(''.join(str_args).encode('utf-8')).hexdigest()" gen_md5: "lambda *str_args: hashlib.md5(''.join(str_args).encode('utf-8')).hexdigest()"
variable_binds: variable_binds:
- TOKEN: debugtalk - TOKEN: debugtalk
- random: {"func": "gen_random_string", "args": [5]} - random: {"func": "gen_random_string", "args": [5]}
- data: '{"name": "user", "password": "123456"}' - data: '{"name": "user", "password": "123456"}'
- authorization: {"func": "gen_md5", "args": ["$TOKEN", "$data", "$random"]} - authorization: {"func": "gen_md5", "args": ["$TOKEN", "$data", "$random"]}
- expected_status_code: 201 - expected_status_code: 201
request: request:
url: http://127.0.0.1:5000/api/users/1000 url: http://127.0.0.1:5000/api/users/1000
method: POST method: POST
headers: headers:
Content-Type: application/json Content-Type: application/json
authorization: "${authorization}" authorization: "${authorization}"
random: "${random}" random: "${random}"
data: "${data}" data: "${data}"
response: response:
status_code: "${expected_status_code}" status_code: "${expected_status_code}"
headers: headers:
Content-Type: application/json Content-Type: application/json
body: body:
success: true success: true
msg: user created successfully. msg: user created successfully.
- - test:
name: create user which does not exist name: create user which does not exist
requires: requires:
- random - random
- string - string
- hashlib - hashlib
function_binds: function_binds:
gen_random_string: "lambda str_len: ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(str_len))" gen_random_string: "lambda str_len: ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(str_len))"
gen_md5: "lambda *str_args: hashlib.md5(''.join(str_args).encode('utf-8')).hexdigest()" gen_md5: "lambda *str_args: hashlib.md5(''.join(str_args).encode('utf-8')).hexdigest()"
variable_binds: variable_binds:
- TOKEN: debugtalk - TOKEN: debugtalk
- random: {"func": "gen_random_string", "args": [5]} - random: {"func": "gen_random_string", "args": [5]}
- data: '{"name": "user", "password": "123456"}' - data: '{"name": "user", "password": "123456"}'
- authorization: {"func": "gen_md5", "args": ["$TOKEN", "$data", "$random"]} - authorization: {"func": "gen_md5", "args": ["$TOKEN", "$data", "$random"]}
- expected_status_code: 500 - expected_status_code: 500
request: request:
url: http://127.0.0.1:5000/api/users/1000 url: http://127.0.0.1:5000/api/users/1000
method: POST method: POST
headers: headers:
Content-Type: application/json Content-Type: application/json
authorization: "${authorization}" authorization: "${authorization}"
random: "${random}" random: "${random}"
data: "${data}" data: "${data}"
response: response:
status_code: "${expected_status_code}" status_code: "${expected_status_code}"
headers: headers:
Content-Type: application/json Content-Type: application/json
body: body:
success: true success: true
msg: user created successfully. msg: user created successfully.

View File

@@ -1,53 +1,57 @@
[ [
{ {
"name": "create user which does not exist", "test": {
"request": { "name": "create user which does not exist",
"url": "http://127.0.0.1:5000/api/users/1000", "request": {
"method": "POST", "url": "http://127.0.0.1:5000/api/users/1000",
"headers": { "method": "POST",
"content-type": "application/json", "headers": {
"authorization": "a83de0ff8d2e896dbd8efb81ba14e17d", "content-type": "application/json",
"random": "A2dEx" "authorization": "a83de0ff8d2e896dbd8efb81ba14e17d",
"random": "A2dEx"
},
"json": {
"name": "user1",
"password": "123456"
}
}, },
"json": { "response": {
"name": "user1", "status_code": 201,
"password": "123456" "headers": {
} "Content-Type": "application/json"
}, },
"response": { "body": {
"status_code": 201, "success": true,
"headers": { "msg": "user created successfully."
"Content-Type": "application/json" }
},
"body": {
"success": true,
"msg": "user created successfully."
} }
} }
}, },
{ {
"name": "create user which existed", "test": {
"request": { "name": "create user which existed",
"url": "http://127.0.0.1:5000/api/users/1000", "request": {
"method": "POST", "url": "http://127.0.0.1:5000/api/users/1000",
"headers": { "method": "POST",
"content-type": "application/json", "headers": {
"authorization": "a83de0ff8d2e896dbd8efb81ba14e17d", "content-type": "application/json",
"random": "A2dEx" "authorization": "a83de0ff8d2e896dbd8efb81ba14e17d",
"random": "A2dEx"
},
"json": {
"name": "user1",
"password": "123456"
}
}, },
"json": { "response": {
"name": "user1", "status_code": 500,
"password": "123456" "headers": {
} "Content-Type": "application/json"
}, },
"response": { "body":{
"status_code": 500, "success": false,
"headers": { "msg": "user already existed."
"Content-Type": "application/json" }
},
"body":{
"success": false,
"msg": "user already existed."
} }
} }
} }

View File

@@ -1,39 +1,39 @@
- - test:
name: create user which does not exist name: create user which does not exist
request: request:
url: http://127.0.0.1:5000/api/users/1000 url: http://127.0.0.1:5000/api/users/1000
method: POST method: POST
headers: headers:
Content-Type: application/json Content-Type: application/json
authorization: a83de0ff8d2e896dbd8efb81ba14e17d authorization: a83de0ff8d2e896dbd8efb81ba14e17d
random: A2dEx random: A2dEx
json: json:
name: "user1" name: "user1"
password: "123456" password: "123456"
response: response:
status_code: 201 status_code: 201
headers: headers:
Content-Type: application/json Content-Type: application/json
body: body:
success: true success: true
msg: user created successfully. msg: user created successfully.
- - test:
name: create user which existed name: create user which existed
request: request:
url: http://127.0.0.1:5000/api/users/1000 url: http://127.0.0.1:5000/api/users/1000
method: POST method: POST
headers: headers:
Content-Type: application/json Content-Type: application/json
authorization: a83de0ff8d2e896dbd8efb81ba14e17d authorization: a83de0ff8d2e896dbd8efb81ba14e17d
random: A2dEx random: A2dEx
json: json:
name: "user1" name: "user1"
password: "123456" password: "123456"
response: response:
status_code: 500 status_code: 500
headers: headers:
Content-Type: application/json Content-Type: application/json
body: body:
success: false success: false
msg: user already existed. msg: user already existed.

View File

@@ -1,50 +1,54 @@
[ [
{ {
"name": "create user which does not exist", "test": {
"request": { "name": "create user which does not exist",
"url": "http://127.0.0.1:5000/api/users/1000", "request": {
"method": "POST", "url": "http://127.0.0.1:5000/api/users/1000",
"headers": { "method": "POST",
"content-type": "application/json" "headers": {
"content-type": "application/json"
},
"cookies": {},
"json": {
"name": "user1",
"password": "123456"
}
}, },
"cookies": {}, "response": {
"json": { "status_code": 201,
"name": "user1", "headers": {
"password": "123456" "Content-Type": "application/json"
} },
}, "body": {
"response": { "success": true,
"status_code": 201, "msg": "user created successfully."
"headers": { }
"Content-Type": "application/json"
},
"body": {
"success": true,
"msg": "user created successfully."
} }
} }
}, },
{ {
"name": "create user which existed", "test": {
"request": { "name": "create user which existed",
"url": "http://127.0.0.1:5000/api/users/1000", "request": {
"method": "POST", "url": "http://127.0.0.1:5000/api/users/1000",
"headers": { "method": "POST",
"content-type": "application/json" "headers": {
"content-type": "application/json"
},
"json": {
"name": "user1",
"password": "123456"
}
}, },
"json": { "response": {
"name": "user1", "status_code": 500,
"password": "123456" "headers": {
} "Content-Type": "application/json"
}, },
"response": { "body":{
"status_code": 500, "success": false,
"headers": { "msg": "user already existed."
"Content-Type": "application/json" }
},
"body":{
"success": false,
"msg": "user already existed."
} }
} }
} }

View File

@@ -1,4 +1,4 @@
- - test:
name: create user which does not exist name: create user which does not exist
request: request:
url: http://127.0.0.1:5000/api/users/1000 url: http://127.0.0.1:5000/api/users/1000
@@ -16,7 +16,7 @@
success: true success: true
msg: user created successfully. msg: user created successfully.
- - test:
name: create user which existed name: create user which existed
request: request:
url: http://127.0.0.1:5000/api/users/1000 url: http://127.0.0.1:5000/api/users/1000

View File

@@ -14,10 +14,18 @@ class TestRunner(ApiServerUnittest):
url = "http://127.0.0.1:5000/api/users" url = "http://127.0.0.1:5000/api/users"
return requests.delete(url) return requests.delete(url)
def test_run_single_testcase_success(self): def test_run_single_testcase_yaml_success(self):
testcase_file_path = os.path.join(os.getcwd(), 'test/data/simple_demo_no_auth.yml')
testcases = utils.load_testcases(testcase_file_path)
testcase = testcases[0]["test"]
success, _ = self.test_runner.run_test(testcase)
self.assertTrue(success)
def test_run_single_testcase_json_success(self):
testcase_file_path = os.path.join(os.getcwd(), 'test/data/simple_demo_no_auth.json') testcase_file_path = os.path.join(os.getcwd(), 'test/data/simple_demo_no_auth.json')
testcases = utils.load_testcases(testcase_file_path) testcases = utils.load_testcases(testcase_file_path)
success, _ = self.test_runner.run_test(testcases[0]) testcase = testcases[0]["test"]
success, _ = self.test_runner.run_test(testcase)
self.assertTrue(success) self.assertTrue(success)
def test_run_single_testcase_fail(self): def test_run_single_testcase_fail(self):

View File

@@ -20,14 +20,16 @@ class TestRunnerV2(ApiServerUnittest):
testcase_file_path = os.path.join( testcase_file_path = os.path.join(
os.getcwd(), 'test/data/simple_demo_auth_hardcode.yml') os.getcwd(), 'test/data/simple_demo_auth_hardcode.yml')
testcases = utils.load_testcases(testcase_file_path) testcases = utils.load_testcases(testcase_file_path)
success, _ = self.test_runner.run_test(testcases[0]) testcase = testcases[0]["test"]
success, _ = self.test_runner.run_test(testcase)
self.assertTrue(success) self.assertTrue(success)
def test_run_single_testcase_json(self): def test_run_single_testcase_json(self):
testcase_file_path = os.path.join( testcase_file_path = os.path.join(
os.getcwd(), 'test/data/simple_demo_auth_hardcode.json') os.getcwd(), 'test/data/simple_demo_auth_hardcode.json')
testcases = utils.load_testcases(testcase_file_path) testcases = utils.load_testcases(testcase_file_path)
success, _ = self.test_runner.run_test(testcases[0]) testcase = testcases[0]["test"]
success, _ = self.test_runner.run_test(testcase)
self.assertTrue(success) self.assertTrue(success)
def test_run_testcase_auth_suite_yaml(self): def test_run_testcase_auth_suite_yaml(self):
@@ -50,7 +52,7 @@ class TestRunnerV2(ApiServerUnittest):
testcase_file_path = os.path.join( testcase_file_path = os.path.join(
os.getcwd(), 'test/data/demo_template_separate.yml') os.getcwd(), 'test/data/demo_template_separate.yml')
testcases = utils.load_testcases(testcase_file_path) testcases = utils.load_testcases(testcase_file_path)
success, _ = self.test_runner.run_test(testcases[0]) success, _ = self.test_runner.run_test(testcases[0]["test"])
self.assertTrue(success) self.assertTrue(success)
success, _ = self.test_runner.run_test(testcases[1]) success, _ = self.test_runner.run_test(testcases[1]["test"])
self.assertFalse(success) self.assertFalse(success)

View File

@@ -17,22 +17,24 @@ class TestUtils(ApiServerUnittest):
os.getcwd(), 'test/data/simple_demo_no_auth.json') os.getcwd(), 'test/data/simple_demo_no_auth.json')
testcases = utils.load_testcases(testcase_file_path) testcases = utils.load_testcases(testcase_file_path)
self.assertEqual(len(testcases), 2) self.assertEqual(len(testcases), 2)
self.assertIn('name', testcases[0]) testcase = testcases[0]["test"]
self.assertIn('request', testcases[0]) self.assertIn('name', testcase)
self.assertIn('response', testcases[0]) self.assertIn('request', testcase)
self.assertIn('url', testcases[0]['request']) self.assertIn('response', testcase)
self.assertIn('method', testcases[0]['request']) self.assertIn('url', testcase['request'])
self.assertIn('method', testcase['request'])
def test_load_yaml_testcases(self): def test_load_yaml_testcases(self):
testcase_file_path = os.path.join( testcase_file_path = os.path.join(
os.getcwd(), 'test/data/simple_demo_no_auth.yml') os.getcwd(), 'test/data/simple_demo_no_auth.yml')
testcases = utils.load_testcases(testcase_file_path) testcases = utils.load_testcases(testcase_file_path)
self.assertEqual(len(testcases), 2) self.assertEqual(len(testcases), 2)
self.assertIn('name', testcases[0]) testcase = testcases[0]["test"]
self.assertIn('request', testcases[0]) self.assertIn('name', testcase)
self.assertIn('response', testcases[0]) self.assertIn('request', testcase)
self.assertIn('url', testcases[0]['request']) self.assertIn('response', testcase)
self.assertIn('method', testcases[0]['request']) self.assertIn('url', testcase['request'])
self.assertIn('method', testcase['request'])
def test_parse_response_object_json(self): def test_parse_response_object_json(self):
url = "http://127.0.0.1:5000/api/users" url = "http://127.0.0.1:5000/api/users"