+1 from me. We need more language and framework tutorials to be written with testing in mind. I wouldn't be surprised if the work from this was moved into Django's official documentation very soon.
Hooray for more guides on testing in python! Although there are a lot of mostly-equivalent tools to what I've grown to love in Ruby-land, I think the most lacking thing might be a sense of arbitrary "best practice".
The zen of python states "There should be one, and preferably only one, way to do it", which I see at the level of a line of code; However, at a higher level, I always feel like there are dozens of ways to pattern something, and none with any particular community buy-in. This leaves my integration balancing awkwardly across several different authors ideas of how things should work.
What can we do about it - How can we make the python community more opinionated? The desired end result is that software is less surprising, that common problems have a readily accessibly, highly compatible solution.
The lines are far too long, it would certainly benefit from more leading, larger sizing would be nice, and a typeface with a larger x-height (times is so tiny), would greatly improve readability.
thanks for the feedback - am not mad good at the old graphic design, but it's all too easy to just say "I can't be bothered with that stuff"... I'll try and make some improvements. I gather it looks particularly ugly on windows, must take a look...
That is a great resource. Especially pulling Selenium into the picture and showing good functional testing. Thanks to the author for putting this together.
While I understand the need to show how unit tests work do we really need to test Django's ability to save records to the database and retrieve them in part 1? I personally don't follow true TDD, especially with model fields. Am I off in left field? Is this pattern common with other developers? Or perhaps people test for a fields existence by checking _meta.fields?
The example of testing a specific attribute, verbose_name, (that is expected to be altered by the user) in part 2 and the user implemented __unicode__ method test I do think is a great example, though.
Also, I'd tend to agree that testing model attributes is a bit OTT - it's very close to testing a constant... but, it really doesn't take that long, and if it provides a scaffold that makes it easier to write future unit tests on the model (eg, on a computed field), so much the better for a little nudge that encourages more test coverage...
re: the first unit test, and the saving & retrieving of records -- i know this feels a bit like "testing django", but the real intention is to test that we've set up our model with the right fields.
The alternative would be to test defaults - instantiate a model, and assert that it has attributes with the correct names for the model fields we've defined, and the correct default values... I suspect that is the more common pattern, and it's certainly what we do at work. I think it can be seen as better in the sense that it has less of a dependency on Django. OTOH, the saving and retrieving provides a useful introduction to the ORM, so it's useful for the wider tutorial. It's also a bit more in the spirit of testing desired behaviour... So, pros and cons on each side really...
I'm a Django guy who started picking up some Rails out of curiosity, and that's when I realized how great TDD, specifically, automating behavior-driven testing is in general .. and I was wondering why it didn't seem like a prominent thing in the Django community. Thanks for doing this, please do continue!
I disagree. I rarely find Django apps without accompanying tests. No submissions to Django proper ever make it in without tests. But as to no test-based tutorial - you're right, but most tutorials in any given language cover testing only after the language is learned, so that's hardly unique to Django. I hope this changes, of course. Everyone should test.
I'm not referring to "it's good practice to test your code". I mean, TDD aka. writing tests to fail, then writing code to make the tests pass. Specifically, behavior driven testing, not just TDD.
I've been wanting to do TDD for my newest project, and hadn't searched for docs yet. Thank you, I'm using this now.
Using the core Django tutorial was a great decision. Being able to recycle things that I already know, rather than learning new things (even simple tutorial steps), is nice.
I recently started learning Django for a project I'm working on. I've been reading the django book at djangobook.com, but it doesn't really talk about writing tests. This is awesome, every Django developer should know how to do this.