DjangoCon 2015

Introducción a los mocks para mejorar los tests de las aplicaciones Python

Daniel Davis  · 

Transcripción

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

all right dinner mocking why you missing doesn't have to be so hard I am Daniel Davis a little bit about me I have Anna software developer for about eight years now I am a senior consultant at Excel a consulting in Washington DC and fun fact about myself

I recently well not recently in December I ran the gingerbread jingle all the way 5k dresses a giant gingerbread man if you want to see what that looks like this is a candid picture of that it was awesome I have some recommendations on things you should not

wear when running a race felt is one of them so all right let's do a quick survey who here in the room has ever been frustrated by an experience writing a unit test show pants yeah everybody in this room here myself included thank you all for raising your

hands I think I came to this with the same sort of problem you guys have had I sort of struggled to write unit tests for so many years it was very very frustrating and hard until I finally learned about mocking and sort of that made the lights you know the

light bulb go off in my head everything sort of clicked I think I have a rendering of that that was sort of how it felt for me but my point is this I think a lot of us here in the room are in the same boat we all kind of want to write better and unit tests

and you know I've talked with a lot of people here and they say I really wish somebody would sit down and write a presentation on this stuff and like just tell me what I need to know so I wrote this with that in mind and if that happens to be you in your

particular situation hopefully this helps you so let's talk about unit tests all right hopefully you've seen this at some point in time if not this is martin fowler's pyramid of testing the idea is that he tries to quantify how much of our testing

should be unit tests versus integration tests versus like UI or manual testing that sort of thing obviously the significant majority of it is unit testing which is kind of ironic because many of us are really bad at unit testing and we don't write lots

of unit tests like we should when I ask people about this stuff I get all kind of stories about why they shouldn't for example they tell me like well you know it's really good when the problems are easy but I run this problem when you know I find these

other things at the test and then I have to write tests for my tests and the code gets really complicated I call this the rabbit hole of testing here's kind of like going down different layers and it becomes really really hard and so you know mocking could

help us with that no no worries another thing I hear is that people tell me I spend too much time writing these tests right I write lots and lots of code and then I have to write more tests for my code and then I have to write tests for my tests and just it's

a big problem mocking can help us solve that lastly I hear people tell me that we can't really write tests for some things just there's just so much stuff you can't unit tests this is sort of a half-truth right I admit that there are things that

we cannot cover simply by using unit tests but I think we way over estimate what that portion is it's actually a very very well-known and small fraction of things that we cannot test you know testing is an incredibly powerful tool mocking helps us make

that more valuable and more it helps us fill that space so mocking makes you know testing easier so what are mocks exactly what what is that mocks foam this sort of strange subclass of a thing called a test double in testing the idea is we typically have like

test stubs or tests pies or mock objects that sort of thing you'll hear me sort of use these I'll call these things mocks interchangeably but understand that maybe in an academic setting this might be a more important distinction for the purposes of

this presentation though I'm just gonna call it a mock so one of those things specifically well you have a test stub and the idea is that it provides a canned response to a method call maybe a spy which is a real object that behaves like a real object

until a certain condition is met then it does something else and you have a mock which helps us to verify that something was called or you know verify a behavior right so here's the thing does anybody in the room feel like after I've given that explanation

that they understand mocking significantly better and they're ready to go test no of course not like for me I think what helps when we talk about mocking is understanding the types of problems that mocking helps us to solve ok so let's talk about the

things that machs helped us solve first problem what happens if you have a dependency right so here I have a sample method foo everyone's favorite and depending upon the value you get back from method bar is gonna impact how the method foo behaves so this

is a dependency we could write this down as rule of X you know it's going to depend on bar right the problem is is that in order to understand how foo works I need to also understand how bar works and that creates a big problem if I could eliminate that

dependency if I could figure out a way of making gaining certainty with bar then I could you know test that more effectively so mocking helps us with this second problem what if I have a method has no return value so I have this method here foo and depending

up on the value of X it's how they're going to call bar or it's going to call something else but how do I know if bar is called now sure we could write a unit test for this and that unit test could say well depending upon the side effects that

bar has you know we could sort of introspect it or figure it out somehow right this is a really bad way to write tests it's really really cumbersome and painful right if I had some we're just saying did bar get called I have much greater certainty

and know better so mocking helps us with that lastly if I have a situation where I want to generate an error or an exception right but that exception is really hard to generate so not talking about like a type error or file not found right so in this case

maybe something that that would be very difficult to in are like a memory error if I could technically generate this somehow like by adding load to the server whatnot that's just it's not a good way to test it's very hard if I could just reliably

call that make testing a lot easier so that's what mocking helps us solve a couple of things let's say you're happening to use a popular web framework that insists that you have you know database calls for all of your methods and spins up a test

database like Django does if you didn't have to have that test database your test to be a lot faster in some cases mocking a model makes a lot of sense so this can be very helpful there it also helps us to reduce complexity so if I'm writing simple

tests it's really easy to understand what those tests do and it's really easy to write them because it's quick and simple so mocking helps with that one other thing that I want to point out is that we don't think about this but if you have

people who are working with you or collaborating and somebody hasn't written their method yet if you can mock those methods that haven't been written yet you can write unit tests for them this is way better than if you had to wait for all of those

things to be done you could just you know you'd have to wait for all the components we finished before you could start testing it so this makes you more productive and efficient you can test things earlier and that's better so great you're sold

you want to use mocking how do I actually do this fortunately for us in Python we have lots of options the one we're going to focus on here is the mock or the magic mock library it's very popular it's extremely powerful and good news for everybody

[ ... ]

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