Python is better with tests

Brian Okken

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....

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....

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

pytest tips and tricks

This is a set of tips/tricks for learning and using pytest. I’ll probably build on the list, so feel free to share with me items you think should be in the list. Many items really deserve more explanation, and maybe full posts. Let me know if there’s something you’d like more in depth discussion of. Note: I’m not numbering these, because I don’t want to keep track of order. Also, this is more of a brain dump, and not a prioritized list....

February 27, 2023 · 6 min · Brian

Fixing Circular Imports in Python with Protocol

The problem started when I had two classes that needed to talk to each other. Sometimes, classes need to talk to each other in both directions. The following example is made up, but mostly behaves like the original problem. Let’s say I have a Director and an Actor. The Director tells the Actor to do_action(). In order to do the action, the Actor needs to get_data() from the Director. Here’s our director....

January 23, 2023 · 4 min · Brian

Testing with Python 3.12

Python 3.12.0a2 is out. So now may be a great time to get your projects to start testing against 3.12. Note about alpha releases of Python This is from the same link as above: “During the alpha phase, features may be added up until the start of the beta phase (2023-05-08) and, if necessary, may be modified or deleted up until the release candidate phase (2023-07-31). Please keep in mind that this is a preview release and its use is not recommended for production environments....

December 5, 2022 · 3 min · Brian