自动化测试是软件开发中安全使用 AI 的关键。

147 阅读2分钟

作者认为,强大的自动化测试套件能够显著提升在编码中使用大型语言模型 (LLM) 的有效性和安全性。测试提供即时、可靠的验证,使开发人员能够自信地使用 LLM 生成代码,并通过测试失败快速识别不符合预期的输出。这种安全保障还能在保持正确性的前提下,使用 LLM 进行重构。此外,LLM 可作为高效的结对编程助手,协助编写测试,包括模拟等复杂任务。文章总结说,项目测试套件的健康状况是决定开发人员能否有效利用人工智能辅助的关键因素。

主要内容

  • 1. 自动化测试显著降低了使用 LLM 进行编码的风险。

    测试提供了一个安全保障,使开发人员能够验证 LLM 生成的代码的正确性,并在开发过程的早期发现错误或不良模式。

  • 2. LLM 可以协助编写和维护测试套件本身。

    利用 LLM 来帮助编写测试,包括复杂的模拟,如同拥有一个全天候的结对编程伙伴,从而提高了测试效率。

  • 3. 测试套件的质量直接影响 LLM 的应用效果。

    拥有全面测试覆盖率的开发人员可能会发现 LLM 在编码任务中更有帮助,更不易遇到阻碍。

文章金句

  • 我所做的 所有 代码都有自动化测试。
  • 大大地 降低了我使用 LLM 的风险。
  • LLM 也可以帮助编写测试。我终于找到了一个全天候、并精通 unittest.mock 的结对编程伙伴!
  • 下次有人抱怨说他们发现 LLM 在他们的编程工作中弊大于利时,我会尽量记住询问他们的测试套件的健康状况。

英文原文

AI-assisted development needs automated tests

I wonder if one of the reasons I'm finding LLMs so much more useful for coding than a lot of people that I see in online discussions is that effectively all of the code I work on has automated tests.

I've been trying to stay true to the idea of a Perfect Commit - one that bundles the implementation, tests and documentation in a single unit - for over five years now. As a result almost every piece of (non vibe-coding) code I work on has pretty comprehensive test coverage.

This massively derisks my use of LLMs. If an LLM writes weird, convoluted code that solves my problem I can prove that it works with tests - and then have it refactor the code until it looks good to me, keeping the tests green the whole time.

LLMs help write the tests, too. I finally have a 24/7 pair programmer who can remember how to use unittest.mock!

Next time someone complains that they've found LLMs to be more of a hindrance than a help in their programming work, I'm going to try to remember to ask after the health of their test suite.