import requests
# 正常输入测试用例
def test_normal_input():
url = "http://ox/Role/getRolesBylenant"
headers = {"Content-Type": "application/json"}
data = {"roleId": "1", "roleName": "Admin"}
response = requests.post(url, json=data, headers=headers)
assert response.status_code == 200
assert response.json() == [{"id": "1", "name": "Admin"}, {"id": "2", "name": "User"}]
# 无效输入测试用例
def test_invalid_input():
url = "http://ox/Role/getRolesBylenant"
headers = {"Content-Type": "application/json"}
data = {"roleId": "", "roleName": ""}
response = requests.post(url, json=data, headers=headers)
assert response.status_code == 400
assert response.json() == {"error": "Invalid role information"}
# 边界条件测试用例
def test_boundary_condition():
url = "http://ox/Role/getRolesBylenant"
headers = {"Content-Type": "application/json"}
data = {"roleId": "1234567890", "roleName": "VeryLongRoleNameThatExceedsTheLimit"}
response = requests.post(url, json=data, headers=headers)
assert response.status_code == 200
assert response.json() == [{"id": "1234567890", "name": "VeryLongRoleNameThatExceedsTheLimit"}]
# 异常情况测试用例
def test_exceptional_case():
url = "http://ox/Role/getRolesBylenant"
headers = {"Content-Type": "application/json"}
data = {"roleId": "<script>alert('XSS')</script>", "roleName": "<img src='http://example.com/malicious.jpg'>"}
response = requests.post(url, json=data, headers=headers)
assert response.status_code == 400
assert response.json() == {"error": "Invalid characters in role information"}
# 空请求体测试用例
def test_empty_request_body():
url = "http://ox/Role/getRolesBylenant"
headers = {"Content-Type": "application/json"}
data = {}
response = requests.post(url, json=data, headers=headers)
assert response.status_code == 400
assert response.json() == {"error": "Empty request body"}
# 缺少必要字段测试用例
def test_missing_required_field():
url = "http://ox/Role/getRolesBylenant"
headers = {"Content-Type": "application/json"}
data = {"roleId": "1"}
response = requests.post(url, json=data, headers=headers)
assert response.status_code == 400
assert response.json() == {"error": "Missing role name"}
# 超时测试用例(需要根据实际情况调整超时时间)
def test_timeout():
url = "http://ox/Role/getRolesBylenant"
headers = {"Content-Type": "application/json"}
data = {"roleId": "1", "roleName": "Admin"}
try:
response = requests.post(url, json=data, headers=headers, timeout=1)
assert False, "Expected a timeout error"
except requests.exceptions.Timeout:
assert True
# 并发测试用例(需要根据实际情况调整并发数和请求内容)
def test_concurrency():
import threading
url = "http://ox/Role/getRolesBylenant"
headers = {"Content-Type": "application/json"}
data_list = [{"roleId": "1", "roleName": "Admin"}, {"roleId": "2", "roleName": "User"}, ...]
results = []
def send_request(data):
response = requests.post(url, json=data, headers=headers)
results.append(response.json())
threads = []
for data in data_list:
thread = threading.Thread(target=send_request, args=(data,))
threads.append(thread)
thread.start()
for thread in threads:
thread.join()
assert len(results) == len(data_list)
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.