找回密码
 立即注册
首页 业界区 安全 ai理解产品需求生成测试用例-deepseek

ai理解产品需求生成测试用例-deepseek

菅舛 2025-6-1 20:53:28
ai:把用例捡起来!把用例捡起来!
给大模型需求文档,让它完成设计用例,编写用例,包括功能用例、接口用例、自动化测试用例,自执行~最后发送至工作群中
直接使用deepseek即可
1.png

 
执行一下看看:
2.png

 调用ds分析需求:
3.png

 生成功能/接口用例:
4.png

 生成自动化用例:
5.png

 
6.png

 
7.png

8.png

 看一下自动生成的功能用例和接口用例:
  1. # 为了验证用户登录功能,我们将使用 `pytest` 框架和 `requests` 库来进行 API 测试。以下是一个示例代码,展示了如何实现这个测试用例。
  2. #
  3. # ### 安装依赖
  4. # 首先,确保你已经安装了 `pytest` 和 `requests` 库。如果没有安装,可以使用以下命令进行安装:
  5. #
  6. # ```bash
  7. # pip install pytest requests
  8. # ```
  9. #
  10. # ### 测试代码
  11. #
  12. # ```python
  13. import pytest
  14. import requests
  15. # 定义登录API的URL
  16. LOGIN_URL = "http://127.0.0.1:5000/login"
  17. # 测试用例:验证用户登录功能
  18. def test_user_login():
  19.     # 定义测试数据
  20.     username = "user1"
  21.     password = "password1"
  22.    
  23.     # 构造请求体
  24.     payload = {
  25.         "username": username,
  26.         "password": password
  27.     }
  28.    
  29.     # 发送POST请求到登录API端点
  30.     response = requests.post(LOGIN_URL, json=payload)
  31.    
  32.     # 验证响应状态码是否为200
  33.     assert response.status_code == 200, f"Expected status code 200, but got {response.status_code}"
  34.    
  35.     # 解析API响应
  36.     response_data = response.json()
  37.    
  38.     # 验证响应中是否包含预期的字段
  39.     assert "token" in response_data, "Response does not contain 'token' field"
  40.     assert "user_id" in response_data, "Response does not contain 'user_id' field"
  41.    
  42.     # 验证token和user_id是否有效
  43.     assert isinstance(response_data["token"], str), "Token is not a string"
  44.     assert isinstance(response_data["user_id"], int), "User ID is not an integer"
  45. # 运行测试
  46. if __name__ == "__main__":
  47.     pytest.main()
  48. # ```
  49. #
  50. # ### 代码说明
  51. #
  52. # 1. **LOGIN_URL**: 这是登录API的URL,你需要将其替换为实际的API端点。
  53. #
  54. # 2. **test_user_login**: 这是测试函数,使用 `pytest` 框架进行测试。
  55. #
  56. # 3. **payload**: 这是发送到API的请求体,包含用户名和密码。
  57. #
  58. # 4. **requests.post**: 发送POST请求到登录API端点。
  59. #
  60. # 5. **assert response.status_code == 200**: 验证响应状态码是否为200,表示请求成功。
  61. #
  62. # 6. **response.json()**: 解析API响应为JSON格式。
  63. #
  64. # 7. **assert "token" in response_data**: 验证响应中是否包含 `token` 字段。
  65. #
  66. # 8. **assert "user_id" in response_data**: 验证响应中是否包含 `user_id` 字段。
  67. #
  68. # 9. **assert isinstance(response_data["token"], str)**: 验证 `token` 是否为字符串类型。
  69. #
  70. # 10. **assert isinstance(response_data["user_id"], int)**: 验证 `user_id` 是否为整数类型。
  71. #
  72. # ### 运行测试
  73. #
  74. # 你可以通过以下命令运行测试:
  75. #
  76. # ```bash
  77. # pytest test_login.py
  78. # ```
  79. #
  80. # 如果所有断言都通过,测试将成功完成。如果有任何断言失败,`pytest` 将输出详细的错误信息,帮助你定位问题。
  81. """
  82. 自动化测试执行器
  83. """
  84. import pytest
  85. import os
  86. import sys
  87. from datetime import datetime
  88. from typing import List, Dict
  89. import json
  90. from dotenv import load_dotenv
  91. # 添加项目根目录到Python路径
  92. current_dir = os.path.dirname(os.path.abspath(__file__))
  93. project_root = os.path.abspath(os.path.join(current_dir, "../../../.."))
  94. src_dir = os.path.join(project_root, "ai-test-generator", "src")
  95. sys.path.insert(0, src_dir)
  96. # 加载环境变量
  97. env_path = os.path.join(project_root, "ai-test-generator", ".env")
  98. print(f"正在加载环境变量文件: {env_path}")
  99. load_dotenv(env_path)
  100. try:
  101.     from test_reporter import TestReporter, TestResult
  102. except ImportError as e:
  103.     print(f"错误:无法导入test_reporter模块。请确保项目结构正确。")
  104.     print(f"当前目录: {current_dir}")
  105.     print(f"项目根目录: {project_root}")
  106.     print(f"src目录: {src_dir}")
  107.     print(f"Python路径: {sys.path}")
  108.     raise e
  109. def run_tests(test_files: List[str] = None) -> bool:
  110.     """
  111.     运行测试用例并生成报告
  112.    
  113.     Args:
  114.         test_files: 要运行的测试文件列表,如果为None则运行所有测试
  115.         
  116.     Returns:
  117.         bool: 所有测试是否通过
  118.     """
  119.     print("\n开始执行测试...")
  120.    
  121.     # 初始化测试报告器
  122.     reporter = TestReporter()
  123.    
  124.     # 如果没有指定测试文件,则运行所有测试
  125.     if test_files is None:
  126.         test_files = [f for f in os.listdir(os.path.dirname(__file__))
  127.                      if f.endswith('_test.py') and f != '_automation.py']
  128.         print(f"将运行所有测试文件: {test_files}")
  129.    
  130.     all_passed = True
  131.    
  132.     for test_file in test_files:
  133.         test_path = os.path.join(os.path.dirname(__file__), test_file)
  134.         test_id = os.path.splitext(test_file)[0]
  135.         
  136.         print(f"\n执行测试文件: {test_file}")
  137.         
  138.         # 记录测试执行
  139.         start_time = datetime.now()
  140.         try:
  141.             # 运行测试
  142.             result = pytest.main([test_path, '-v'])
  143.             status = 'passed' if result == 0 else 'failed'
  144.             error_msg = None if result == 0 else 'Test execution failed'
  145.             if result != 0:
  146.                 all_passed = False
  147.             print(f"测试执行结果: {status}")
  148.         except Exception as e:
  149.             status = 'error'
  150.             error_msg = str(e)
  151.             all_passed = False
  152.             print(f"测试执行出错: {e}")
  153.         
  154.         duration = (datetime.now() - start_time).total_seconds()
  155.         
  156.         # 记录测试结果
  157.         reporter.add_test_result(TestResult(
  158.             test_id=test_id,
  159.             test_name=test_file,
  160.             test_type='api',
  161.             status=status,
  162.             duration=duration,
  163.             error_message=error_msg,
  164.             start_time=start_time.isoformat(),
  165.             end_time=datetime.now().isoformat()
  166.         ))
  167.    
  168.     # 生成并保存报告
  169.     reports_dir = os.path.join(project_root, "ai-test-generator", "test_cases", "reports")
  170.     reporter.save_report(reports_dir)
  171.    
  172.     # 发送钉钉通知
  173.     print("\n准备发送测试报告...")
  174.     reporter.send_dingtalk_notification()
  175.    
  176.     print(f"\n测试执行完成,{'全部通过' if all_passed else '存在失败'}")
  177.     return all_passed
  178. if __name__ == "__main__":
  179.     # 获取命令行参数中的测试文件
  180.     test_files = sys.argv[1:] if len(sys.argv) > 1 else None
  181.    
  182.     # 运行测试
  183.     success = run_tests(test_files)
  184.    
  185.     # 设置退出码
  186.     sys.exit(0 if success else 1)
