Quantcast
Channel: Planet Python
Viewing all articles
Browse latest Browse all 22873

Python Bytes: #306 Some Fun pytesting Tools

$
0
0
<p><strong>Watch the live stream:</strong></p> <a href='https://www.youtube.com/watch?v=O5OyphLaDU8' style='font-weight: bold;'>Watch on YouTube</a><br> <br> <p><strong>About the show</strong></p> <p>Sponsored by <a href="http://pythonbytes.fm/foundershub2022"><strong>Microsoft for Startups Founders Hub</strong></a>.</p> <p><strong>Brian #1:</strong> <a href="https://github.com/zupo/awesome-pytest-speedup"><strong>Awesome pytest speedup</strong></a></p> <ul> <li>Neyts Zupan</li> <li>A checklist of best practices to speed up your pytest suite.</li> <li>as a talk at <a href="https://www.youtube.com/watch?v=uvkSOaFYsLo">Plone NAMUR 2022</a></li> <li>Measure first</li> <li>Then make sure (all items have explanations) <ul> <li>Hardware is fast <ul> <li>use a faster computer</li> <li>also try a self-hosted runner <ul> <li>seriously, a dedicated computer (or a few) for making test runs faster might be worth it. CI resources are usually slower in cloud than local, and even expensive VM farms are often slower. Try local </li> </ul></li> </ul></li> <li>Collection is fast <ul> <li>utilize <code>norecursedirs</code> and specifying the location of the tests, either on the command line or with <a href="https://docs.pytest.org/en/7.1.x/reference/reference.html#confval-testpaths"><code>testpaths</code></a></li> </ul></li> <li>PYTHONDONTWRITEBYTECODE=1 is set <ul> <li>might help</li> </ul></li> <li>Built-in pytest plugins are disabled <ul> <li>try <code>-p no:pastebin -p no:nose -p no:doctest</code></li> </ul></li> <li>Only a subset of tests are executed <ul> <li>Especially when developing or debugging, run a subset and <a href="https://pypi.org/project/pytest-skip-slow/">skip the slow tests</a>.</li> </ul></li> <li>Network access is disabled <ul> <li><a href="https://pypi.org/project/pytest-socket/"><code>pytest-socket</code></a> can make sure of that</li> </ul></li> <li>Disk access is disabled <ul> <li>interesting idea</li> </ul></li> <li>Database access is optimized <ul> <li>great discussion here, including using truncate and rollback.</li> </ul></li> <li>Tests run in parallel <ul> <li><a href="https://pypi.org/project/pytest-xdist">pytest-xdist</a> or similar</li> </ul></li> </ul></li> <li>Then keep them fast <ul> <li>monitor test speed</li> </ul></li> </ul> <p><strong>Michael #2:</strong> Strive to travel without a laptop</p> <ul> <li><a href="https://panic.com/prompt/"><strong>Prompt from Panic</strong></a> for SSH on iThings</li> <li><a href="https://github.dev"><strong>github.dev</strong></a> for an editor on iPad</li> <li>Push to branch for continuous deployment</li> <li>BTW, Apple could just make M1 iPads boot to macOS rather than chase silly multi windowing systems (stage manager, etc, etc)</li> </ul> <p><strong>Brian #3:</strong> <strong>Some fun tools from the previous testing article</strong></p> <ul> <li><a href="https://github.com/sharkdp/hyperfine">hyperfine</a> for timing the whole suite</li> <li><code>pytest</code> <code>--``durations 10</code> for finding test times of slowest 10 tests <ul> <li>leave the <code>10</code> off to find times of everything, sorted</li> </ul></li> <li><a href="https://pyinstrument.readthedocs.io/en/latest/home.html">pyinstrument</a> for profiling with nice tree structures <ul> <li>and <a href="https://pyinstrument.readthedocs.io/en/latest/guide.html#profile-pytest-tests">how to use it with pytest</a></li> </ul></li> <li><a href="https://pypi.org/project/pytest-socket/">pytest-socket</a> disables network calls with <code>--disable-socket</code>, helping to find tests that use network calls.</li> <li><a href="https://github.com/pytest-dev/pyfakefs">pyfakefs</a>, a fake file system that mocks the Python file system modules. “Using pyfakefs, your tests operate on a fake file system in memory without touching the real disk.”</li> <li><a href="https://github.com/apps/blueracer-io">BlueRacer.io</a></li> </ul> <p><strong>Michael #4:</strong> <a href="https://github.com/dosisod/refurb"><strong>Refurb</strong></a></p> <ul> <li>A tool for refurbishing and modernizing Python codebases</li> <li>Think of it as suggesting the pythonic line of code.</li> <li>A little sampling of what I got on Talk Python Training <ul> <li>file.py:186:25 [FURB106]: Replace <code>x.replace("\t", "")</code> with <code>x.expandtabs(1)</code></li> <li>file.py:128:17 [FURB131]: Replace <code>del x[y]</code> with <code>x.pop(y)</code></li> <li>file.py:103:17 [FURB131]: Replace <code>del x[y]</code> with <code>x.pop(y)</code></li> <li>file.py:112:39 [FURB109]: Replace <code>not in [x, y, z]</code> with <code>not in (x, y, z)</code></li> <li>file.py:45:5 [FURB131]: Replace <code>del x[y]</code> with <code>x.pop(y)</code></li> <li>file.py:81:21 [FURB131]: Replace <code>del x[y]</code> with <code>x.pop(y)</code></li> <li>file.py:143:9 [FURB131]: Replace <code>del x[y]</code> with <code>x.pop(y)</code></li> <li>file.py:8:50 [FURB123]: Replace <code>list(x)</code> with <code>x.copy()</code></li> </ul></li> <li>You don’t always want the change, can suppress the recommendation with either a CLI flag or comment.</li> </ul> <p><strong>Extras</strong> </p> <p>Michael:</p> <ul> <li>Back on <a href="https://pythonbytes.fm/episodes/show/54/pyannotate-your-way-to-the-future"><strong>episode 54</strong></a> in 2017 we discussed python apps in systemd daemons. <ul> <li>Multiprocessing allows for a cool way to save on server memory</li> <li>Do the scheduled work a multiprocessing.Process</li> <li>Here’s <a href="https://python-bytes-static.nyc3.digitaloceanspaces.com/glances-view.png"><strong>an example from Talk Python Training</strong></a></li> </ul></li> <li>Completely rewrote <a href="https://twitter.com/TalkPython/status/1580691498416615426"><strong>search UI for Talk Python courses</strong></a></li> <li><a href="https://youtu.be/9K--N8frWq0"><strong>Google analytics is now illegal</strong></a>? </li> <li><a href="https://www.jetbrains.com/fleet/"><strong>Fleet</strong></a> is <em>finally</em> in public preview</li> <li>I’ll be on <a href="https://blog.jetbrains.com/pycharm/2022/10/webinar-django-in-pycharm/"><strong>a JetBrains/PyCharm webcast</strong></a> Thursday.</li> </ul> <p><strong>Joke:</strong> <a href="https://twitter.com/PR0GRAMMERHUM0R/status/1578943360705781762"><strong>Tests pass</strong></a></p>

Viewing all articles
Browse latest Browse all 22873

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>