如何使用Scalene对Pytest测试套件进行性能分析?
用Scalene分析Pytest测试套件的正确方法
Scalene不能直接通过scalene pytest的方式调用,正确的执行方式有两种:
方式一:直接运行pytest模块
使用Scalene的-m参数指定运行pytest模块,这是最直接的方式,还可以附带pytest的所有常规参数:
- 基础命令:
scalene -m pytest - 带参数的示例(指定测试目录、显示详细日志、遇到失败立即停止):
scalene -m pytest tests/ -xvs
方式二:通过脚本封装测试调用
如果需要更灵活的测试配置,可以先写一个简单的Python脚本封装pytest调用,再用Scalene分析这个脚本:
- 创建
run_tests.py脚本:import pytest if __name__ == "__main__": # 这里传入pytest的参数列表,比如指定测试文件、标记等 pytest.main(["tests/unit/", "-m", "slow"]) - 用Scalene运行该脚本:
scalene run_tests.py
额外注意事项
- 确保Scalene已正确安装:
pip install scalene - 若要生成可视化HTML报告,添加
--html参数:scalene --html scalene_report.html -m pytest tests/ - 所有pytest专属参数必须放在
-m pytest之后,避免被Scalene解析错误
内容的提问来源于stack exchange,提问作者snowskeleton
相关产品推荐
相关产品推荐

