""" API客户端测试 - TDD Green阶段 测试API客户端的功能。 """ import pytest import allure from core.api_client import APIClient, APIResponse @allure.epic("测试基础设施") @allure.feature("API客户端测试 - TDD Green阶段") class TestAPIClient: """API客户端测试类 - TDD Green阶段""" @allure.title("测试API客户端GET请求 - TDD Green阶段") @allure.description("验证API客户端可以发送GET请求") @allure.severity(allure.severity_level.CRITICAL) @pytest.mark.smoke def test_api_get_request(self) -> None: """ TDD Green阶段: 测试API客户端GET请求 预期结果: - 可以发送GET请求 - 返回APIResponse对象 """ with allure.step("Step 1: 创建API客户端"): client = APIClient(base_url="http://localhost:8080") allure.attach("API客户端创建成功", "步骤1", allure.attachment_type.TEXT) with allure.step("Step 2: 发送GET请求"): response = client.get("/api/users") allure.attach(f"响应类型: {type(response).__name__}", "步骤2", allure.attachment_type.TEXT) assert isinstance(response, APIResponse), "响应应该是APIResponse类型" assert hasattr(response, 'status_code'), "响应应该有status_code属性" assert hasattr(response, 'success'), "响应应该有success属性" # 由于没有实际服务,连接会失败,但API客户端应该正确处理 assert response.success is False, "没有服务时应该返回失败" assert response.error_message is not None, "应该有错误信息" @allure.title("测试API客户端POST请求 - TDD Green阶段") @allure.description("验证API客户端可以发送POST请求") @allure.severity(allure.severity_level.CRITICAL) @pytest.mark.smoke def test_api_post_request(self) -> None: """ TDD Green阶段: 测试API客户端POST请求 预期结果: - 可以发送POST请求 - 返回APIResponse对象 """ with allure.step("Step 1: 创建API客户端"): client = APIClient(base_url="http://localhost:8080") with allure.step("Step 2: 发送POST请求"): data = { "username": "test_user", "email": "test@example.com" } response = client.post("/api/users", json_data=data) allure.attach(f"响应类型: {type(response).__name__}", "步骤2", allure.attachment_type.TEXT) assert isinstance(response, APIResponse), "响应应该是APIResponse类型" assert hasattr(response, 'status_code'), "响应应该有status_code属性" @allure.title("测试API客户端认证 - TDD Green阶段") @allure.description("验证API客户端可以处理认证") @allure.severity(allure.severity_level.CRITICAL) @pytest.mark.smoke def test_api_authentication(self) -> None: """ TDD Green阶段: 测试API客户端认证 预期结果: - 可以设置默认请求头 - 认证信息会包含在请求中 """ with allure.step("Step 1: 创建带认证的API客户端"): client = APIClient( base_url="http://localhost:8080", default_headers={"Authorization": "Bearer test_token"} ) allure.attach("创建带认证的客户端", "步骤1", allure.attachment_type.TEXT) with allure.step("Step 2: 验证默认请求头"): assert "Authorization" in client._default_headers, "应该有Authorization头" assert client._default_headers["Authorization"] == "Bearer test_token", "Token应该正确" allure.attach("✅ 认证头设置正确", "步骤2", allure.attachment_type.TEXT) @allure.title("测试API错误处理 - TDD Green阶段") @allure.description("验证API客户端可以处理错误") @allure.severity(allure.severity_level.NORMAL) @pytest.mark.regression def test_api_error_handling(self) -> None: """ TDD Green阶段: 测试API错误处理 预期结果: - 正确处理连接错误 - 返回包含错误信息的APIResponse """ with allure.step("Step 1: 创建API客户端"): client = APIClient(base_url="http://invalid-host:9999") with allure.step("Step 2: 发送请求到无效地址"): response = client.get("/api/test") allure.attach(f"响应: success={response.success}, error={response.error_message}", "步骤2", allure.attachment_type.TEXT) assert response.success is False, "应该返回失败" assert response.error_message is not None, "应该有错误信息" assert "连接" in response.error_message or "Connection" in response.error_message, "错误信息应该包含连接错误" @allure.title("测试API超时处理 - TDD Green阶段") @allure.description("验证API客户端可以处理超时") @allure.severity(allure.severity_level.NORMAL) @pytest.mark.regression def test_api_timeout_handling(self) -> None: """ TDD Green阶段: 测试API超时处理 预期结果: - 支持超时设置 - 超时后返回错误 """ with allure.step("Step 1: 创建API客户端"): client = APIClient(base_url="http://localhost:8080", timeout=1) with allure.step("Step 2: 验证超时设置"): assert client._default_timeout == 1, "超时时间应该为1秒" allure.attach("✅ 超时设置正确", "步骤2", allure.attachment_type.TEXT)