Speed Up Your Django Tests
✨ Updated for Django 3.2✨ - see blog post.
This book is a practical guide to making your Django project's tests faster. It has many tips and tricks that apply to all projects, big and small. And it covers the two most popular test runners: Django's test framework and pytest.
It's based on my experience speeding up various Django projects' test suites, improving Django's own testing framework, and creating pytest plugins.
The book contains 13 chapters:
Opening notes, how to use the book.
A tour of the various tools you can use to change how your tests run.
The importance of profiling, with walkthroughs using two different profilers on a test suite.
- Easy Wins
11 things you can do in 5 minutes to speed up your test suite.
The importance of upgrades, and some how-to guidance to get them done easily.
How to use test suite parallelization to boost your tests.
Reduce the overhead of building your test database.
- Database Configuration
Tweak your database's configuration to get more speed.
- CI Configuration
Tweak your CI configuration to boost test speed too.
- Test Structure
Notes on the best structure for fast, accurate tests, and ways you can restructure.
- Test Data
The best strategies for creating data in your tests.
- Targeted Mocking
A guide to mocking, and the best libraries for mocking out specific things such as HTTP requests and time.
Closing notes with extra links.
If you'd like a sample of the content, check out these blog posts, which contain extracts:
- How to Unit Test a Django Form
- Disable Instrumentation Packages During Tests
- How to Use Django's Parallel Testing on macOS With Python 3.8+
Who are you?
Hi, I'm Adam Johnson. I'm an author and solo consultant working with Django and Python.
I've used Django since 2012 and have been a member of the Django Project Steering Council since the 2.2 release cycle.
Is this an official Django product?
The Django Software Foundation didn't help me out with this book, and this is in no way "official". It's a product of A.W.S. (Adam's Web Services) Ltd.
You should make a donation to the DSF separately!
A PDF watermarked with your email address, an ePub, and a resources zip file including images and django projects for all the code examples.
Will you do a printed version?
Unfortunately, I can’t afford to do print versions given that I (aim to) update at least every eight months for each new Django version, and the audience is so global.
Do you offer any discounts?
Gumroad’s purchasing power parity feature will automatically offer you a discount based on your location.
Outside of that, I offer occasional seasonal discounts. Follow my blog, Mastodon, or Twitter to be notified of these.
Can I get an invoice?
Gumroad will send you a receipt with a "generate" button to turn it into an invoice, so you can really make your employer pay!
Can I purchase with an alternative method (e.g. direct bank transfer)?
Due to accounting limitations, I'm only able to sell my product on Gumroad. If it doesn't work for you please try emailing Gumroad support (email@example.com) and CC'ing me.
”I learned more from those two books than years of coding in Django”
—Nick McCullum on Twitter
”Adam's wonderful book on writing Django tests... I own and recommend it.”
—Jeff Triplett on Twitter
“Today I ran into some tests that were taking minutes. I decided to that was reason enough to finally buy your book. An hour later, I've dropped the test time from 3 minutes to 10s. Thanks!”
—Tim Schilling on Twitter
“I've been obsessing about fast tests for years, and still learned loads in @AdamChainz's great book. It's super dense with medium-to-advanced tips. Go pick it up!”
—Maik Hoepfel on Twitter
“Just finished reading "Speed Up Your Django Tests" by @AdamChainz. It's a good read, found a few nuggets in there. Shame it wasn't available three years ago, but if you're starting your #Django testing adventure today, don't even think twice, grab it”
—Tim Kamanin on Twitter
“So nearly through @AdamChainz’s book. I have to say it’s very good.
Much more than just tests in there.
Not for a novice, but good for everyone from levelling-up to advanced. Recommended.”
—Carlton Gibson on Twitter
“While @adamchainz is working on his new book "Boost Your Django DX" I was thinking about how great his other book "Speed Up Your Django Tests" is. It's great with lots of great tips that really helped me in better understanding how to write fast tests. But the best part is @adamchainz has tips for the Django Test Framework AND pytest. Would highly recommend BOTH books.”
—Ryan Cheley on Twitter
“There are a slew of good tips in there that I'm now planning to bring into my company's test suite. I highly recommend it!”
—Matt Layman on Twitter
“I liked this book. People who are new in testing journey or a Django developer working with tests, this is for them. Well organized and you cover a very basic stuff that will help beginners to understand testing well.”
—Ngazetungue Muheue on Twitter
”Whenever you find yourself waiting for that test build to finish running - flip a chapter, and you’ll find something to improve. For those who want to go deeper down the rabbit hole, the book is sprinkled generously with references and links.”
—Julius Šėporaitis (Technical Reviewer for the book)
“If you already have a basic understanding of Python, Django, and the tests are already running in your Django project, then this Book is for you....
I also sent an email to Adam to ask some questions and got a quick response, which is incredible!“
“This book contains invaluable insights, and I really like the way you organized it.”
—Caio Ariede on Twitter
“I just finished reading “Speed Up Your Django Tests" by @adamchainz. It’s awesome and i have learned a lot of things. I highly recommend it.”
—@massumo on Twitter
”Great book! And not only for testing Django apps, lots of great general Python dev tips along the way.”
—Roché Compaan on Twitter
“It’s a great book! Super easy to find some great advice.”
—Craig Anderson on Twitter
“@AdamChainz, your book is excellent. It addresses a complex theme - tests optimization - very easily. Applying some of the concepts, I was able to reduce from 40s to 4s in total time for +400 tests.”
“@AdamChainz Your Speed Up Your Django Test book is tops. Creating test data with mixins is a godsend and is much better than the way I was doing it with a file full of methods. Everyone else, I recommend this book!“
—Conor Cunningham on Twitter
“I bought the book and it contains a lot of things which are new to me, although I use django since several years. Thank you for the book.”
—Thomas Güttler on the Django Forum
“I couldn't have picked up the book at anytime better. I was working on a large semi-multitenant Django codebase with almost zero test coverage. So, to capitalize on the advantages of having a greenfield project of significant size—I wrote a whole bunch of tests and started live-applying the bits of tips on them as I worked my way through the book. I started seeing immediate returns as the tricks began doing their magic.”
—Redowan Delowar on Goodreads
Watermarked PDF, ePub, & code in zip file