一、核心框架对比
特性 | unittest (标准库) | pytest (主流第三方) | nose2 (unittest扩展) | doctest (文档测试) |
安装 | Python标准库 | pip install pytest | pip install nose2 | Python标准库 |
语法风格 | 类继承(JUnit风格) | 函数式+简洁断言 | 类继承+插件扩展 | 文档注释嵌入测试 |
断言方法 | self.assertXxx() | 原生assert | self.assertXxx() | >>> 表达式验证 |
夹具(Fixture) | setUp()/tearDown() | @pytest.fixture (更灵活) | setUp()/插件扩展 | 无 |
参数化测试 | @unittest.parameterized | @pytest.mark.parametrize | @params 装饰器 | 不支持 |
测试发现 | 默认支持 | 智能递归发现 | 增强型发现 | 需指定文档路径 |
插件生态 | 有限 | 丰富(800+插件) | 中等 | 无 |
执行速度 | 中等 | 快(并行支持) | 中等 | 慢(需解析文档) |
二、关键知识点详解
断言方法
- unittest: 需使用特定方法(18+种)
self.assertEqual(a, b) # 值相等
self.assertRaises(Error, func) # 异常断言
- pytest: 直接使用assert,失败时智能输出差异
assert user.name == "Alice" # 失败时显示具体值对比
- 技巧:pytest可通过pytest.raises捕获异常
- 注意:unittest的assertTrue可能掩盖类型错误
夹具(Fixture)
- unittest:类级别的setUpClass() 和实例级别的setUp()
class TestMath(unittest.TestCase):
def setUp(self):
self.calc = Calculator() # 每个测试前初始化
- pytest:通过装饰器实现作用域控制(session/module/class/function)
@pytest.fixture(scope="module")
def db_conn():
conn = create_db_connection()
yield conn # 测试后自动清理
conn.close()
- 技巧:pytest夹具可复用,支持依赖注入
- 注意:避免在夹具中写业务逻辑,保持单一职责
参数化测试
- pytest实践建议:
@pytest.mark.parametrize("input, expected", [
("3+5", 8),
("2*4", 8),
("6/2", 3.0),
])
def test_eval(input, expected):
assert eval(input) == expected
- unittest需借助第三方库(如parameterized)
- 注意:复杂参数建议使用pytest.param添加ID标记
测试发现机制
- 默认规则: 文件匹配:test_*.py 或 *_test.py 类/函数名:Test* 或 test_*
- pytest增强:pytest tests/ --ignore="legacy/" # 排除目录
pytest -k "add" # 关键字过滤
插件生态
- pytest明星插件: pytest-cov: 测试覆盖率 pytest-xdist: 并行测试 pytest-mock: 内置mock支持
- 配置示例(pytest.ini):
[pytest]
addopts = -v --cov=myapp --junitxml=report.xml
三、知识图谱
四、总结与建议
框架选择:
- 新项目首选pytest:语法简洁、扩展性强、报告美观
- 维护旧项目:unittest或nose2(兼容旧代码)
- 文档驱动开发:doctest辅助验证示例
实践建议:
- 命名规范:测试文件/函数明确表达被测对象
- 夹具分层:按作用域(session > module > function)组织资源
- 参数化极限:边界值测试覆盖(0, None, 空列表等)
- 隔离性:每个测试独立运行,避免共享状态
避坑摘记:
- 避免在setUp中初始化耗时资源(用@pytest.fixture(scope="module")替代)
- 不要过度Mock导致测试失真
- 参数化测试数据与逻辑分离(JSON/YAML文件)
优化摘记:
- 使用pytest-xdist并行执行
- 高频测试集用pytest --last-failed仅重跑失败用例
- 耗时资源(数据库)通过夹具共享
建议:大型项目采用 pytest + Factory Boy(数据生成) + Faker(假数据) + Allure(报告) 组合,平衡效率与可维护性。