PyCon Australia 2013

Los secretos de los maestros de los tests

Russell Keith-Magee  · 

Presentación

Vídeo

Transcripción

Extracto de la transcripción automática del vídeo realizada por YouTube.

so Russell's probably no stranger to most people he's a seven year veteran of the Django core team and he's the president of the Django software foundation he's going to talk to us today about testing secrets of the testing masters give it

up hi so yes thanks very much as introduced I'm a member of the core team and a prisoner the Django software foundation my day job is as a CTO and co-founder of trades cloud where a software-as-a-service cloud provider for services to trades people so

plumbers electricians as those sort of people if you're interested in migrating to Perth we likely to be hiring soon or if you're already input so come and talk to me if you if you add a loose end and need something to do so like I said that's

this my day job I'm here because of my involving with the Django project and in particular I'm here to talk about testing now hopefully by this point I don't need to convince anyone in the room that testing is good and we should all be doing it

and we should all be doing it a lot more personally I got bitten by the testing testing bugged my first job out of uni interestingly my university degree taught me nothing about testing and then I got thrown headlong into a job where my first job was to write

tests one of my first really big contributions to django was actually adding a user facing testing suite capability to django so the fact that you can write your own unit tests and have django run them is something that i added in like 2006 probably previous

to that jangar had a test to it and a really good test suite but it was only for internal used to make sure django was working not for other people since then lots of people have contributed pieces to the django test week new features have been added to the

django test suite most notably 1.6 the discover test runner but django doesn't contain everything out of the box there are lots of very interesting testing tools that aren't part of testing core and probably shouldn't be part of testing core but

as a result of that they don't always get the attention that I think they deserve and that's true of many many things outside of django school so what I'm hoping to do today is to give you a lightning introduction to two testing tools that aren't

in the box but that I've come across and found to be extraordinary useful when writing writing good system tests or good unit tests now solutions are only good when you actually have problems so I'm going to tackle this by looking at some actual to

actual real-world problems that you'll likely to have when writing tests and and so reverse engineer the problem from there so first off really common problem you have a test that means you need test data you need to better populate your database with

something meaningful so that you can check a complex filter condition or check that some method operates the expected way when particular sets of data are available to you and as great because django provides a fixtures framework problem solved so how does

that work in practice well you've got yourself a test case which is your class you declare that you're going to use the test users fixture and then in your actual test you write a series of calls on your database to retrieve those particular objects

from the database okay that's not too ugly the bit where it gets ugly is then you have your fixtures themselves now okay this is the single this is a fixture for a single user object the fixture for the previous example used to user objects so the actual

fixture would need to be twice that length 18 lines of code 12 of which are relating to the required data they're very easy to create you can dump them out of a live database Django serialization framework will dump that Jason straight out but the signal

gets lost in the noise in that user model like I said you've got Django's default user model has 12 required fields that you need to fill in just to create a user at all the fixture is then 18 lines of Jason because of the metadata is required to tell

you that you're actually creating a user and from a readability point of view there's almost no data in there if for example you were setting up users for the purposes of a test it's likely that you just need to have our user so that you can test

that my blog entry is owned by someone but blog entry has to have an owner I need a user make me a user so you've got 18 lines of boilerplate to make sure that you've got a user of some property another likely use case let's say you've got

you want to test that super users can do a particular thing in which case one line out of that 18 lines is actually significant from the point of view of your test okay so that's fine that means we've got a lot of extra noise in the yeah you then make

a modification to your models you you migrate using a your own custom user model in Django 1.5 or you've got your blog model of a sudden requires a date stamp an extra day stand to say when it was edited that means you've got to go through and make

modifications to all of your fixtures and essentially that means doing it manually you know you can there isn't a dash dash ignore non-existent option on load data so if you remove the field you're all you've got a little bit of help there but

you can't use that in testing so you'd have to manually load your fixture with loaded with ignore non-existent and then dump it again or you've got to go write yourself an ad hoc you know regex script or something on a said script to go through

and modify all of your fixtures and add the extra fuel that you want in there so yeah it's a little bit messy and there's also primary key issue your your fixture you can you reference other objects using primary key values so if you've got this

whole fixture you need to know somehow that user 23 is your test staff user who hasn't created any additional content and that metadata can't be in your Jason fixture itself because Jason won't allow you to have comments in a fixture so you've

[ ... ]

Nota: se han omitido las otras 2.999 palabras de la transcripción completa para cumplir con las normas de «uso razonable» de YouTube.