How to speed up pytest

前端 未结 7 646
醉梦人生
醉梦人生 2020-12-09 01:14

Is there some way to speed up the repeated execution of pytest? It seems to spend a lot of time collecting tests, even if I specify which files to execute on th

相关标签:
7条回答
  • 2020-12-09 01:28

    In bash, try { find -name '*_test.py'; find -name 'test_*.py'; } | xargs pytest.

    For me, this brings total test time down to a fraction of a second.

    0 讨论(0)
  • 2020-12-09 01:37

    In the special case where you are running under cygwin's python, its unix-style file handling is slow. See pytest.py test very slow startup in cygwin for how to speed things up in that special situation.

    0 讨论(0)
  • 2020-12-09 01:37

    If you have some antivirus software running, try turning it off. I had this exact same problem. Collecting tests ran incredibly slow. It turned out to be my antivirus software (Avast) that was causing the problem. When I disabled the antivirus software, test collection ran about five times faster. I tested it several times, turning the antivirus on and off, so I have no doubt that was the cause in my case.

    Edit: To be clear, I don't think antivirus should be turned off and left off. I just recommend turning it off temporarily to see if it is the source of the slow down. In my case, it was, so I looked for other antivirus solutions that didn't have the same issue.

    0 讨论(0)
  • 2020-12-09 01:40

    Using the norecursedirs option in pytest.ini or tox.ini can save a lot of collection time, depending on what other files you have in your working directory. My collection time is roughly halved for a suite of 300 tests when I have that in place (0.34s vs 0.64s).

    If you're already using tox like I am, you just need to add the following in your tox.ini:

    [pytest]
    norecursedirs = docs *.egg-info .git appdir .tox
    

    You can also add it in a free-standing pytest.ini file.

    The pytest documentation has more details on pytest configuration files.

    0 讨论(0)
  • 2020-12-09 01:42

    With xdist you can parallelize pytest runs. It allows even to ship tests to remote machines. Depends on your setup it can speedup quite a bit :)

    0 讨论(0)
  • 2020-12-09 01:47

    For me, adding PYTHONDONTWRITEBYTECODE=1 to my environment variables achieved a massive speedup! Note that I am using network drives which might be a factor.

    • Windows Batch: set PYTHONDONTWRITEBYTECODE=1
    • Unix: export PYTHONDONTWRITEBYTECODE=1
    • subprocess.run: Add keyword env={'PYTHONDONTWRITEBYTECODE': '1'}
    • PyCharm already set this variable automatically for me.

    Note that the first two options only remain active for your current terminal session.

    0 讨论(0)
提交回复
热议问题