Brought to you by Michael and Brian - take a Talk Python course or get Brian's pytest book

#306: Some Fun pytesting Tools

Published Wed, Oct 19, 2022, recorded Tue, Oct 18, 2022
Watch this episode on YouTube
Play on YouTube
Watch the live stream replay

About the show

Sponsored by Microsoft for Startups Founders Hub.

Brian #1: Awesome pytest speedup

  • Neyts Zupan
  • A checklist of best practices to speed up your pytest suite.
  • as a talk at Plone NAMUR 2022
  • Measure first
  • Then make sure (all items have explanations)
    • Hardware is fast
      • use a faster computer
      • also try a self-hosted runner
        • 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
    • Collection is fast
      • utilize norecursedirs and specifying the location of the tests, either on the command line or with testpaths
      • might help
    • Built-in pytest plugins are disabled
      • try -p no:pastebin -p no:nose -p no:doctest
    • Only a subset of tests are executed
    • Network access is disabled
    • Disk access is disabled
      • interesting idea
    • Database access is optimized
      • great discussion here, including using truncate and rollback.
    • Tests run in parallel
  • Then keep them fast
    • monitor test speed

Michael #2: Strive to travel without a laptop

  • Prompt from Panic for SSH on iThings
  • for an editor on iPad
  • Push to branch for continuous deployment
  • BTW, Apple could just make M1 iPads boot to macOS rather than chase silly multi windowing systems (stage manager, etc, etc)

Brian #3: Some fun tools from the previous testing article

  • hyperfine for timing the whole suite
  • pytest --``durations 10 for finding test times of slowest 10 tests
    • leave the 10 off to find times of everything, sorted
  • pyinstrument for profiling with nice tree structures
  • pytest-socket disables network calls with --disable-socket, helping to find tests that use network calls.
  • pyfakefs, 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.”

Michael #4: Refurb

  • A tool for refurbishing and modernizing Python codebases
  • Think of it as suggesting the pythonic line of code.
  • A little sampling of what I got on Talk Python Training
    • [FURB106]: Replace x.replace("\t", " ") with x.expandtabs(1)
    • [FURB131]: Replace del x[y] with x.pop(y)
    • [FURB131]: Replace del x[y] with x.pop(y)
    • [FURB109]: Replace not in [x, y, z] with not in (x, y, z)
    • [FURB131]: Replace del x[y] with x.pop(y)
    • [FURB131]: Replace del x[y] with x.pop(y)
    • [FURB131]: Replace del x[y] with x.pop(y)
    • [FURB123]: Replace list(x) with x.copy()
  • You don’t always want the change, can suppress the recommendation with either a CLI flag or comment.



Joke: Tests pass

Want to go deeper? Check our projects