gpt4 book ai didi

python - 我可以通过 pytest 测试运行 line_profiler 吗?

转载 作者:IT老高 更新时间:2023-10-28 20:33:01 30 4
gpt4 key购买 nike

我已经用

确定了一些长期运行的 pytest 测试
py.test --durations=10

我现在想用 line_profiler 或 cprofile 之类的东西来检测其中一个测试。我真的很想从测试本身获取配置文件数据,因为 pytest 设置或拆除很可能是缓慢的一部分。

但是考虑到 line_profiler 或 cprofile 通常如何参与,我不清楚如何使它们与 pytest 一起使用。

最佳答案

像这样运行 pytest:

python3 -m cProfile -o profile -m pytest

你甚至可以传入可选参数:

python3 -m cProfile -o profile -m pytest tests/worker/test_tasks.py -s campaigns

这将在您的当前目录中创建一个名为 profile 的二进制文件。这可以用 pstats 来分析:

import pstats
p = pstats.Stats('profile')
p.strip_dirs()
p.sort_stats('cumtime')
p.print_stats(50)

这将打印累积持续时间最长的 50 行。

关于python - 我可以通过 pytest 测试运行 line_profiler 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18830232/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com