测试

案例研究:用 Python 构建软件

Mark Pedigo

Principal Data Scientist

路线图

展示项目已完成部分和将要开始部分的路线图。

案例研究:用 Python 构建软件

为何进行自动化测试?

  • 自动化、由软件驱动的测试
  • 无需人工干预
  • 覆盖从简单检查到全面评估

一个机器人在测试代码

案例研究:用 Python 构建软件

自动化测试的益处

  • 提升代码质量
  • 降低风险
  • 改善用户体验
案例研究:用 Python 构建软件

单元测试的作用

  • 单独测试各组件或函数
  • 关注应用的最小单元
  • 给定输入,验证期望输出

放大镜检查方块

案例研究:用 Python 构建软件

什么是 pytest 库?

  • 流行的 Python 测试框架
  • 支持单元测试
  • 以简单、灵活、功能丰富著称
案例研究:用 Python 构建软件

pytest 的优势

  • 简洁易读
  • 强大的 fixtures
  • 可与其他工具集成
案例研究:用 Python 构建软件

单元测试示例

# math.py
def add(a, b):
  return a + b
# test_math.py
import pytest
def test_add():
    assert add(2, 3) == 5
    assert add(-1, 1) == 0
    assert add(0, 0) == 0
    assert add(-5, -7) == -12

在控制台运行:

pytest test_math.py

===== test session starts ===== collected 1 item test_math_functions.py . [100%] ====== 1 passed in 0.03s ======
案例研究:用 Python 构建软件

Ayo berlatih!

案例研究:用 Python 构建软件

Preparing Video For Download...