Can I run line_profiler over a pytest test?

后端 未结 3 1802
挽巷
挽巷 2021-02-03 21:28

I have identified some long running pytest tests with

py.test --durations=10

I would like to instrument one of those tests now with something

3条回答
  •  梦毁少年i
    2021-02-03 21:48

    Run pytest like this:

    python3 -m cProfile -o profile -m pytest
    

    You can even pass in optional arguments:

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

    This will create a binary file called profile in your current directory. This can be analyzed with pstats:

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

    This will print the 50 lines with the longest cumulative duration.

提交回复
热议问题