Python Testing with pytest: Simple, Rapid, Effective, and Scalable

Watch the live stream:

Watch on YouTube

About the show

Sponsored by Datadog: pythonbytes.fm/datadog

Michael #1: Physics Breakthrough as AI Successfully Controls Plasma in Nuclear Fusion Experiment

  • Interesting break through using AI
  • Is Python at the center of it?
  • With enough digging, the anwswer is yes, and we love it!

Brian #2: PEP 680 -- tomllib: Support for Parsing TOML in the Standard Library

  • Accepted for Python 3.11
  • This PEP proposes basing the standard library support for reading TOML on the third-party library tomli

Michael #3: Thread local

  • threading.local: A class that represents thread-local data. Thread-local data are data whose values are thread specific.
  • Just create an instance of local (or a subclass) and store attributes on it
  • You can even subclass it.

Brian #4: What is a generator function?

  • Trey Hunner
  • Super handy, and way easier than you think, if you’ve never written your own.
  • Really, it’s just a function that uses yield instead of return and supplies one element at a time instead of returning a list or dict or tuple or other large structure.
  • Some details
    • generator functions return generator objects
    • generator objects are on pause and use the built in next() function to get next item.
    • they raise StopIteration when done.
    • Most generally used from for loops.
    • Generator objects cannot be re-used when exhausted
      • but you can get a new one with the next for loop you use. So, it’s all good.

Michael #5: dirty-equals

  • via Will McGugan, by Samual Colvin
  • Doing dirty (but extremely useful) things with equals.

``` from dirty_equals import IsPositive

assert 1 == IsPositive assert -2 == IsPositive # this will fail!

```

``` user_data = db_conn.fetchrow('select * from users') assert user_data == { 'id': IsPositiveInt, 'username': 'samuelcolvin', 'avatar_file': IsStr(regex=r'/[a-z0-9-]{10}/example.png'), 'settings_json': IsJson({'theme': 'dark', 'language': 'en'}), 'created_ts': IsNow(delta=3), }

```

Brian #6: Commitizen

  • from the docs
    • Command-line utility to create commits with your rules. Defaults: Conventional commits
    • Display information about your commit rules (commands: schema, example, info)
    • Bump version automatically using semantic versioning based on the commits. Read More
    • Generate a changelog using Keep a changelog
  • considering using for consistent commit message formatting
  • can be used with python-semantic-release for automatic semantic versioning
  • learned about it in 10 Tools I Wish I Knew When I Started Working with Python
  • questions
    • anyone using this or something similar?
    • does this make sense for small to medium sized projects? or overkill?

Extras:

  • pytest book
    • 40% off sale continues through March 19 for eBook
    • Amazon lists the book as “shipping in 1-2 days”, as of March 2

Michael:

  • Pronouncing the Python Walrus operator := as “becomes”
  • Via John Sheehan: String methods startswith() and endswith() can take a tuple as its first argument that lets you check for multiple values with one call:

``` >>> x = "abcdefg" >>> x.startswith(("ab", "cd", "ef"), 2) True

```

Joke: CS Background