![](/img/trans.png)
[英]pytest: Best way to add test description (Long test name) in the report with out renaming the files or functions
[英]pytest: Is there a way to report memory usage of a test?
我检查了 pytest 文档,但找不到任何相关内容。 我知道 pytest --durations=0 将打印出所有测试的运行时间。 有没有办法让 pytest 也打印出 function 消耗的峰值 memory 使用量? 否则,我可能只使用下面的装饰 function。 但我想知道是否有更好的方法来做到这一点。
from functools import wraps
def mem_time(func):
@wraps(func)
def wrapper(*args, **kwargs):
# Start of function
r0 = resource.getrusage(resource.RUSAGE_SELF)
t0 = datetime.datetime.now()
# Call function
status = func(*args, **kwargs)
# End of function
r1 = resource.getrusage(resource.RUSAGE_SELF)
t1 = datetime.datetime.now()
sys.stderr.write('{}: utime {} stime {} wall {}\n'.format(func.__name__,
datetime.timedelta(seconds=r1.ru_utime - r0.ru_utime),
datetime.timedelta(seconds=r1.ru_stime - r0.ru_stime),
t1 - t0))
sys.stderr.write('{}: mem {} MB ({} GB)\n'.format(func.__name__,
(r1.ru_maxrss - r0.ru_maxrss) / 1000.0,
(r1.ru_maxrss - r0.ru_maxrss) / 1000000.0))
return status
return wrapper
pytest-monitor一个新的 pytest 插件有助于监控资源使用情况、时间、memory 等。 所有指标都存储在 sqlite 数据库中以进行后期分析
从 pypi 或 conda-forge 查看pytest-monitor :
例子
$ pytest --db ./monitor.db
$ sqlite3 ./monitor.db
sqlite>.headers on
sqlite> select ITEM, MEM_USAGE from TEST_METRICS;
ITEM|MEM_USAGE
test_api/test_data_get[/data/listdsh]|17.5703125
test_api/test_data_get[/data/listrecipients]|17.97265625
test_api/test_data_get[/data/getuserdetails]|18.125
希望这会有所帮助
Memory 剖析:
没有插件可以从 pytest(据我所知)获取 memory 分析。 使用以下链接进行 memory 分析
https://github.com/fabianp/memory_profiler
其他参考: https://stackoverflow.com/a/43772305/9595032
累计次数:
https://pypi.org/project/pytest-profiling/
但是要获得使用插件pytest-profiling
的所有调用的累积时间
pip install pytest-profiling
用法:
pytest -s -v [file_name] --profile
output 看起来有点像这样
Profiling (from /home/work/project/analytics/testing/TestCases/cloud/prof/combined.prof):
Wed Nov 20 12:09:47 2019 /home/work/project/analytics/testing/TestCases/cloud/prof/combined.prof
437977 function calls (380211 primitive calls) in 3.866 seconds
Ordered by: cumulative time
List reduced from 683 to 20 due to restriction <20>
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 3.866 3.866 runner.py:107(pytest_runtest_call)
1 0.000 0.000 3.866 3.866 python.py:1457(runtest)
1 0.000 0.000 3.866 3.866 hooks.py:275(__call__)
1 0.000 0.000 3.866 3.866 manager.py:59(<lambda>)
1 0.000 0.000 3.866 3.866 manager.py:65(_hookexec)
1 0.000 0.000 3.866 3.866 callers.py:157(_multicall)
1 0.000 0.000 3.866 3.866 python.py:188(pytest_pyfunc_call)
1 0.001 0.001 3.866 3.866 test_abc.py:46(test_abc)
1 0.000 0.000 3.865 3.865 test_abc.py:9(run_abc_test)
1 0.001 0.001 3.854 3.854 dataAnalyzer.py:826(sanitize_data)
1 0.000 0.000 3.773 3.773 Analyzer.py:563(Traffic)
1 0.000 0.000 3.772 3.772 traffic.py:83(false_alarm_checks)
3 0.000 0.000 3.767 1.256 api.py:60(get)
3 0.000 0.000 3.765 1.255 api.py:135(_get_from_overpass)
3 0.000 0.000 3.765 1.255 api.py:101(post)
3 0.000 0.000 3.765 1.255 api.py:16(request)
3 0.000 0.000 3.762 1.254 sessions.py:445(request)
3 0.000 0.000 3.759 1.253 sessions.py:593(send)
3 0.000 0.000 3.757 1.252 adapters.py:393(send)
3 0.000 0.000 3.755 1.252 connectionpool.py:447(urlopen)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.