AI写测试用例_ai generate unit test case(1)

img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上软件测试知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

需要这份系统化的资料的朋友,可以戳这里获取


#### 2.3、让AI根据代码解释制定测试计划



def generate_a_test_plan(full_code_explaination, unit_test_package=“pytest”):
prompt_to_explain_a_plan = f"“”

A good unit test suite should aim to:

  • Test the function’s behavior for a wide range of possible inputs
  • Test edge cases that the author may not have foreseen
  • Take advantage of the features of {unit_test_package} to make the tests easy to write and maintain
  • Be easy to read and understand, with clean code and descriptive names
  • Be deterministic, so that the tests always pass or fail in the same way

{unit_test_package} has many convenient features that make it easy to write and maintain unit tests. We’ll use them to write unit tests for the function above.

For this particular function, we’ll want our unit tests to handle the following diverse scenarios (and under each scenario, we include a few examples as sub-bullets):
-“”"
prompt = full_code_explaination+prompt_to_explain_a_plan
response = gpt35(prompt)
return response, prompt

test_plan, prompt_to_get_test_plan = generate_a_test_plan(prompt_to_explain_code+code_explaination)
print(test_plan)


针对生成的测试计划,对AI制定了几点要求:


* 测试用例要覆盖更广的范围。
* 测试用例的边界要涉及到作者无法想到的场景。
* 充分利用pytest的特性。
* 确保测试用例简洁、易理解。
* 测试用例的结果是确定的,要么成功、要么失败。


输出结果:



Normal inputs:
- days is a positive integer
- days is 0

  • Edge cases:
    • days is a negative integer
    • days is a float
    • days is a string
  • Invalid inputs:
    • days is None
    • days is a list

#### 2.4、根据测试计划生成测试代码



def generate_test_cases(function_to_test, unit_test_package=“pytest”):
starter_comment = “Below, each test case is represented by a tuple passed to the @pytest.mark.parametrize decorator”
prompt_to_generate_the_unit_test = f"“”

Before going into the individual tests, let’s first look at the complete suite of unit tests as a cohesive whole. We’ve added helpful comments to explain what each line does.

import {
   
   unit_test_package}  # used for our unit tests

{
   
   function_to_test}

#{starter_comment}"""
    full_unit_test_prompt = prompt_to_explain_code +
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值