Should Developers Still Learn How to Write Tests?
### 章节 1:告别手动编写测试:从“编写代码”到“掌控行为” 📝 **本节摘要**: > 讲者提出了一个具有争议的观点:随着AI的发展,开发者不应再手动编写每一行测试代码。引用Node.js创始人Ryan Dahl的观点,强调工程师应专注于应用程序的“行为”而非具体的“语法”。同时,讲者预言Q...
Category: AI📝 本节摘要:
讲者提出了一个具有争议的观点:随着AI的发展,开发者不应再手动编写每一行测试代码。引用Node.js创始人Ryan Dahl的观点,强调工程师应专注于应用程序的“行为”而非具体的“语法”。同时,讲者预言QA工程师的角色将在未来几年内面临被自动化取代的巨大压力。
[原文] [Speaker]: so here's a tricky question With all the advancements in the AI that we have nowadays should you still be writing tests for your application or should you just ditch them and let some kind of a tool automate all of that
[译文] [Speaker]: 这是一个棘手的问题:鉴于如今AI的所有进步,你是否还应该为你的应用程序编写测试,或者你应该干脆放弃手动编写,让某种工具来自动化这一切?
[原文] [Speaker]: and I'm talking about unit tests integration tests contract testing E2 testing whatever you can think of I believe that there's no need to do so much testing anymore
[译文] [Speaker]: 我指的是单元测试、集成测试、契约测试、端到端(E2E)测试,任何你能想到的测试。我相信再也没有必要做那么多(手动)测试了。
[原文] [Speaker]: Now let me explain I'm talking about the manual test ownership meaning writing every line of the test by hand and actually knowing what your tests look like I mean the code specifics
[译文] [Speaker]: 让我解释一下,我指的是手动测试的所有权,即亲手编写每一行测试代码,并且确切地知道测试长什么样——我是指代码细节。
[原文] [Speaker]: Even the creator of NodeJS Ryan Dal also states that writing syntax directly is not what software engineers should do anymore and I totally agree with that
[译文] [Speaker]: 就连Node.js的创始人Ryan Dahl也指出,直接编写语法不再是软件工程师应该做的事情了,我完全同意这一点。
[原文] [Speaker]: Instead what we should do is own the behavior We should know how the app should behave and let some automated tool write those tests for us Not only write but also adapt them as we change our production code
[译文] [Speaker]: 相反,我们应该做的是掌控“行为”。我们需要知道应用程序应该如何表现,并让某种自动化工具为我们编写这些测试。不仅是编写,还要随着我们修改生产环境代码而调整它们。
[原文] [Speaker]: So every time we change the production code the test should be updated in the background and run against the new application
[译文] [Speaker]: 所以,每当我们更改生产代码时,测试应该在后台更新,并针对新的应用程序运行。
[原文] [Speaker]: And by the way as a side note my tip to QA engineers you should really consider refocusing because QA engineering or writing tests rather is going to be automated even more compared to front-end development within the next years
[译文] [Speaker]: 顺便提一下,作为补充,我对QA工程师的建议是:你们真的应该考虑重新调整方向,因为与前端开发相比,QA工程或编写测试在未来几年内将更加自动化。
📝 本节摘要:
为了验证观点,讲者设计了一个在电商网站添加“优惠券”功能的实验。本节详细描述了如果不借助AI,开发者需要手动完成的繁琐工作:包括后端API调整、前端UI修改以及端到端(E2E)测试流程的更新,展示了传统开发流程中的高昂成本。
[原文] [Speaker]: And I'm going to do a small experiment by adding a small feature with a coupon codes on my e-commerce website And I'm going to try to see how much effort it would take me to adjust the test manually
[译文] [Speaker]: 我要做一个小实验,在我的电商网站上添加一个小的优惠券代码功能。我想看看手动调整测试需要花费我多少精力。
[原文] [Speaker]: I'm going to let my AI agent to do that And at the end I'm going to use the sponsor of today's video Testbrite and see how all of that can actually be automated from the beginning to all the way to the end
[译文] [Speaker]: 我打算让我的AI代理(Agent)来做这件事。最后,我将使用今天视频的赞助商TestSprite,看看这一切如何从头到尾被真正自动化。
[原文] [Speaker]: So this is going to be the code for my e-commerce website On the back end we have a simple Node.js app with a list of products that I have hardcoded and we're going to return them to the UI
[译文] [Speaker]: 这是我的电商网站代码。在后端,我们有一个简单的Node.js应用,带有一组硬编码的产品列表,我们将把它们返回给UI。
[原文] [Speaker]: On the front end it's a simpler React app with its own test files And we also have the folder called test that's responsible for E2 tests that are running with playright
[译文] [Speaker]: 在前端,这是一个更简单的React应用,有它自己的测试文件。我们还有一个名为test的文件夹,负责运行基于Playwright的端到端(E2E)测试。
[原文] [Speaker]: Now if I were to introduce a new feature called a product spec coupon feature where I have already described all of that what I would have to do and by the way let's take a look at my e-commerce website
[译文] [Speaker]: 现在,如果我要引入一个名为“产品规格优惠券”的新功能——我已经描述了所有细节——我需要做什么呢?顺便说一下,让我们先看看我的电商网站。
[原文] [Speaker]: As you can see here are some items You can add them to the cart and you can remove them from the cart if you like So this is how the code looks like Uh and I want to have a new section here which says add a coupon Right very simple
[译文] [Speaker]: 如你所见,这里有一些商品。你可以把它们加入购物车,如果不想要也可以移除。这就是代码的样子。呃,我想在这里增加一个新的部分,显示“添加优惠券”。对,非常简单。
[原文] [Speaker]: What I would have to do is go to my backend application I would have to adjust the endpoint or actually create a new endpoint that is going to include some of the tests as well
[译文] [Speaker]: 我需要做的是去我的后端应用程序,调整端点(endpoint),或者实际上创建一个新的端点,这同时也需要包含一些测试。
[原文] [Speaker]: Some of the changes would have to be made on the front end for the React application So for example at the checkout I would need to adjust the way the coupon is being applied and so on
[译文] [Speaker]: React应用的前端也需要做一些改动。例如,在结账时,我需要调整应用优惠券的方式等等。
[原文] [Speaker]: And by the way this is already some of the work And last but not least I would also have to change my E2 testing meaning add a user flow for adding a coupon code that's going to apply some kind of a discount for the overall price
[译文] [Speaker]: 顺便说一句,这已经是不少工作了。最后但同样重要的是,我还必须更改我的E2E测试,意味着要添加一个用户流程来通过输入优惠券代码为总价应用某种折扣。
[原文] [Speaker]: Now as you can see this is quite some effort that you would need to do without even an AI agent
[译文] [Speaker]: 如你所见,如果不使用AI代理,你需要付出相当大的努力。
📝 本节摘要:
讲者展示了利用AI Agent根据详细的需求文档自动生成功能代码及相应测试的过程。虽然AI成功生成了后端校验逻辑、前端组件以及Playwright测试脚本,但讲者指出这仍属于“半自动化”——开发者依然需要人工审查和维护这些生成的测试,缺乏全局概览。
[原文] [Speaker]: When it comes to the AI agent what I could do is reference this file here And by the way this file contains all of the needed things for my feature So it has some constraints user stories functional requirements invalid use cases and so on
[译文] [Speaker]: 当涉及到AI代理时,我可以做的是引用这个文件。顺便说一下,这个文件包含了我的功能所需的所有内容。它有一些约束条件、用户故事、功能需求、无效用例等等。
[原文] [Speaker]: So as you can see it's quite thorough and my AI agent would probably do a good job and even adjust the test Now I didn't add it as a system prompt but let's run it and see what changes it does
[译文] [Speaker]: 如你所见,它非常详尽,我的AI代理可能会做得很好,甚至调整测试。我现在没有把它作为系统提示词添加,但让我们运行它,看看它做了什么改变。
[原文] [Speaker]: All right I can see that my agent did a lot of changes And here's the summary So we're going to take a look at how the feature looks like now
[译文] [Speaker]: 好的,我可以看到我的代理做了很多更改。这是摘要。所以我们要看看这个功能现在长什么样。
[原文] [Speaker]: So I'm going to add some elements to the card And hopefully we see a section and we do for the coupon code And this is how the coupon code is later going to be applied to the total sum
[译文] [Speaker]: 我要在购物车里添加一些元素。希望我们能看到一个区域——确实看到了——用于输入优惠券代码。这就是优惠券代码稍后如何应用到总金额上的方式。
[原文] [Speaker]: But anyways let's take a look at the test that it adjusted As expected in the back end we do get a new describe block for the coupon validation endpoint and some of the tests that are underlying for for example rejecting it handling coupon code with white spaces and so on So that's pretty cool
[译文] [Speaker]: 无论如何,让我们看看它调整后的测试。不出所料,在后端,我们确实得到了一个新的 describe 块,用于优惠券验证端点以及一些底层测试,例如拒绝无效代码、处理带有空格的优惠券代码等等。这非常酷。
[原文] [Speaker]: And on the front end we do have some tests for the cart itself where the coupon input field is being validated and rendered and so on So this is great
[译文] [Speaker]: 在前端,我们确实有一些针对购物车本身的测试,验证并渲染了优惠券输入字段等等。这很棒。
[原文] [Speaker]: But every time I adjust my application meaning I let my AI write some feature or I add some feature I still need to go over to this unit test
[译文] [Speaker]: 但是,每当我调整我的应用程序,意味着我让AI编写某些功能或者我自己添加某些功能时,我仍然需要去检查这个单元测试。
[原文] [Speaker]: And by the way I hope it also adjusted the EWE test And it did As you can see we have a new test that playright is going to run when I start the EWE test
[译文] [Speaker]: 顺便说一下,我希望它也调整了E2E测试。它确实调整了。如你所见,当我们启动E2E测试时,Playwright将会运行一个新的测试。
[原文] [Speaker]: This is all good but all of that still needs to be adjusted every time I write a feature and I'm kind of missing an big overview of seeing what kind of tests I have what are the results whether they're failing or not
[译文] [Speaker]: 这一切都很好,但每次我编写一个功能时,所有这些仍然需要调整,而且我有点缺少一个大的概览,无法看到我有哪些类型的测试、结果如何、它们是否失败了。
[原文] [Speaker]: So let me show you even a better way of dealing with that with test sprite
[译文] [Speaker]: 所以,让我向你展示一个更好的处理方法,使用TestSprite。
📝 本节摘要:
讲者回滚了之前的测试代码,演示如何通过MCP Server(Model Context Protocol)集成TestSprite工具。本节详细介绍了从引导(Bootstrapping)、读取产品需求文档,到自动生成测试计划(Test Plan)和测试代码的完整工作流。
[原文] [Speaker]: So I'm going to go and revert these tests but I'm still going to keep the feature as it is So let's revert these and we don't have the tests anymore
[译文] [Speaker]: 所以我要去回滚这些测试,但我仍然保留该功能原样。让我们回滚这些,现在我们没有测试了。
[原文] [Speaker]: So obviously you need to set up Testbrite and it's actually very easy to do if you want to use an MCP server
[译文] [Speaker]: 显然你需要设置TestSprite,如果你想使用MCP服务器的话,这其实非常容易做到。
[原文] [Speaker]: So the MCP server is installed and it's installed uh by adding the simple configuration for the MCP server in the JSON file under the VSC code folder And my chat already has access to this MCB server
[译文] [Speaker]: MCP服务器已经安装好了,呃,是通过在VS Code文件夹下的JSON文件中添加MCP服务器的简单配置来安装的。我的聊天窗口已经可以访问这个MCP服务器了。
[原文] [Speaker]: The workflow at the end of the day is going to look like this We're going to see that we have bootstrapping of test sprite Obviously we read the product requirements We generate normalized requirements Then we create test plans Then we generate the test code Obviously execute them And finally we analyze and the AI is able to iterate and fix those issues
[译文] [Speaker]: 说到底,工作流程是这样的:我们将看到TestSprite的引导启动;显然我们要读取产品需求;我们生成标准化的需求;然后创建测试计划;接着生成测试代码;显然要执行它们;最后我们进行分析,AI能够迭代并修复那些问题。
[原文] [Speaker]: And we're going to go to the very first step which is telling test sprite hey help me test this project with test sprite
[译文] [Speaker]: 我们将进入第一步,就是告诉TestSprite:“嘿,帮我用TestSprite测试这个项目。”
[原文] [Speaker]: So if I go to my agent and I actually paste it here what it's going to do is it's going to use the MCP server of test sprite
[译文] [Speaker]: 所以如果我去我的代理那里,把这句话粘贴到这里,它要做的是使用TestSprite的MCP服务器。
[原文] [Speaker]: All right so the project is bootstrapped and it opened this kind of a window Let me make this bigger where we can choose whether we want to create backend test or fondant test
[译文] [Speaker]: 好了,项目已经引导启动了,它打开了这样一个窗口。让我把它放大一点,我们可以选择是要创建后端测试还是前端测试。
[原文] [Speaker]: We're going to go for fun and test and I'm going to give it a port Actually it already picked up the correct port and the product specification document is our markdown file So I'm going to choose this file that I have here Open and continue
[译文] [Speaker]: 我们选择前端测试,我要给它一个端口。实际上它已经获取了正确的端口。产品规格文档是我们的Markdown文件,所以我选择我这里的这个文件。打开并继续。
[原文] [Speaker]: Okay So it's creating a summary It already created this folder called test sprite test tmp and we have config summary We have the product requirement Now it's trying to generate a test plan for our front-end project
[译文] [Speaker]: 好的,它正在创建摘要。它已经创建了这个名为 test sprite test tmp 的文件夹,我们有了配置摘要,有了产品需求。现在它正试图为我们的前端项目生成测试计划。
[原文] [Speaker]: And after some time we can see that test sprite actually generated a bunch of files here and it's already running our test So we just need to wait
[译文] [Speaker]: 过了一段时间,我们可以看到TestSprite实际上在这里生成了一堆文件,并且已经在运行我们的测试了。所以我们只需要等待。
📝 本节摘要:
测试运行结束后,讲者展示了TestSprite的仪表盘(Dashboard)。虽然部分测试未通过(归因于之前手动回滚了代码),但这恰好展示了工具的强大之处:它不仅提供了详细的错误日志和测试执行视频,还能通过“Help me fix”指令,让AI根据失败结果自动修复代码库。
[原文] [Speaker]: Okay now we have the test completed and only four out of 15 have passed most likely because the playright test that we had uh actually did not incorporate the new changes with a coupon because I reverted them for this demonstration
[译文] [Speaker]: 好了,现在测试完成了,15个测试中只有4个通过了。这很可能是因为我们之前的Playwright测试实际上没有包含关于优惠券的新更改,因为为了这次演示我把它们回滚了。
[原文] [Speaker]: And let me show you how actually the dashboard of test sprite would look like I have two test runs and one of them are my backend changes that I uh ran previously
[译文] [Speaker]: 让我向你展示TestSprite的仪表盘实际上长什么样。我有两个测试运行记录,其中一个是之前运行的后端更改。
[原文] [Speaker]: So as you can see it actually monitors everything and it can tell you what passed what did not pass
[译文] [Speaker]: 如你所见,它实际上监控着一切,并且能告诉你什么通过了,什么没通过。
[原文] [Speaker]: And the cool thing is it's not only limited to front end or E2E tests but you can also just create a backend test for your API
[译文] [Speaker]: 很酷的一点是,它不仅限于前端或E2E测试,你也可以只为你的API创建后端测试。
[原文] [Speaker]: And by the way in this dashboard a really cool thing is the fact that you can actually drill down into individual tests that have failed to better understand what happened and which steps test sprite actually took under the hood
[译文] [Speaker]: 顺便说一下,在这个仪表盘里,真正酷的一点是你可以深入查看每一个失败的测试,以更好地理解发生了什么,以及TestSprite在底层实际采取了哪些步骤。
[原文] [Speaker]: So here's the video that it's going to record during the execution I'm going to see the errors and I also can see the code that actually got executed for this specific test
[译文] [Speaker]: 这是它在执行过程中录制的视频。我可以看到错误,也可以看到针对这个特定测试实际执行的代码。
[原文] [Speaker]: Now given the fact that we have some failed tests and if we scroll down in the documentation to step nine AI fixes issues we're going to see that there's actually a prompt for letting test sprite fix all of your issues
[译文] [Speaker]: 既然我们有一些失败的测试,如果我们向下滚动文档到第九步“AI修复问题”,我们会看到实际上有一个提示词,可以让TestSprite修复你所有的问题。
[原文] [Speaker]: and it's called help me fix the codebase based on these test results and it's going to look at the results that it generated by itself and then apply the needed fixes How cool is that
[译文] [Speaker]: 这个指令叫“根据这些测试结果帮我修复代码库”,它会查看它自己生成的结果,然后应用所需的修复。这多酷啊!
📝 本节摘要:
讲者总结全文,重申核心观点:在AI工具高度发达的今天,开发者的核心能力在于判断“何时不需要手动写测试”。通过合理利用AI代理和自动化工具,可以极大地解放生产力。
[原文] [Speaker]: if you think this can be useful for you or your team go check it out link is going to be in the description
[译文] [Speaker]: 如果你觉得这对你或你的团队有用,去看看吧,链接会在简介里。
[原文] [Speaker]: So at the end of the day the answer to the question whether developers should be writing tests is they should know when not to write the test
[译文] [Speaker]: 所以归根结底,关于开发者是否应该编写测试这个问题的答案是:他们应该知道什么时候不写测试。
[原文] [Speaker]: All of this can already be automated either by an AI agent or by using a tool like test sprite
[译文] [Speaker]: 所有这些都已经可以通过AI代理或使用像TestSprite这样的工具来实现自动化。
[原文] [Speaker]: And with that said if you agree with me or even if you disagree with me leave a comment if you like and check out my channel for more interesting videos I'm trying to publish every week
[译文] [Speaker]: 话虽如此,如果你同意我,或者即使你不同意我,欢迎留言。如果你喜欢,请查看我的频道获取更多有趣的视频,我正在努力每周发布。
[原文] [Speaker]: And with that said I'm going to see you guys in the next one Goodbye
[译文] [Speaker]: 就说到这,我们在下期视频见。再见。