使用 Copilot 进行测试与代码质量

使用 GitHub Copilot 进行软件开发

Thalia Barrera

AI Engineering Curriculum Manager, DataCamp

本章要点回顾

 

  • 代码补全 — 边写边给出建议
  • Copilot Chat — 提问、获解释、重构代码
  • Agent 模式 — 自主多步任务
  • 自定义工具 — 扩展 Copilot 能力
使用 GitHub Copilot 进行软件开发

现在来应用它

nanobanana: half: 开发者旅程:一条路径上有三枚里程碑图标,代表测试、安全、性能,从代码编辑器通往已部署的精良应用,扁平矢量插图

 

  • 测试 — 在上线前捕获缺陷
  • 安全 — 及早发现漏洞
  • 性能 — 编写可扩展代码
使用 GitHub Copilot 进行软件开发

Copilot 让测试更轻松

nanobanana: half: 一位 AI 助手在开发者屏幕上生成测试代码,测试结果旁有绿色对号,扁平矢量插图

 

  • ✔ 生成完整测试套件
  • 💡 捕捉边界情况
  • 🤖 处理样板代码
使用 GitHub Copilot 进行软件开发

易被忽略的边界情况

 

def calculate_discount(price, discount_pct):
    discount = price * (discount_pct / 100)
    return price - discount

calculate_discount(50, 20)  # → 40.0 ✓

 

但还有……

  • 负数 price
  • discount_pct > 100
  • 数量为零的订单
使用 GitHub Copilot 进行软件开发

[screencast]

使用 GitHub Copilot 进行软件开发

[screencast]

使用 GitHub Copilot 进行软件开发

何时深入细化

nanobanana: half: 一位专注的软件开发者在笔记本上,通过 AI 聊天界面撰写消息

简单场景

  • /tests

 

特殊场景

Write pytest unit tests for calculate_discount covering: negative price, discount > 100%, and empty item list. Use parametrize for edge cases.

使用 GitHub Copilot 进行软件开发

超越单元测试

nanobanana: half: 一个测试金字塔:底层为"单元测试",中层为"集成测试",顶层为"端到端测试"

 

  • 单元 — 用 /tests 或简短提示
  • 集成 — 描述组件如何连接
  • 端到端 — 使用完整代码库上下文
使用 GitHub Copilot 进行软件开发

集成测试

 

  • 为每个相关文件添加 #file:
  • 描述交互mock 策略
  • 测试失败时用 /fixTestFailure

 

Write integration tests for the checkout flow. Use #file:cart.py and #file:payment.py. Mock the payment gateway and assert that order status updates correctly.

使用 GitHub Copilot 进行软件开发

端到端测试

 

  • @workspace 自动发现相关代码
  • PlaywrightCypress 配合良好
  • #terminalLastCommand 调试失败

 

@workspace Write Playwright end-to-end tests for the user registration flow. Assert that the confirmation email is triggered.

使用 GitHub Copilot 进行软件开发

[screencast]

使用 GitHub Copilot 进行软件开发

Passons à la pratique !

使用 GitHub Copilot 进行软件开发

Preparing Video For Download...