Developing Software with Tests
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....
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....
Installing Python 3.11 on Mac or Windows
Installing Python from python.org The easiest and most obvious (to me) way to install the latest version of Python on either Mac or Windows is: Go to python.org Hover over the “Downloads” link in the navigation. This will detect if you are on Mac or Windows and present you with a button called “Python 3.11.0” (or whatever the latest version is). Click that button. This downloads an installer. Run the installer....
Talk - Sharing is Caring - pytest fixture edition
Sharing is Caring, pytest fixture edition I gave a talk at PyBay 2022 with the above title. It’s about sharing fixtures. The source code, and the slides, are at github.com/okken/pytest_fixture_sharing.
Current Git CLI workflow
Workflow Most of my interactions with git CLI, especially for quick changes, is: $ git checkout main $ git pull $ git checkout -b okken_something < code changes > $ git commit -a -m 'quick message' $ git push Then the code review and merge happen on the server. Commands Let’s break that down. git checkout main Start at the main branch. git pull Grab any changes from remote repo....
Lean TDD
Preface Lean TDD is an attempt to reconcile some conflicting aspects of Test Driven Development and Lean Software Development. I’ve mentioned Lean TDD on the podcast a few times and even tried to do a quick outline at the end of episode 162. This post is a more complete outline, or at least a first draft. In audio form The initial version of this post is also available in audio form as Test & Code, episode 180....
Beta 9.0 of Python Testing with pytest, 2nd ed, available.
The Beta 9.0 is available for Python Testing with pytest, 2nd edition, as of Feb 9, 2022. With this 9th beta release, copy edit and indexing are complete. Next it goes to layout, then to printing. Amazon has it for pre-order, listing March 22 as availability. I think we should be able to hit that. I’m excited.
Python Packaging Struggle for pytest Plugins
Recently, Pradyun posted a tweet commenting not on the state of Python packaging, but more on the state of the documentation and guidance. }} -- Now, at the time, I figured that was the case for people new to packaging, not myself. I’m not really an old timer with packaging, but: I have a few published packages. I’ve used both Flit and setuptools. I’ve interviewed Brett Cannon 3 times on the packaging in episodes of Test & Code....
Pinning Application Dependencies with pip-tools compile
pip-tools has more functionality than this, but compile alone is quite useful. Start with a loose list of dependencies in requirements.in: typer rich The requirements.in file can have things like >= and such if you have some restrictions on your dependencies. Now install pip-tools: pip install pip-tools Then, in create a requirements.txt file with compile: pip-compile requirements.in or: python -m piptools compile requirements.in The output will be shown on stdout, but also in requirements....
Python Testing with pytest
Simple, Rapid, Effective, and Scalable by Brian Okken Do less work when testing your Python code, but be just as expressive, just as elegant, and just as readable. The pytest testing framework helps you write tests quickly and keep them readable and maintainable—with no boilerplate code. Using a robust yet simple fixture model, it’s just as easy to write small tests with pytest as it is to scale up to complex functional testing for applications, packages, and libraries....