Mocking Strategies in Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so presentation I ever actually rented a lot of iterations of what to call this so long marketing strategies but since we're almost kind of like you know mocking jokes not something I have any better titles for this play just chat about if you got one anyone you guys obviously I'm not hot enough beer is great all right so we have please don't lock me um well uh you guys are asking me like yes luckily I have a projecting Boise so hopefully effective um my name is minimus you probably already knew that I've been a software developer for about eight years it's kind of Shing because I've been programming since I was 17 but my professional career started after I got out of college so I kind of eight years um fun fact I ran the jingle all the way 5k dressed as a giant gingerbread man if you guys weren't there this is what's that look like if I may make some recommendations on things not to wear when running is really not good so I was this big sweaty mess like when I got out of the thing and people might take pictures I felt like a celebrity so it was awesome and people are like you're good you ate away ah okay um come or see that what am I um I actually came from the Java world so like I was a Java programmer for about five years so I lost the connection I got the double room wins you know that was my wedding I have been affected over about two years now so I kind of been the switch um and I've also been a lot of DevOps work recently which is kind of neat interesting so I'm in sunny loves them with quality and automation and trying to like how do we get some server isn't like why my developers keep breaking the build so I care very much about software quality oh yeah I'm very passionate about this but it's it's not something that I think is like a something you just kind of develop overnight you know it's something you have to sort of get into a second journey you have to go through so I want to talk a little about Cheryl about that um I became a certified scrum developer about two years ago can be like two or three Hank I remember um that for me was a really really awesome turning point in my career because somebody finally sat down was like okay this is what software craftsmanship is it's an amazing course like that you guys can go through it's amazing someone actually sat down and told me this is what it is to be a good husband this is how you write unit tests in a way that makes sense in a way that's easy and actually supports building good software um the problem was that kind of artists hauled in Java and then when I switch to Python it was a little jarring because I kept wondering well how do I do this stuff in Python like I want to do all the things shuttle is very structured rigorous sounds very like dynamic an open and doesn't you know it's different so I kept dealing with these things like you know if you have this incredibly complicated framework like Django and their documentation like yeah that's cool you just write these tests but they spit up this you know test server and that makes sense right because it's you know it's not good so that's my colleague from this um I said what you guys think you know how do you do this stuff and they actually said we don't know whether you know they rocks and when I asked one people in the community they don't know and that's kind of sad you know I'm going to testing is something we all should know and so I wanted to learn and more importantly I wanted to talk to you guys about that and they wanted to share that knowledge because I really really believe this is important and it's something that every developer here should know we all want to be good developers build all the code so let's talk about good tests I want to go that high so you may have seen this pyramid it's Martin collars like you're testing pyramid and it kind of is testing pyramid and is trying to explain like well how much of our tests should be unit tests and how much of our test should be like integration or UI or browser or manual tests right and the vast majority is obviously unit tests this poses a big problem course my name is don't really know how to write good unit tests so it could have a problem for us if 70% of our stuff is supposed to be unit just let me get a lot of learning to do and you're a lot of things when I ask people about this they have all kinds of myths and things that they tell me they say Dan isn't thing you know tests are great except when it gets complicated and I joined the write a lot of complicated code so I can't write unit tests and that's really hard we're like they'll tell me let's say well I have to write this really complicated unit test but then I write a unit test for my gonna test because the test is so complicated and then I call this the rabbit hole of testing that's not a good thing like we nobody wants to be in the rabbit hole testing we want simple tests who know things are straightforward um I also hear that tell me like well I find myself spinning my wheels trying to figure out how to test something I write a lot of code and then I have to spend all this time looking up how to write the test and that makes me feel unproductive and I don't like that what we really desperately all want is to be able to write effective tests be able to sit done like yeah this is how I'm going to test it this humming good done move on the next thing we don't want to spend a lot of time spinning our wheels fast sometimes people tell me that there's just stuff you can't get it justice it Dan there's just stuff you can't use and I mean in reality I kind of agree to an extent but I think that we lower estimate like how what that section is it's actually a very very narrow set of things that we cannot you're going to test so you know there are times that patience for that but the vast majority of things that we can do we can indeed unit tests so the good news for everyone here is that mocking makes writing unit tests a lot easier and that's a huge relief it really is kind of like a sort of difference it is really a black magic of sorts so I hope you're all excited because I am really super excited to be talking about this sharing this with your community whoo yay mmm that's enthusiasm right there give that good another beer so so you might wonder like water rocks and how does that stuff work so let's go back into computer science school let's remember how this works you may oh man I've ever seen this we have these things called test doubles and they are like testing structures that allow us to test things um you have things like dummy objects and think objects where you're essentially just saying there's a method I'm just going to call that and it's you know just the different method to does stuff but more interestingly we have these things called test stubs test spies and mock objects now what mock objects are but what all these things are I refer to them in a common vernacular as in mock but some people like in an academic setting or maybe like you know in a white paper or something like that this distinction is a little more important quite frankly I think that's like esoteric and silly let's just call them all knocks for now so for the purposes of this presentation those three things are mocks but no there's a there is a slight difference between these things so for example stubs are really used for providing a canned response to a method so I want to call it and it's going to do something that I want instead spies are real objects that behave and act like a real object except when you tell them not to instead everything like a spy right um mocks or use objects for used to verify behaviors so I call it and I say were you called yes it was or no it was not um so does anybody feel like they got smarter in that like that that was like a really good way to explain mocks are good for like you guys think y'all go home right now if you think that was like what it was useful for but for me I think that this is not satisfying the way that I generally approach these types of things is what types of problems do mocks help us solve why are they useful why are they important what do I want to do with them so what I want to give you guys three problems the Mach salt okay and if you guys remember anything remember these three things what marks help us eliminate dependencies right have something like on test and it's called something else and I want to remove that dependency so here is my method everyone's favorite food it's going to call this other method called bar in the implementation and depending on what I get back from bar is going to affect how the rest of the method behaves how can I possibly write you're gonna test for this well you could like riding in a test for this thing and if you understand like what bar does then you write even test for you know it would you could figure it out but wouldn't be pretty so really foo is sort of dependent on bar what we want to do is when our limit eliminate that dependency we want to make this that we know for certain what the value of bars when you back said what up right okay second problem locks up a salt they help us salt methods that have no return value here's the method it has no return value but we would all agree that this has logic aid and that logic ISA t-test right if X is greater than 10 it's going to call bar how do I know a bar gets called although doesn't return anything I can't run test for this it's impossible you know I mean I guess you could write a test if you need to let the effect of blah blahs and you kind of like sort it's too much work it's too hard mocks help us solve this problem we can know if bar was called or not that's one air handling this one is sort of overlooked a lot of times um and it's easy because sometimes we sort of like well I can sort of jaring this barrier to come up and like a foul to the i/o error like oh it's not a bottle there right you know it's just missing but what about errors that are really difficult to reproduce so in this example I have just some method and it calls a thing called parse large file and suppose for whatever reason maybe you are running out of memory on the server you decide they want to put some code in there that will make it handle it a little more gracefully right whatever whatever you want to do return every string coming out the best implementation the intern how about that so how would we actually simulate this in a unit test okay it's emboss or will be very very difficult you know how much RAM is the machine hat I can spit a whole bunch of processes and try and crash the server but I'm sure he Rob's people would not like that at all they would be very upset myself I saw this problem we can simulate their handling we can we can simulate errors being thrown in an easy and understandable way so right um other things that mocks are really good for they help us eliminate entities on database databases for example let's say we're using a popular web framework and I left framework insisted in their documentation you spin up a test database every time you're ready in a test I don't know when that would ever happen but you know if it does we can actually you know eliminate that dependency and it will speed this whole testing thing I don't have to spin that database set and I'll have to rely on fixtures because they constantly bring um we can reduce our testing complexity this one is my favorite I don't like writing really complicated tests because we're the complicated things are hard let's just write really nice simple easy tests and mocks help us do that it makes it so I can look at the test and it's just there just specifically the thing I want to test and nothing more lastly and this one is kind of neat and over over looks as well if we have stuff that we're waiting on other people rights especially if you're collaborating with another developer we can actually develop simultaneously if we muck whatever it is that they're using so I'm waiting for the intern to finish writing his method and you know I want to get my testing I want to actually continue doing stuff I can just let that and write tests and whatnot and it goes through this can also save your button in case you're doing like a demo like a quick and dirty sort of like and it's like all it's not done but I got 15 minutes tossing them up make a return the letters let everybody once great you know so it has a lot of other useful lives so you are all probably now super excited about math right I can tell ya see okay great thank you thank you very much um so how do you actually do this um goodness people who love Python there are lots of options for you um lots of options for you in researching this is like wow wow so just kind of looking at a list here so muck and magic math is the one I'm going to base this presentation on it's the one that I use it's popular it's robust and you'll be excited to know that if you're using Python 3 I assume you all are using rather all right if you're using Python 3 it is included it's built-in now which is a huge endorsement for that but let's say that's not your jam uh you can also use flex Mach if you're coming from maybe the Ruby world and you're familiar with Flex my Ruby you can use box so if you've ever favored coming from the Java side and you're looking at like Easy Mac and that's one thing you got mocker dingus another popular ones also fabulous name uh they're they're interesting framework you've got fudge also an interesting name similar to like maketo if you've ever used that in the java world and then mini mock if you guys are a big dot test i put this list up here to point out that there are many many ways to do mocks everyone sort of has their own favorites and their own thing it's up to you guys what your own personal preferences I'm gonna stick with this one up here for this presentation but I do believe that all these do the things that we're talking about or fashion we there read their documentation cool let's talk about several problems alright so again we have our three things that might help us solve let's just walk through those three things and do an example of each of them but you know what guys tell you what I was inspired by the by the track maven like Valentine's Day stuff here let's make this a little let's touch Saturday is Valentine's Day with this Valentine's Day is a full civil problem so we'll talk about you know finding true love on the Internet um and by that what I mean is tinder uh so let's see we're going to build a tinder competitor right we all know how this thing works right so tinder shows you a random profile you swipe to the right or the left dependent if you like or dislike the person so let's give it something cool like something that I think the the tech community get behind we've got tinder and got Grindr about like uh the docker let's go talk all right I even come with the cache timeline for it so that's fantastic um should contain Iran all right um so let's say we were making our sample docker dating app and we wanted to create the alarm should be I have shown random robot right so we're going to put that piece of it so I want to put a bet that will show me a new random profile now if if they show this thing my constraints are it can't be the same person and it can't be someone I've already seen so you wouldn't want to like the multiple times my person does this make sense is this as a setup like everyone good like tricking heads cool so this is how we're going to this is a method than what I mean once you test so let's write an implementation for this you get it very simple straightforward code I have a person I spent the club next person it passes in a user if you know we're going to grab a random person on this get random person method and then this wild loop here just says if that person is already in my list of people I've seen iterate or keep grabbing random people until I find somebody that I haven't seen and then return that person this is make sense to everybody okay cool um one thing I would like to know uh there's definitely a bug in this uh that I came up against when I was creating this example basically you've seen everything in the database it was up an infinite loop um we're we're gonna ignore that for now I I thought about how to change this and make it better than unless I'm really a to interesting it's too complicated I want to keep a simple example so let's assume the intern wrote this code um so here's our first problem right if I'm gonna test this first line we have a dependency on this next random person you know get random person we we have no idea how that's implemented you don't know what it does we don't know how it's supposed to work and it's a dependency course and a bigger problem is it's a random dependency so I can write a simple unit test for this right easy enough here is my test method in our you know setting this thing up we have a user has nobody that they've seen so is this brand new user to our application we're expecting them to see a person we're going to call that person Katie and I'm gonna call her action so you know calls get next person gets back a result and then we assert that the person we got that was actually the expected person Katie um and because this works with early works and it's great I mean isn't that awesome it works except it doesn't work all the time um and that creates a problem for us so like 60% of the time it works every time this is the problem with random dependencies it's going to grab a random person new database every single time and so it's like well you know that's gonna cause all kinds of trouble for us the problem is is I I don't really know what the value of get random person is going to be ahead of time so there's some way I could force it to be what I want the non make testing a lot easier so what are we gonna do all the dependencies yes okay I know I love names too so let's talk about actual code and like how to write this stuff so here's what we've got you've got this dispatch decorator and once your patch decorator is going to do is you're going to pass it in your module name and then your you know method that you want a month listen in this case it's like an unbound method so you pass it in module dot get random person right then little further down you're going to get past interior method a actual mock object so this is your mock you know instance that you can work so that's a mock of that method and then we just set that thing up we say mozza mock it random person not return value is equal to kt what's going to happen here is when this method gets called when a get next person gets called it's going to call the mock instead and it's going to say okay it's just going to return exactly what we want and that's it that's all the magic there is to this so when we get back our expected result there you go we have what we want just make sense like this is easier to follow okay good news it works every single time it works every single time around it's awesome you know no Loretta dependency because I was able to eliminate that randomness out of it and just so the next value so let's take this a couple of different ways you're like okay we can do a whole bunch of variations on this let's say I you know I don't have an undone it that I have class so what I've done is I've rewritten this as you know a class with just a method called get next person I updated the appropriate same general implementation just kind of slightly structured differently how would we mock that simple we're going to use this thing called you know patch not object and we're going to pass it in the class name and then the method letter we want to mock everything else basically stays the same it was everything else stays the same in this in this example so I don't have to change anything else about it so this difference between unbound methods and classes let's take another variation let's say you're like Dan you know what decorators are really scary I'm not going to use that let's just do this in a little simpler way so no decorator one of things you can do is you can actually just directly assign this this is a cool thing Python where you can just swap out methods if you want so all we've done here if you created an instance an application and then we just said okay get random person the method right is going to equal to a lock or a magic block either one works and then we set the return value like normal so this is kind of me uh I like this I find this is sort of like a stylistic difference some people like decorators some people don't show games who likes this better like just that's like no one no one likes or spinkle back to the decorator that's really calm okay guys I think it's interesting like different ways to a different ways to do this and in some cases it it's more clear to do it this way just know this is an option this is something else you have in your tool bag one more variation here uh what we're going to be a let's say really love context managers ah because I know maybe they're like - you can do that the same thing here - this is actually really really useful if you're trying to do something on like the command line please you know pull open the shell you're like I just want to check this one thing I'll crap it's calling me something method you can toss this into a context manager and this is what the decorator is really just doing in the back so the decorator is actually just creating a context and putting lighter stuff inside of it does that make sense to everybody cool um so what happens if we call this thing multiple times you know we have situations where you know we we have that forward of that bottle OOP right so we've tested this piece but what about the stuff in here well what do we do well if I went since the code is in the while loop I'm going to run into a problem with my strategy right so I'm going to call I'm going to snap exactly the same way and then I would say read new person's not returned by was equal to but then I don't know what because if it gets called multiple times that methods known to be the same thing and that person's already mean that the list of people I've seen so it's going to keep churning through and getting right into that infinite loop so that's a problem so how we deal with things where we have multiple calls to that ma method and we like to return different values so we use this thing called pride event same idea here same decorator all that stuff but side effect is slightly different it takes it in iterable object so in this case i just passing a list of strings that I want and then every single time that method gets called it will just return back the next thing in the iterator so first it's gonna return back Mary that is for sure next Sarah that's going to turn back cake so easy enough first to go through the list of nut I've seen that dope seen that last one comes through great hold up does this example make sense okay great oh cool so this is really sort of pretty much it oh good about like side effect versus like return value might say well oh damn I should just start with side effects like why don't you show that it's for me I don't really particularly like the term side effect I think it's unclear and it's difficult to see you know return value is very specific and and everyone understands what that is and I think the case of using like multiple returns is a little more less likely so I personally prefer like just using which are nine question if you raising an exception when you use side effect or tell me yes I'll talk about that there yeah thank you that's actually sets up really nicely um so let's let's recap what I learned so they passion mock's brings certainty to our methods so again we're eliminating those dependencies that was our that was the thing we're trying to figure out right how do I get rid of that even if we have unfinished code so that developer may have never even written that method doesn't matter we can still test it that's really awesome that means you can still get work done um there's lots of ways to do this as I showed do you pick your favorite alright let's talk about the next one so the second problem was mocking to verify behavior right so let's uh let's keep going with our doctor Dana now let's talk about gnashing so you've shown the random profile and let's say this person has swiped to the right so your user swipes to the right when they do we have a decision tree that happens right if the other user has already said that they like them right then they want to send both of them a confirmation email so they can like you know contact one another um if the other person dislikes them then they sort of let us let them down gently you know be nice little you know toast it feels like I'm never like next time you know huh and and if they haven't evaluated them yet we want to send them a little fuel we'll do something like you know hey this person hasn't you know evaluated you you just gonna need to wait a couple minutes okay so let's write an implementation for that again first of all family method evaluates and basically pass into people right if personalized Andrew list of people the person to likes the moon ascend both of them an email if person one is in person two dislikes then we're going to let them down gently calling this method here and guess whatever that you know it sure it's a complicated thing so more like that and if person one is not an either it is not any person ones the person twos likes or dislikes then we're going to give it some time away okay you'll notice this method doesn't return anything so like the necktie test this like there are no return values I have no idea what to do with this evaluate that so let's focus on a very specific piece let's say we're going to for the sake of the demo let's say we're going to evaluate this middle one right here if the person dislikes you know the user how do we how do we test that so we're going to use our good old friend the patch right we're going to mock that method and I just got a simple set up here you know person want to fill person to is just going to be a dictionary with an array of who they like in just like you know just some implementation for this pass these things in to evaluate the important pieces down here at the bottom so we won't use this thing called compound and it does pretty much what you'd expect it tells you how many times that method was called that my method so when if I weight this call it goes through its decision logic does its thing and then let you know how many times its goal that's it it allows me to verify that the correct thing is being called great now you might say well okay the call comes great but what if you want to check like the parameters being passed in my value more thorough right you know probably a smarter thing to do so let's do that so we have our same exact setup the only difference here is this bottom line here where we're going to call different method called assert assert assert called once which reminds us two things this is ok is the call count exactly 1 and was it called with this parameter so in this case I'm going to pass it person 1 it should match this is really really valuable and important actually I think this is a good practice for everyone because obviously you saw when I was explaining it person 1 person 2 that could really easily get flipped and it would be really really trivial for somebody to accidentally put the wrong thing in and now you've got all kinds of problems going on so it's an easy like simple than we can use to test and have better better quality code um what about the other Mendes so we verify the thing that should be calling it's called but we also want to make sure the thing that shouldn't get called doesn't get called for these methods here right I don't want an email to go out I don't want other things happening so going to be good records for us to all so verify those but in order to do that we have to be able to mock multiple things right so I need to be able to have more than one mock in my test method the good news is you can do that because game Python decorators stack and that's okay that's you're allowed to do that and because they are like just nested contexts that's also okay so that's pretty cool it's really Hindi one thing I do want to point out is because these things are being applied they don't get applied top-down the applied bottom-up that's just like how decorators work so these are not keyword arguments these are just like order parameters so you have to make sure you have the right order for your box otherwise you'll be testing with little things that's a little bit tricky sometimes essentially the implementation is the same same basic stuff and then down here I just say hey make sure the call count for those things is zero that make sense to everybody cool let's say you're like dad's decorators are scary and particularly like that or I don't like the possibility I could screw that up they also provide this thing called patched up multiple which gives you a slightly different application of this um you pass it in the class and then methods that you want to lock and then it will provide those as keyword arguments to your method so this is nice because you can't really screw that up you know like it'll tell you if you put the wrong thing there and like the downside is like you can't name them mock whatever and sometimes that can be a little confusing but more or less the implementation is the same okay so two different ways of having multiple decorators up to you your personal preference so let's say we want to test multiple calls so we've covered this thing you know that's good but what about what I call the same method multiple times you know I want to make sure that I'm passing the correct parameters into the method right a lot of issue I built again fat-finger it and sends copy/paste fear like send email to person one place you know so we're going to focus on that whole section so multiple calls looks like this uh down here I had let's see OOP this is much it call arms call arms list what we'll do is again you call your luck and call arms list all it says is I want to provide back an array of all the things that got called and it's just a little abstraction for you know it was called and then here's the parameters that was called with so I can take that out and say okay my first call by my second call and then I can verify first of all and second call were called with person 1 and person 2 respectively some accepts that question oh the cool object yes oh so the call object is specific to mock so it's just like a little abstraction it's it was very because like I was expecting the method signature or something like that something else is like a function but really it's just a wrapper on that says like okay it was called and then use the primer so this moment nothing more but it's just part of the mock library hi next one point thank you okay almost done Mike I know I know I've been saying the word test an awful lot so the ogres didn't think about the kittens or I guess okay no I know some of you guys are not kidding people your dog people so think about the dog Pandurang I know alright so let us brace ourselves for the next subject so what's the third thing that we're gonna test air handling also I want to talk to the built-ins because that's kind of neat - all right let's do this so we're done with the doctor dating app let's just do something easy and simple for everyone let's say I have a simple Jason reader this is just a little you know all it does is it's going to take a file it's going to open the file and then parse it as JSON return it back essay as a Python object this is very straightforward and simple code and suppose you wanted to put a little wrap around in case whatever reason it breaks it throws an eyewear or a value error like it's not properly formed JSON we just wanted to return an empty string something like that that's our tested our simple gesture here but I wanted s something like this this is a little more scary right you've got a built-in backup they're open and you've got lots and lots of stuff packaged together so for me it seems like I want to break this down to three things we have to test right we want to test the way parsley valid file right so I'm going to pass this thing in and it's going to return back valid Jason okay we're going to test the IO error because you want to test error handling and that's tuition and then once the value bear makes sense to everybody this is a good plan of attack okay so how do I test of it well let's just create a sample flour right everybody that's the right way to do this right no no you know better than that you know that locks we don't need to do that that's crazy so can you actually like maka built-in yes you can you can tell look at that just it's again one of the wonderful things about Python is all that stuff is up for grabs so just do better score built-in not open you can mock that and then you say okay you betcha mock open and the same fraternal you is this is this you know it's done hooray everything's simple except it's not sorry I wish you were like that easy I'm slightly different so let's go with this example it got a little bit complicated right so open return is a file object right and then we have to call this you know open file name top three so really done read is what I want a mock I want that to be the thing that returns back a string a JSON that I'm going to try and parse so what we really need to do is mock file that read but there's a problem it's an instance not an actual like laughs let me control so I can't just like hot spot this thing out so how do we do that that's crazy you know you told me that like mock you saw everything well have you tried solving with box because that's a good way to solve that problem right yeah okay cool oh so what we're going to do we're going to keep our same structure we were just mocking you know open and then we're going to add two more lines right here we say okay it's returned by you is going to be another mock and then this month we create here so we just use that same you know that that's alternative way of creating a lot and we're just soak a paternal sorry and we say yeah they're returned by you that is going to actually be Jason so let me take this through the three let me take you guys to this again mocked it opened our turn God is going to turn back and mock that we created that lot is then going to return a value that we want which is the string of JSON we will parse does that make sense to everybody cool so this is me like you have control over these things you don't have to be powerless you know against just things that don't work out it's very very flexible okay so what about our handling you promised me that we would talk about arrogant and it's actually really simple um we just use side effects ah like one line of code you not open and you say when you call mock mock down open or when you call open it's going to throw an i/o exception and that's great it allows us to handle everything else exactly the same way you want to so you've seen this general setup same general idea there what about something a little more complicated we wanted you the value error right and we're code slightly more complicated right we're doing a jason bugs but same general idea fine so we have a mock our mock and then we say when somebody calls loads on this it's got thrown value error so it gets all these effective returns it throws your value error and night interesting does that make sense to everybody and this assumes a little more complex cool yes maybe what could we also just brilliant so the problem we gonna run into if you just say like I was putting some you could put in some dollar Jason but I find that um I find that this is more explicit it's like I want to throw a value error if I were another developer looking at this code and it's just like okay that looks like some mangle Jason or whatever I might misinterpret that this is really clear it is going to throw a value where there's no question about that like that's what it's going to do I give it some mangle Jason maybe it's a little open for interpretation but I mean it's not going to the world the thing I would contribute also is that JSON that after you've image Kasich level into one line yet or I've used this it was like we have code that uses pill to open an image and I want to try various forms and corrupt images testable yes exactly you know that's the other thing we don't want to have to just have this random test files committed to our repositories like what is this all these test files were from this random obscurity and then you have to maintain them this is all inside the text if something let's maintain it there you go it's all there it's on github I can like you know point at it I can touch it that's that's a benefit to us and then that improves clarity yes thank you cool so that's pretty much it let me wrap up let me give you guys some takeaways for this okay if you remember nothing from tonight mocking makes writing unit tests a lot simpler all the examples that I showed we're simple because we're using mocks right because we're putting this stuff together it's like I don't have to write a bunch of extra logic I don't have to handle a bunch of special things just mock it and then I can test the thing that I want to test um it helps us to eliminate dependencies now just to verify behavior and helps us to handle errors um I think for a lot of us this just requires a little bit of practice you know seeing in the presentations like ok that kind of makes sense but I really do encourage everyone to kind of go home just try to it's it's surprisingly straightforward to pick up and once you start using this like now this makes sense like okay I get it now I understand why it's useful when you use it okay and if you want to try on your own there is the link to the documentation if you're not running my thumb free you can just do a pip install lock very very straightforward and what I did was I just created a sample class and started poking at it you know what are some situations that would come up what are some things with this be useful for that's how I I learned this and I you guys too as well alright let's go ahead some tests yes you guys excited okay questions yes um John nuda like testing so that might factor this question but so you mocked out like the interns work in the beginning the game um that method mm-hmm when he finishes the work your test is like always going to pass because you're mocking the return value right correct but I'm not testing this code right so would you need to write or would you like in your experience writing another test that actually tests his code because how do you then have a test that is code actually works as it should when the app is fired up saying yeah so we would call this like isolation so we're gonna have a set of tests for like the the get random person method and then we're also going to have you know a set of tests for get next random person in like testing terms of calls like the class under test of the method under test so in this case like my example I'm testing get next person and I watched a laser focused on that I don't know what anybody else is writing I just came up that method when I go to the test get next random person I'll like write a whole suite of tests specifically for that sure does that match the question ah yeah it does ah in the back what is magic mark two pets in law so as far as I understand it like magic pocket locker like they're they're part of the same thing and there's some distinction between them that like when you want to use one or the other but I'm really uncertain as to what yes magic mark everything that you ever do with we'll just give you another batch of block instance and so like if you did that magic makan old baby called me you get back magic walk that's great the problem is if you call you know reading this type of the name it will also return magic all right so it works great for things like okay I want to test this Twitter API client I don't want to have you know keys and things like that floating around but you really have to make sure that you use you know what you're testing and you only test that you still have like some sort of like an integration test later that there was an I agree I've used it a few times and it's very easy to have that like well that was almost but not quite but the real code does so this whole test was waste it also lets you lock dole onto school methods on the job oh yeah when she called you on a normal make sense me so there you have it you know other questions cool yes so I'm actually Brandis Oh too long oh sure so so I thought about really hard about bringing my classic tango examples but I thought that might be a little overwhelming you know especially like this is your first time doing like mocking or even testing of any type so essentially it works the same way one of the nice things is that notch allows to do like chaining up methods so you have your call to like objects doll you know and you can just say okay this is in mock and that's it so I wanna grab something from the database you just said like okay it's that's a matter you know and then when it tries to grab something I just say you know object on all the filter not whatever return value is this and then when that particular thing that's called you will get back here a few things this is really useful for like I'm testing a view and it's supposed to get Baxter data or I'm testing I'm just in the view anyone like mock your request you know so the request has a whole bunch of extra stuff on it and I wanted to have these things does that make sense yeah anything else yes it's more like a philosophical question but do you mock your interaction with the database do you mock the rme when you're doing your year test I would I would put the preference I'm doing that only because I really dislike having to load fixtures my problem with fixtures is that you spent the test database but then you do your like any hundred fixtures and that's one more thing you have to kind of maintain and then you say well if I migrate my data that I also have to update all my fixtures and it's not really that easy consumable format that you can change for something simple the little tiny picture like or if you have a really really big like this is a huge set of data and adds a very specific thing maybe I'm Soviet but I would say we should definitely try our best not to you know hit the database that's really an integrationist and our stuff will be faster so that's a big benefit to us anything else great thank you guys very much for coming out let's get some here you
Info
Channel: Excella
Views: 23,440
Rating: undefined out of 5
Keywords: Python (Programming Language), Mock, Mock Object, TDD, Software (Industry)
Id: zW0f4ZRYF5M
Channel Id: undefined
Length: 43min 41sec (2621 seconds)
Published: Sat Feb 21 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.