复制代码
  1. metadata:
  2.   id: FUNC_1__20250313_155452
  3.   type: functional
  4.   feature: 1._**用户登录功能**:
  5.   generated_time: '2025-03-13T15:54:52.411287'
  6. test_case:
  7.   title: 用户登录功能测试
  8.   description: 验证用户登录功能是否正常工作,确保用户能够通过正确的用户名和密码成功登录系统。
  9.   test_type: functional
  10.   prerequisites:
  11.   - 系统已安装并运行
  12.   - 用户已注册并拥有有效的用户名和密码
  13.   steps:
  14.   - 打开登录页面
  15.   - 输入有效的用户名
  16.   - 输入有效的密码
  17.   - 点击登录按钮
  18.   expected_results:
  19.   - 登录页面成功加载
  20.   - 用户名和密码输入框接受输入
  21.   - 登录按钮可点击
  22.   - 用户成功登录并跳转到主页
  23.   test_data:
  24.     username: testuser
  25.     password: testpassword
复制代码
本地简单写了个登录接口,用生成的用例跑一下,看看收集结果以及发送结果的功能
接口:
9.png

 改改用例里面的url和参数:
10.png

 
11.png

 
12.png

 
13.png

14.png

 report中的内容:
15.png

发送的wehook通知:
16.png

 代码:
