An example-driven guide to writing effortless, fast tests with pytest for your Django project.
Rahul Ramesh (~rahul10) |
We all know we should be writing tests, yet most of us don’t. Many Django developers I know want to get started, but don’t have a clue on how to. Some, on the other hand, write weak tests that take a long time to run which leads to testing not well and often enough. Django has awesome support for testing, though the docs don’t explain how to go about it. Atleast not well enough. There are a few good ideas and best practices to easily write fast tests for your new/existing Django projects.
This will be a beginner-intermediate level talk. I won’t talk much about what testing is and why it’s important, because we all know that. I will also not make Pytest the center of this talk, rather we’ll focus more on Django, tips on writing testable code, other best-practices and how some testing tools could make your life, as a developer, a wee bit easier.
I’ll be going over the following topics
Why you should write tests.
Sneaking tests into your code.
What makes your tests slow and how to improve.
How tests force you to write clean, maintainable code in Django
Why use Pytest.
When and what to mock.
Why JSON fixtures are a bad idea, and what the alternative is.
Brief explanation of other testing tools and methodologies in the Django world.
If you work with Django, and don’t write many (or any) tests because you’re short on time, you work with legacy codebase, you lack enough knowledge, or because testing appears to be overwhelming, then you may find this talk interesting.
Basic experience working with Django.
Work in progress. Will update this section with the slides soon. Please let me know if this need to be expedited.
I’m work as the CTO at Entri, an EduTech startup based in Kerala. Previously, I was leading the engineering team of a software services company.
I conduct a lot of workshops on Python and Django and I am a coach at DjangoGirls workshops in Kochi. I am also actively involved in the developer communities in Kerala, and have helped to create and run developer groups like KochiPython, KeralaJS, GDG Cochin etc.