Intro to Python Mocks

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Python mocks what are they and how can we use them let's learn to code how to use mocks mocks are used in testing so let's start with a program that we want to test this is a program called short tweeter it asks you what you want to tweet modifies the message to be under 40 characters then tweets it will go through the program first before we get to mocks so we understand what it's supposed to do starting from the bottom if we execute this program from the command line main is called UMaine we create the Twitter API you don't have to worry about the details here these are just API keys you can get from Twitter to make this work the Twitter interface by the way is called Python Twitter and is available by pip installation we create the API then we ask the user what they want to tweet we then call tweet with the API and the message that the user typed in inside of sweet the message is checked and if it's over 40 characters you strip away the punctuation we check it again and if it's still over 40 characters we change all the CK's and exas to X's we check again and if it's still over 40 we change ends to ampersands and finally we check again and if it's still over 40 it uses I can't be concise and posts the link to this video so that's it it's a very basic program but it's enough that we can use this to demonstrate how mocking works so how do we test our Python function we start with a skeleton of the unit test just as a quick review a unit test in Python is made by importing unit tests up here creating a class that derives from unit test not test case creating tests by defining methods that start with test underscore and then making assertions you can use the unit tests assert functions for this finally invoking the whole thing with unit tests mean to test our program we have to import it first so we import short tweeter we want to test the tweet function so we're going to be calling short tweeter tweet but now comes the problem what do we pass into this function to test it he wants a Twitter API and how do we check on the message what we get back is a status when we're really interested in the message parsing portion just as a note before we solve this problem with mocks the other way to solve it is to break this function apart we can split off the string modifying portion of the function into its own function that just takes in a string and returns the modified string and that's a great way to solve it in fact that should be the preferred way to solve it but let's just say for the sake of argument that we don't want to modify this function in any way it's legacy code or it's very complicated or it's your boss's favorite whatever the reason we don't want to modify this we have to test it as is sometimes this happens in the real world and you just have to roll with it so we solve it with mocks we're going to generate a mock and pass it in it's going to take the place of this API and then it gets called with post update mocks have a special property that any function called on them records the call and returns another mock so back here we're going to make assertions that it was called with the correct message first you import it I like to do from mock import mock in later versions of Python mock is included in unit under unit tests that mock so if you have Python three or greater check there we create mock underscore Twitter as an instance of mock and now we just pass that mock into the function we're going to make an assertion on the mock but just for illustrative purposes I'm going to print the mock calls first this is a special function on a mock that will display the calls that have been made on it also note if you have any trouble running this you may have to pip install mock so we run it and it shows the post update call good now we can call the mocks cert called with method they put the string we expect you and if we run it again we see it passes that's really all there is to it we're now testing the function without posting the Twitter so if we want to test the stripping of punctuation we can do that just copy this test and modify it to have extra punctuation this cake you can see how we can keep going with this and anytime we want to run through all the tests it's a simple command-line invocation that's an intro to mocks in my next video I'll go into more things you can do with them and what to do if you're not passing in the object that you want them up link the references are below please like comment and subscribe and thanks for watching
Info
Channel: Michael R-A
Views: 44,233
Rating: undefined out of 5
Keywords: Python (Programming Language), Learning (Quotation Subject), Mocks, Education (TV Genre)
Id: smPbDqGjFAI
Channel Id: undefined
Length: 6min 6sec (366 seconds)
Published: Sun Jun 26 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.