main.py
17.gif
18.gif
  1.   1 import os
  2.   2 import sys
  3.   3 from test_generator import AITestGenerator
  4.   4 from test_case_manager import TestCaseManager
  5.   5 from dotenv import load_dotenv
  6.   6
  7.   7 def read_requirements():
  8.   8     """从用户输入读取需求文档"""
  9.   9     print("\n请输入需求文档(输入完成后请按Ctrl+D或Ctrl+Z结束):")
  10. 10     lines = []
  11. 11     try:
  12. 12         while True:
  13. 13             line = input()
  14. 14             lines.append(line)
  15. 15     except (EOFError, KeyboardInterrupt):
  16. 16         pass
  17. 17     return "\n".join(lines)
  18. 18
  19. 19 def read_requirements_from_file(file_path):
  20. 20     """从文件读取需求文档"""
  21. 21     with open(file_path, 'r', encoding='utf-8') as f:
  22. 22         return f.read()
  23. 23
  24. 24 def main():
  25. 25     # 加载环境变量
  26. 26     load_dotenv()
  27. 27     
  28. 28     # 初始化生成器和管理器
  29. 29     generator = AITestGenerator()
  30. 30     manager = TestCaseManager()
  31. 31     
  32. 32     # 解析命令行参数
  33. 33     if len(sys.argv) < 2:
  34. 34         print("用法: python main.py <command> [options]")
  35. 35         print("命令:")
  36. 36         print("  generate <requirements_file>  - 从需求文件生成测试用例")
  37. 37         print("  generate                      - 从用户输入生成测试用例")
  38. 38         print("  run [type] [feature]         - 执行测试用例")
  39. 39         print("选项:")
  40. 40         print("  type: functional 或 api")
  41. 41         print("  feature: 特定功能名称")
  42. 42         return
  43. 43     
  44. 44     command = sys.argv[1]
  45. 45     
  46. 46     if command == "generate":
  47. 47         # 获取需求文档
  48. 48         if len(sys.argv) > 2:
  49. 49             # 从文件读取需求
  50. 50             requirements = read_requirements_from_file(sys.argv[2])
  51. 51         else:
  52. 52             # 从用户输入读取需求
  53. 53             requirements = read_requirements()
  54. 54         
  55. 55         if not requirements.strip():
  56. 56             print("错误:需求文档不能为空")
  57. 57             return
  58. 58         
  59. 59         # 分析需求
  60. 60         print("\n正在分析需求文档...")
  61. 61         features = generator.analyze_requirements(requirements)
  62. 62         
  63. 63         print("\n提取的功能点:")
  64. 64         for i, feature in enumerate(features, 1):
  65. 65             print(f"{i}. {feature}")
  66. 66         
  67. 67         # 为每个功能点生成测试用例
  68. 68         test_types = ['functional', 'api', 'automation']
  69. 69         for feature in features:
  70. 70             feature_name = feature.strip().lower().replace(' ', '_')
  71. 71            
  72. 72             for test_type in test_types:
  73. 73                 print(f"\n正在生成 {test_type} 测试用例,功能点:{feature}")
  74. 74                 test_case = generator.generate_test_cases(feature, test_type)
  75. 75                 
  76. 76                 # 保存测试用例
  77. 77                 if test_type == 'functional':
  78. 78                     # 保存为YAML文件
  79. 79                     file_path = manager.save_functional_test(test_case.dict(), feature_name)
  80. 80                     print(f"功能测试用例已保存到: {file_path}")
  81. 81                     
  82. 82                 elif test_type == 'api':
  83. 83                     # 保存为Python测试文件和JSON数据文件
  84. 84                     case_path, data_path = manager.save_api_test(test_case.dict(), feature_name)
  85. 85                     print(f"API测试用例已保存到: {case_path}")
  86. 86                     print(f"API测试数据已保存到: {data_path}")
  87. 87                     
  88. 88                 elif test_type == 'automation':
  89. 89                     # 生成自动化测试代码
  90. 90                     automation_code = generator.generate_automation_code(test_case)
  91. 91                     # 保存自动化测试代码
  92. 92                     file_path = os.path.join(manager.api_cases_dir, f"{feature_name}_automation.py")
  93. 93                     with open(file_path, 'w', encoding='utf-8') as f:
  94. 94                         f.write(automation_code)
  95. 95                     print(f"自动化测试代码已保存到: {file_path}")
  96. 96         
  97. 97         print("\n所有测试用例生成完成!")
  98. 98         print("\n您可以在以下目录找到生成的测试用例:")
  99. 99         print(f"功能测试用例: {manager.functional_dir}")
  100. 100         print(f"API测试用例: {manager.api_cases_dir}")
  101. 101         print(f"API测试数据: {manager.api_data_dir}")
  102. 102         
  103. 103     elif command == "run":
  104. 104         # 执行测试用例
  105. 105         test_type = sys.argv[2] if len(sys.argv) > 2 else None
  106. 106         feature = sys.argv[3] if len(sys.argv) > 3 else None
  107. 107         
  108. 108         print(f"\n开始执行测试{'(' + test_type + ')' if test_type else ''}")
  109. 109         if feature:
  110. 110             print(f"功能点: {feature}")
  111. 111         
  112. 112         success = manager.execute_tests(test_type, feature)
  113. 113         
  114. 114         print("\n测试执行完成!")
  115. 115         print(f"测试{'全部通过' if success else '存在失败'}")
  116. 116         print(f"详细报告已保存至: {manager.reports_dir}")
  117. 117     
  118. 118     else:
  119. 119         print(f"未知命令: {command}")
  120. 120         return
  121. 121
  122. 122 if __name__ == "__main__":
  123. 123     main()
