Python is better with tests

Brian Okken

Updates to the Top pytest Plugins - now 200

I’m working on some updates to the Top pytest plugins list. For January’s numbers, I’ve used a tweak on the process. Starting with the process documented by Hugo for the Top PyPI Packages, I’m grabbing the entire data set through BigQuery so that I can grab a nice round number of the top 200 pytest plugins. I am still working my way through the list to come up with an exclusion list, but having control of the data set allows me to play with the process a bit. ...

January 22, 2025 · 1 min · Brian

Testing some tidbits with pytest

I noticed a fun post by Ned Batchelder called Testing some tidbits. The post looks at different ways to see if a string has only 0 or 1 in it. He posted a few ways on Bluesky/Mastodon and got a bunch of replies with more ways. And then wrote a small script to check to see if they worked. It’s a fun post, and from it I learned about: cleandoc - a way to strip leading blank space and maintain code block indentation partition - splitting strings based on a substring Using | to pass imports to eval() - I don’t use eval much. I was a little dissapointed that with a title like “Testing some tidbits”, Ned didn’t use pytest or any other test framework. ...

December 16, 2024 · 3 min · Brian

Finding the top pytest plugins

What are the top downloaded pytest plugins? I want to know this. And I’d like the answer updated regularly. So today I decided to write a script to do that for me. Grab data Let’s start with Top PyPI Packages from Hugo van Kemenade. This list is “A monthly dump of the 8,000 most-downloaded packages from PyPI.” Perfect. Parse Now: Filter for “pytest” in the package name Remove “pytest” itself. I could leave this in for comparison, but it would always be #1. Renumber. Include a link to the pypi.org package page. Throw it all in a table. Pipe it to a markdown blog page. Added on 26-Aug: Grab summaries from pypi.org via Warehouse API. Thanks Jeff Triplett and Hugo for pointing me in that direction. ...

August 25, 2024 · 3 min · Brian

pytest 8 is here

pytest 8.0.0 was released on 17-Jan-2024, and I’m pretty excited about it. I’m not going to cover all fo the changes, I’ll just highlight a few. For full set of changes, see the pytest changelog: Changes in 8.0.0rc1 Changes in 8.0.0rc2 Changes in 8.0.0 Version Compatibility Dropped support for Python 3.7, as it reached EOL last June. Features and Improvements Improved Diffs Improved diffs that pytest prints when an assertion fails, including: ...

January 29, 2024 · 2 min · Brian

Testing argparse Applications

I was asked recently about how to test the argument parsing bit of an application that used argparse. argparse is a built in Python library for dealing with parsing command line arguments for command line interfaces, CLI’s. You know, like git clone <repo address>. git is the application. <repo address> is a command line argument. clone is a sub-command. Well, that might be a bad example, as I’m not going to use subcommands in my example, but lots of this still applies, even if you are using subcommands. Anyway, loads of applications use command line arguments, also sometimes called flags and options. ...

November 16, 2023 · 7 min

pytest slow order

I received a question the other day about combining the notion of marking slow tests and ordering them to run the slow tests first. This post describes a bit of background and a solution to the problem. @pytest.mark.slow It’s possible to mark slow tests with @pytest.mark.slow and then either run or skip the slow tests. To run slow tests: pytest -m slow To skip slow tests: pytest -m "not slow" With the pytest-skip-slow plugin, you can: ...

November 2, 2023 · 3 min

Upgrading to Python 3.12 and my battle with virtualenv cache

edited Oct 3, 2023 with a new fix and a new understanding of the problem Initial title was … Python 3.12 + "AttributeError: module ‘pkgutil’ has no attribute ‘ImpImporter’. Did you mean: ‘zipimporter’?" This is a tale of upgrading Python 3.12 I ran into this error while I was trying to update a project to test on Python 3.12: ... File "/Users/okken/Library/Application Support/virtualenv/wheel/house/pip-23.0.1-py3-none-any.whl/pip/_internal/metadata/importlib/_envs.py", line 123, in _find_eggs_in_zip from pip._vendor.pkg_resources import find_eggs_in_zip File "/Users/okken/Library/Application Support/virtualenv/wheel/house/pip-23.0.1-py3-none-any.whl/pip/_vendor/pkg_resources/__init__.py", line 2164, in <module> register_finder(pkgutil.ImpImporter, find_on_path) ^^^^^^^^^^^^^^^^^^^ AttributeError: module 'pkgutil' has no attribute 'ImpImporter'. Did you mean: 'zipimporter'? py312: internal error Specifically: ...

October 2, 2023 · 2 min

pytest Primary Power Course Is Ready

Everything you need to get started with pytest and use it effectively. Learn about: Test Functions Structure functions effectively Fixtures setup, teardown, and so much more Builtin Fixtures many common testing tasks are pre-built and ready to use Parametrization turn one test into many test cases Markers Builtin markers provided by pytest Custom markers for test selection Combining markers and fixtures to alter pytest behavior Available now Grab pytest Primary Power now, and start testing more effectively today. ...

September 12, 2023 · 1 min

pytest Course

Introducing “The Complete pytest Course” series. This is a video course series based on “Python Testing with pytest, 2nd edition”. Basically, the bundle includes all courses in the series, available as he videos get completed, and includes a private discussion group and repo. See The Complete pytest Course for more info.

August 21, 2023 · 1 min · Brian

Sharing is Caring - Sharing pytest Fixtures - PyCascades 2023

Slides and code and such for a talk for PyCascades 2023. Talk page: Sharing is Caring - Sharing pytest Fixtures Summary: pytest rocks, obviously. When people start using pytest as a team, they often come up with cool fixtures that would be great to share across projects. In fact, many great Python packages come pre-loaded with pytest fixtures. This talk describes how easy it is to share fixtures using the pytest plugin model. ...

March 19, 2023 · 1 min · Brian