You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Scalene对Pytest测试套件进行性能分析?

用Scalene分析Pytest测试套件的正确方法

Scalene不能直接通过scalene pytest的方式调用,正确的执行方式有两种:

方式一:直接运行pytest模块

使用Scalene的-m参数指定运行pytest模块,这是最直接的方式,还可以附带pytest的所有常规参数:

  • 基础命令:
    scalene -m pytest
    
  • 带参数的示例(指定测试目录、显示详细日志、遇到失败立即停止):
    scalene -m pytest tests/ -xvs
    

方式二:通过脚本封装测试调用

如果需要更灵活的测试配置,可以先写一个简单的Python脚本封装pytest调用,再用Scalene分析这个脚本:

  1. 创建run_tests.py脚本:
    import pytest
    
    if __name__ == "__main__":
        # 这里传入pytest的参数列表,比如指定测试文件、标记等
        pytest.main(["tests/unit/", "-m", "slow"])
    
  2. 用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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 19:27:22