赞
踩
先自我介绍一下,小编浙江大学毕业,去过华为、字节跳动等大厂,目前阿里P7
深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年最新软件测试全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上软件测试知识点,真正体系化!
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新
如果你需要这些资料,可以添加V获取:vip1024b (备注软件测试)
if months > 0:
time_str += str(months) + “m”
if weeks > 0:
time_str += str(weeks) + “w”
if days > 0:
time_str += str(days) + “d”
return time_str
“”"
def explain_code(function_to_test, unit_test_package=“pytest”):
prompt = f"“”"# How to write great unit tests with {unit_test_package}
In this advanced tutorial for experts, we’ll use Python 3.8 and {unit_test_package}
to write a suite of unit tests to verify the behavior of the following function.
{function_to_test}
Before writing any unit tests, let’s review what each element of the function is doing exactly and what the author’s intentions may have been.
code_explaination, prompt_to_explain_code = explain_code(code)
print(code_explaination)
首先定义了一个gpt35的函数,这个函数的作用如下:
然后,通过一组精心设计的提示语,让GPT模型为我们来解释代码。
输出结果:
the function takes an integer value representing days as its sole argument.
divmod
function is used to calculate the number of years and days, the number of months and days, and the number of weeks and days.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:
{unit_test_package}
to make the tests easy to write and maintain{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制定了几点要求:
输出结果:
Normal inputs:
days
is a positive integerdays
is 0days
is a negative integerdays
is a floatdays
is a stringdays
is None
days
is a listdef 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 + code_explaination + test_plan + prompt_to_generate_the_unit_test
unit_test_response, prompt_to_generate_the_unit_test = generate_test_cases(code)
print(unit_test_response)
输出结果:
@pytest.mark.parametrize(“days, expected”, [
(1, “1d”), # normal input
(7, “1w”), # normal input
(30, “1m”), # normal input
(365, “1y”), # normal input
(731, “2y”), # normal input
(-1, pytest.raises(ValueError)), # abnormal input
(0, pytest.raises(ValueError)), # abnormal input
(1.5, pytest.raises(TypeError)), # abnormal input
(“1”, pytest.raises(TypeError)), # abnormal input
])
def test_format_time(days, expected):
“”"
Test the format_time() function.
“”"
if isinstance(expected, type):
with pytest.raises(expected):
format_time(days)
else:
assert format_time(days) == expected
最后我们最好还是要再检查一下生成的测试代码语法,这个可以通过Python的AST库来完成。检查代码的时候,我们不仅需要生成的测试代码,也需要原来的功能代码,不然无法通过语法检查。
import ast
code_output = prompt_to_generate_the_unit_test[code_start_index:] + unit_test_response
try:
ast.parse(code_output)
except SyntaxError as e:
print(f"Syntax error in generated code: {e}")
print(code_output)
输出结果:
import pytest # used for our unit tests
def format_time(days):
years, days = divmod(days, 365)
months, days = divmod(days, 30)
weeks, days = divmod(days, 7)
time_str = “”
if years > 0:
time_str += str(years) + “y”
if months > 0:
time_str += str(months) + “m”
if weeks > 0:
time_str += str(weeks) + “w”
if days > 0:
time_str += str(days) + “d”
return time_str
#Below, each test case is represented by a tuple passed to the @pytest.mark.parametrize decorator.
#The first element of the tuple is the name of the test case, and the second element is a list of arguments to pass to the function.
#The @pytest.mark.parametrize decorator allows us to write a single test function that can be used to test multiple input values.
@pytest.mark.parametrize(“test_input,expected”, [
(“Valid Inputs”, [
(0, “0d”), # test for 0 days
(1, “1d”), # test for 1 day
(7, “7d”), # test for 7 days
(30, “1m”), # test for 30 days
(365, “1y”), # test for 365 days
(400, “1y35d”), # test for 400 days
(800, “2y160d”), # test for 800 days
(3650, “10y”), # test for 3650 days
(3651, “10y1d”), # test for 3651 days
]),
(“Invalid Inputs”, [
(“string”, None), # test for string input
([], None), # test for list input
((), None), # test for tuple input
({}, None), # test for set input
({1: 1}, None), # test for dictionary input
(1.5, None), # test for float input
(None, None), # test for None input
]),
(“Edge Cases”, [
(10000000000, “274247y5m2w6d”), # test for large positive integer
(1, “1d”), # test for small positive integer
(-10000000000, “-274247y5m2w6d”), # test for large negative integer
(-1, “-1d”) # test for small negative integer
])
])
def test_format_time(test_input, expected):
for days, expected_result in expected:
assert format_time(days) == expected_result
从上面看到有些测试用例跟预期还是有差距的,比如:
@pytest.mark.parametrize(“test_input,expected”, [
(“Valid Inputs”, [
(7, “7d” -> “1w”), # test for 7 days
(30, “1m”), # test for 30 days
(365, “1y”), # test for 365 days
(400, “1y35d” -> “1y1m5d”), # test for 400 days
(800, “2y160d” -> “2y5m1w3d”), # test for 800 days
(3650, “10y”), # test for 3650 days
(3651, “10y1d”), # test for 3651 days
]),
OpenAI 的大语言模型,只是提供了简简单单的 Completion 和 Embedding 这样两个核心接口,通过合理使用这两个接口,我们完成了各种各样复杂的任务。
llama-index 专注于为大语言模型的应用构建索引,虽然 Langchain 也有类似的功能,但这一点并不是 Langchain 的主要卖点。Langchain 的第一个卖点其实就在它的名字里,也就是链式调用。
上面通过多步提示语自动给代码写单元测试。Langchain可以顺序地通过多个Prompt调用OpenAI的GPT模型,这个能力用来实现自动化测试的功能正好匹配。
from langchain import PromptTemplate, OpenAI, LLMChain
from langchain.chains import SequentialChain
import ast
def write_unit_test(function_to_test, unit_test_package=“pytest”):
explain_code = “”“”# How to write great unit tests with {unit_test_package}
In this advanced tutorial for experts, we’ll use Python 3.8 and {unit_test_package}
to write a suite of unit tests to verify the behavior of the following function.
{function_to_test}
Before writing any unit tests, let’s review what each element of the function is doing exactly and what the author’s intentions may have been.
explain_code_template = PromptTemplate(
input_variables=[“unit_test_package”, “function_to_test”],
template=explain_code
)
explain_code_llm = OpenAI(model_name=“text-davinci-002”, temperature=0.4, max_tokens=1000,
top_p=1, stop=[“\n\n”, “\n\t\n”, “\n \n”])
explain_code_step = LLMChain(llm=explain_code_llm, prompt=explain_code_template, output_key=“code_explaination”)
test_plan = “”"
A good unit test suite should aim to:
{unit_test_package}
to make the tests easy to write and maintain{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):
-“”"
test_plan_template = PromptTemplate(
input_variables=[“unit_test_package”, “function_to_test”, “code_explaination”],
template=explain_code+“{code_explaination}”+test_plan
)
test_plan_llm = OpenAI(model_name=“text-davinci-002”, temperature=0.4, max_tokens=1000,
top_p=1, stop=[“\n\n”, “\n\t\n”, “\n \n”])
test_plan_step = LLMChain(llm=test_plan_llm, prompt=test_plan_template, output_key=“test_plan”)
starter_comment = “Below, each test case is represented by a tuple passed to the @pytest.mark.parametrize decorator”
prompt_to_generate_the_unit_test = “”"
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}“”"
unit_test_template = PromptTemplate(
input_variables=[“unit_test_package”, “function_to_test”, “code_explaination”, “test_plan”, “starter_comment”],
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
需要这份系统化的资料的朋友,可以添加V获取:vip1024b (备注软件测试)
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
mptTemplate(
input_variables=[“unit_test_package”, “function_to_test”, “code_explaination”, “test_plan”, “starter_comment”],
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
需要这份系统化的资料的朋友,可以添加V获取:vip1024b (备注软件测试)
[外链图片转存中…(img-qlZ5VDY7-1713281948133)]
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。