| 粒度 | 单个函数/类/模块 |
|---|---|
| 特点 | 毫秒级执行、依赖隔离、可高频运行 |
| 常见框架 | pytest、JUnit 5、Vitest、Jest、NUnit |
pytest 示例
pytest 示例
import pytest
def test_withdraw_rejects_overdraft():
account = Account(balance=100)
with pytest.raises(InsufficientFunds):
account.withdraw(150)单元测试重在快速反馈与失败定位;对外依赖用测试替身隔离,避免连真实数据库或网络。