""" 边界条件测试用例 测试系统在各种边界条件下的行为 """ import pytest import asyncio import time from api.user_api import UserAPI from api.role_api import RoleAPI @pytest.mark.boundary @pytest.mark.regression class TestNumericBoundaries: """数值边界测试类""" @pytest.mark.asyncio async def test_username_length_boundary(self, authenticated_client, test_data_manager): """测试用户名长度边界""" user_api = UserAPI(authenticated_client) unique_id = f"{int(time.time() * 1000)}" # 测试正常长度用户名 normal_username = f"user_{unique_id}" user_data = { "username": normal_username, "password": "Test123!@#", "email": f"normal_{unique_id}@example.com", "status": 1 } response = await user_api.create_user(user_data) if response.status_code == 201: user_id = response.json()["id"] test_data_manager.add_user(user_id) assert response.json()["username"] == normal_username # 至少正常长度应该成功 assert response.status_code == 201, "正常长度用户名创建失败" @pytest.mark.asyncio async def test_role_sort_boundary(self, authenticated_client, test_data_manager): """测试角色排序边界""" role_api = RoleAPI(authenticated_client) unique_id = f"{int(time.time() * 1000)}" # 测试正常排序值 normal_role_data = { "roleName": f"Normal_Role_{unique_id}", "roleKey": f"normal_role_{unique_id}", "roleSort": 100, "status": 1 } response = await role_api.create_role(normal_role_data) if response.status_code == 201: role_id = response.json()["id"] test_data_manager.add_role(role_id) assert response.json()["roleSort"] == 100 # 正常排序值应该成功 assert response.status_code == 201, "正常排序值创建失败" @pytest.mark.asyncio async def test_numeric_field_boundaries(self, authenticated_client, test_data_manager): """测试数值字段边界""" role_api = RoleAPI(authenticated_client) unique_id = f"{int(time.time() * 1000)}" # 测试正常数值 role_data = { "roleName": f"Boundary_Role_{unique_id}", "roleKey": f"boundary_role_{unique_id}", "roleSort": 100, "status": 1 } response = await role_api.create_role(role_data) if response.status_code == 201: role_id = response.json()["id"] test_data_manager.add_role(role_id) assert response.json()["roleSort"] == 100 # 正常数值应该成功 assert response.status_code == 201, "正常数值测试失败" @pytest.mark.boundary @pytest.mark.regression class TestTimeBoundaries: """时间边界测试类""" @pytest.mark.asyncio async def test_rapid_sequential_operations(self, authenticated_client, test_data_manager): """测试快速连续操作""" user_api = UserAPI(authenticated_client) unique_id = f"{int(time.time() * 1000)}" # 快速连续创建用户 user_ids = [] for i in range(5): user_data = { "username": f"rapid_user_{unique_id}_{i}", "password": "Test123!@#", "email": f"rapid_{unique_id}_{i}@example.com", "status": 1 } response = await user_api.create_user(user_data) if response.status_code == 201: user_id = response.json()["id"] user_ids.append(user_id) test_data_manager.add_user(user_id) # 至少80%应该成功 assert len(user_ids) >= 4, f"快速连续操作成功率过低: {len(user_ids)}/5" @pytest.mark.asyncio async def test_operation_timing_consistency(self, authenticated_client, test_data_manager): """测试操作时间一致性""" user_api = UserAPI(authenticated_client) unique_id = f"{int(time.time() * 1000)}" # 创建用户 user_data = { "username": f"timing_user_{unique_id}", "password": "Test123!@#", "email": f"timing_{unique_id}@example.com", "status": 1 } create_response = await user_api.create_user(user_data) assert create_response.status_code == 201 user_id = create_response.json()["id"] test_data_manager.add_user(user_id) # 多次查询,验证响应时间一致性 response_times = [] for _ in range(10): start_time = time.time() response = await user_api.get_user_by_id(user_id) end_time = time.time() assert response.status_code == 200 response_times.append(end_time - start_time) await asyncio.sleep(0.1) # 验证响应时间一致性:标准差应该小于1秒 avg_time = sum(response_times) / len(response_times) variance = sum((t - avg_time) ** 2 for t in response_times) / len(response_times) std_dev = variance ** 0.5 assert std_dev < 1.0, f"响应时间不一致,标准差: {std_dev}"