mirror of
https://github.com/httprunner/httprunner.git
synced 2026-08-25 01:59:52 +08:00
TestRunner: run single testcase
This commit is contained in:
21
ate/runner.py
Normal file
21
ate/runner.py
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
import requests
|
||||||
|
from ate import utils, exception
|
||||||
|
|
||||||
|
class TestRunner(object):
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.client = requests.Session()
|
||||||
|
|
||||||
|
def run_single_testcase(self, testcase):
|
||||||
|
req_kwargs = testcase['request']
|
||||||
|
|
||||||
|
try:
|
||||||
|
url = req_kwargs.pop('url')
|
||||||
|
method = req_kwargs.pop('method')
|
||||||
|
except KeyError:
|
||||||
|
raise exception.ParamsError("Params Error")
|
||||||
|
|
||||||
|
resp_obj = self.client.request(url=url, method=method, **req_kwargs)
|
||||||
|
diff_content = utils.diff_response(resp_obj, testcase['response'])
|
||||||
|
success = False if diff_content else True
|
||||||
|
return success, diff_content
|
||||||
92
test/test_runner.py
Normal file
92
test/test_runner.py
Normal file
@@ -0,0 +1,92 @@
|
|||||||
|
import os
|
||||||
|
import random
|
||||||
|
import requests
|
||||||
|
from ate import runner, exception
|
||||||
|
from .base import ApiServerUnittest
|
||||||
|
|
||||||
|
class TestUtils(ApiServerUnittest):
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
self.test_runner = runner.TestRunner()
|
||||||
|
self.clear_users()
|
||||||
|
|
||||||
|
def clear_users(self):
|
||||||
|
url = "http://127.0.0.1:5000/api/users"
|
||||||
|
return requests.delete(url)
|
||||||
|
|
||||||
|
def test_run_single_testcase_success(self):
|
||||||
|
testcase = {
|
||||||
|
"name": "create user which does not exist",
|
||||||
|
"request": {
|
||||||
|
"url": "http://127.0.0.1:5000/api/users/1000",
|
||||||
|
"method": "POST",
|
||||||
|
"headers": {
|
||||||
|
"content-type": "application/json"
|
||||||
|
},
|
||||||
|
"json": {
|
||||||
|
"name": "user1",
|
||||||
|
"password": "123456"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"response": {
|
||||||
|
"status_code": 201,
|
||||||
|
"headers": {
|
||||||
|
"Content-Type": "application/json"
|
||||||
|
},
|
||||||
|
"body": {
|
||||||
|
'success': True,
|
||||||
|
'msg': 'user created successfully.'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
success, _ = self.test_runner.run_single_testcase(testcase)
|
||||||
|
self.assertTrue(success)
|
||||||
|
|
||||||
|
def test_run_single_testcase_fail(self):
|
||||||
|
testcase = {
|
||||||
|
"name": "create user which does not exist",
|
||||||
|
"request": {
|
||||||
|
"url": "http://127.0.0.1:5000/api/users/1000",
|
||||||
|
"method": "POST",
|
||||||
|
"headers": {
|
||||||
|
"content-type": "application/json"
|
||||||
|
},
|
||||||
|
"json": {
|
||||||
|
"name": "user1",
|
||||||
|
"password": "123456"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"response": {
|
||||||
|
"status_code": 200,
|
||||||
|
"headers": {
|
||||||
|
"Content-Type": "html/text"
|
||||||
|
},
|
||||||
|
"body": {
|
||||||
|
'success': False,
|
||||||
|
'msg': "user already existed."
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
success, diff_content = self.test_runner.run_single_testcase(testcase)
|
||||||
|
self.assertFalse(success)
|
||||||
|
self.assertEqual(
|
||||||
|
diff_content['status_code'],
|
||||||
|
{'expected': 200, 'value': 201}
|
||||||
|
)
|
||||||
|
self.assertEqual(
|
||||||
|
diff_content['headers'],
|
||||||
|
{'Content-Type': {'expected': 'html/text', 'value': 'application/json'}}
|
||||||
|
)
|
||||||
|
self.assertEqual(
|
||||||
|
diff_content['body'],
|
||||||
|
{
|
||||||
|
'msg': {
|
||||||
|
'expected': 'user already existed.',
|
||||||
|
'value': 'user created successfully.'
|
||||||
|
},
|
||||||
|
'success': {
|
||||||
|
'expected': False,
|
||||||
|
'value': True
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
Reference in New Issue
Block a user