复制代码
View Codetest_case_manager.py
19.gif
20.gif
  1.   1 import os
  2.   2 import sys
  3.   3 from test_generator import AITestGenerator
  4.   4 from test_case_manager import TestCaseManager
  5.   5 from dotenv import load_dotenv
  6.   6
  7.   7 def read_requirements():
  8.   8     """从用户输入读取需求文档"""
  9.   9     print("\n请输入需求文档(输入完成后请按Ctrl+D或Ctrl+Z结束):")
  10. 10     lines = []
  11. 11     try:
  12. 12         while True:
  13. 13             line = input()
  14. 14             lines.append(line)
  15. 15     except (EOFError, KeyboardInterrupt):
  16. 16         pass
  17. 17     return "\n".join(lines)
  18. 18
  19. 19 def read_requirements_from_file(file_path):
  20. 20     """从文件读取需求文档"""
  21. 21     with open(file_path, 'r', encoding='utf-8') as f:
  22. 22         return f.read()
  23. 23
  24. 24 def main():
  25. 25     # 加载环境变量
  26. 26     load_dotenv()
  27. 27     
  28. 28     # 初始化生成器和管理器
  29. 29     generator = AITestGenerator()
  30. 30     manager = TestCaseManager()
  31. 31     
  32. 32     # 解析命令行参数
  33. 33     if len(sys.argv) < 2:
  34. 34         print("用法: python main.py <command> [options]")
  35. 35         print("命令:")
  36. 36         print("  generate <requirements_file>  - 从需求文件生成测试用例")
  37. 37         print("  generate                      - 从用户输入生成测试用例")
  38. 38         print("  run [type] [feature]         - 执行测试用例")
  39. 39         print("选项:")
  40. 40         print("  type: functional 或 api")
  41. 41         print("  feature: 特定功能名称")
  42. 42         return
  43. 43     
  44. 44     command = sys.argv[1]
  45. 45     
  46. 46     if command == "generate":
  47. 47         # 获取需求文档
  48. 48         if len(sys.argv) > 2:
  49. 49             # 从文件读取需求
  50. 50             requirements = read_requirements_from_file(sys.argv[2])
  51. 51         else:
  52. 52             # 从用户输入读取需求
  53. 53             requirements = read_requirements()
  54. 54         
  55. 55         if not requirements.strip():
  56. 56             print("错误:需求文档不能为空")
  57. 57             return
  58. 58         
  59. 59         # 分析需求
  60. 60         print("\n正在分析需求文档...")
  61. 61         features = generator.analyze_requirements(requirements)
  62. 62         
  63. 63         print("\n提取的功能点:")
  64. 64         for i, feature in enumerate(features, 1):
  65. 65             print(f"{i}. {feature}")
  66. 66         
  67. 67         # 为每个功能点生成测试用例
  68. 68         test_types = ['functional', 'api', 'automation']
  69. 69         for feature in features:
  70. 70             feature_name = feature.strip().lower().replace(' ', '_')
  71. 71            
  72. 72             for test_type in test_types:
  73. 73                 print(f"\n正在生成 {test_type} 测试用例,功能点:{feature}")
  74. 74                 test_case = generator.generate_test_cases(feature, test_type)
  75. 75                 
  76. 76                 # 保存测试用例
  77. 77                 if test_type == 'functional':
  78. 78                     # 保存为YAML文件
  79. 79                     file_path = manager.save_functional_test(test_case.dict(), feature_name)
  80. 80                     print(f"功能测试用例已保存到: {file_path}")
  81. 81                     
  82. 82                 elif test_type == 'api':
  83. 83                     # 保存为Python测试文件和JSON数据文件
  84. 84                     case_path, data_path = manager.save_api_test(test_case.dict(), feature_name)
  85. 85                     print(f"API测试用例已保存到: {case_path}")
  86. 86                     print(f"API测试数据已保存到: {data_path}")
  87. 87                     
  88. 88                 elif test_type == 'automation':
  89. 89                     # 生成自动化测试代码
  90. 90                     automation_code = generator.generate_automation_code(test_case)
  91. 91                     # 保存自动化测试代码
  92. 92                     file_path = os.path.join(manager.api_cases_dir, f"{feature_name}_automation.py")
  93. 93                     with open(file_path, 'w', encoding='utf-8') as f:
  94. 94                         f.write(automation_code)
  95. 95                     print(f"自动化测试代码已保存到: {file_path}")
  96. 96         
  97. 97         print("\n所有测试用例生成完成!")
  98. 98         print("\n您可以在以下目录找到生成的测试用例:")
  99. 99         print(f"功能测试用例: {manager.functional_dir}")
  100. 100         print(f"API测试用例: {manager.api_cases_dir}")
  101. 101         print(f"API测试数据: {manager.api_data_dir}")
  102. 102         
  103. 103     elif command == "run":
  104. 104         # 执行测试用例
  105. 105         test_type = sys.argv[2] if len(sys.argv) > 2 else None
  106. 106         feature = sys.argv[3] if len(sys.argv) > 3 else None
  107. 107         
  108. 108         print(f"\n开始执行测试{'(' + test_type + ')' if test_type else ''}")
  109. 109         if feature:
  110. 110             print(f"功能点: {feature}")
  111. 111         
  112. 112         success = manager.execute_tests(test_type, feature)
  113. 113         
  114. 114         print("\n测试执行完成!")
  115. 115         print(f"测试{'全部通过' if success else '存在失败'}")
  116. 116         print(f"详细报告已保存至: {manager.reports_dir}")
  117. 117     
  118. 118     else:
  119. 119         print(f"未知命令: {command}")
  120. 120         return
  121. 121
  122. 122 if __name__ == "__main__":
  123. 123     main()
