![]() |
VOOZH | about |
11th February 2020
I’ve been making a lot of progress on Datasette Cloud this week. As an application that provides private hosted Datasette instances (initially targeted at data journalists and newsrooms) the majority of the code I’ve written deals with permissions: allowing people to form teams, invite team members, promote and demote team administrators and suchlike.
The one thing I’ve learned about permissions code over the years is that it absolutely warrants comprehensive unit tests. This is not code that can afford to have dumb bugs, or regressions caused by future development!
I’ve become a big proponent of pytest over the past two years, but this is the first Django project that I’ve built using pytest from day one as opposed to relying on the Django test runner. It’s been a great opportunity to try out pytest-django, and I’m really impressed with it. It maintains my favourite things about Django’s test framework—smart usage of database transactions to reset the database and a handy test client object for sending fake HTTP requests—and adds all of that pytest magic that I’ve grown to love.
It also means I get to use my favourite trick for productively writing unit tests: the combination of pytest and Black, the “uncompromising Python code formatter”.
Datasette and sqlite-utils, and those are by far the best tested pieces of software I’ve ever released.
I started doing this around two years ago, and I’ve held off writing about it until I was confident I understood the downsides. I haven’t found any yet: I end up with a robust, comprehensive test suite and it takes me less than half the time to write the tests than if I’d been hand-crafting all of those comparisons from scratch.
Unrelated to Datasette Cloud, I also shipped twitter-to-sqlite 0.16 with a new command for importing your Twitter friends (previously it only had a command for importing your followers).
In bad personal motivation news… I missed my weekly update to Niche Museums and lost my streak!
This is How to cheat at unit tests with pytest and Black by Simon Willison, posted on 11th February 2020.
Part of series My open source process
Next: Things I learned about shapefiles building shapefile-to-sqlite
Previous: Weeknotes: Shaving yaks for Datasette Cloud
Sponsor me for $10/month and get a curated email digest of the month's most important LLM developments.
Pay me to send you less!
Sponsor & subscribeWeeknotes: How to cheat at unit tests with pytest and Black https://t.co/8NaV4t4cy7
— Simon Willison (@simonw) February 11, 2020