复制代码
View Codetest_generator.py
21.gif
22.gif
[code]  1 import os  2 import sys  3 import time  4 import json  5 import re  6 from typing import List, Dict  7 import requests  8 import httpx  9 from dotenv import load_dotenv 10 from pydantic import BaseModel 11  12 class TestCase(BaseModel): 13     """测试用例模型""" 14     title: str 15     description: str 16     test_type: str  # 'functional', 'api', 'automation' 17     prerequisites: List[str] 18     steps: List[str] 19     expected_results: List[str] 20     test_data: Dict[str, str] 21  22 class AITestGenerator: 23     def __init__(self): 24         load_dotenv() 25         self.api_key = os.getenv("DEEPSEEK_API_KEY") 26         if not self.api_key: 27             print("错误: 未找到DEEPSEEK_API_KEY环境变量。请确保.env文件中包含有效的API密钥。") 28             sys.exit(1) 29              30         self.url = "https://api.deepseek.com/chat/completions" 31         self.headers = { 32             "Content-Type": "application/json", 33             "Authorization": f"Bearer {self.api_key}" 34         } 35         print("成功初始化DeepSeek API配置") 36          37     def _format_json_prompt(self, content: str) -> str: 38         """格式化提示词,确保AI生成标准JSON格式响应""" 39         return f"""请严格按照以下JSON格式生成响应: 40 {{ 41     "title": "测试用例标题", 42     "description": "测试用例描述", 43     "test_type": "api", 44     "prerequisites": ["前置条件1", "前置条件2"], 45     "steps": ["步骤1", "步骤2"], 46     "expected_results": ["预期结果1", "预期结果2"], 47     "test_data": {{ 48         "key1": "value1", 49         "key2": "value2" 50     }} 51 }} 52  53 请基于以下内容生成测试用例(请确保生成的是合法的JSON格式): 54 {content}""" 55  56     def _extract_json_from_text(self, text: str) -> str: 57         """从文本中提取JSON部分""" 58         # 查找第一个 { 和最后一个 } 之间的内容 59         json_match = re.search(r'({[\s\S]*})', text) 60         if json_match: 61             return json_match.group(1) 62         return text 63  64     def _clean_json_string(self, json_str: str) -> str: 65         """清理和修复常见的JSON格式问题""" 66         # 移除可能导致解析错误的Unicode字符 67         json_str = json_str.encode('utf-8', 'ignore').decode('utf-8') 68          69         # 修复常见的格式问题 70         json_str = re.sub(r'(?来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
您需要登录后才可以回帖 登录 | 立即注册