Managing Mocks

Video thumbnail (Frame 0) Video thumbnail (Frame 6838) Video thumbnail (Frame 8762) Video thumbnail (Frame 15538) Video thumbnail (Frame 24521) Video thumbnail (Frame 28098) Video thumbnail (Frame 31011) Video thumbnail (Frame 31996) Video thumbnail (Frame 36182) Video thumbnail (Frame 37797)
Video in TIB AV-Portal: Managing Mocks

Formal Metadata

Managing Mocks
Title of Series
Part Number
Number of Parts
CC Attribution - NonCommercial - ShareAlike 3.0 Unported:
You are free to use, adapt and copy, distribute and transmit the work or content in adapted or unchanged form for any legal and non-commercial purpose as long as the work is attributed to the author in the manner specified by the author or licensor and the work or content is shared also in adapted form only under the conditions of this license.
Release Date

Content Metadata

Subject Area
Helen Sherwood-Taylor - Managing Mocks Mocking is a valuable technique for writing tests but mocking effectively is often a stumbling block for many developers and can raise questions about its overall value as a technique. There will be a brief introduction to mocking, then a look at features and techniques of Python’s unittest.mock library and cover some useful tips and common scenarios, so this will be useful to those who have some experience mocking but would like to do so more effectively. ----- Mocking is a valuable technique for writing tests but mocking effectively is often a stumbling block for many developers and can raise questions about its overall value as a technique. The audience will have some familiarity with unit testing and may have tried mocking before, but some introduction will be provided for those who haven’t. We will look at some features and techniques of Python’s unittest.mock library and cover some useful tips and common scenarios, so this will be useful to those who have some experience mocking but would like to do so more effectively. Summary of proposed content: 1. A short introduction to what mocking is and why it is useful. 2. Tour of Python’s mock library and how to make the most of it * Creating and manipulating Mock objects * Setting up return values and side effects to control test environment * Inspecting mocks - different ways to examine a mock object and find out what happened during the test * How and where to patch 3. Common mocking situations - scenarios where mocking is particularly useful and/or tricky to get right. For example - date/time, filesystem, read only properties 4. Some discussion of when mocking is and isn't helpful. Focus will be mainly on Python's unittest.mock module but we will also have a brief look at some other useful libraries.
Game controller Group action Divisor Code Multiplication sign Set (mathematics) Code Revision control Programmer (hardware) Mathematics Semiconductor memory Different (Kate Ryan album) Object (grammar) Operator (mathematics) File system Software testing Software framework Endliche Modelltheorie Data structure Physical system Social class Execution unit Projective plane Interactive television Expert system Database Unit testing Category of being Computer animation Integrated development environment Software Personal digital assistant output Software testing Right angle Object (grammar) Writing Library (computing)
Ocean current Complex (psychology) Functional (mathematics) Existential quantification Game controller System call Code Multiplication sign Parameter (computer programming) Event horizon Sound effect Revision control Frequency Object (grammar) Green's function Software testing Circle Endliche Modelltheorie Pairwise comparison Traffic reporting Exception handling Social class Pairwise comparison Standard deviation Information Forcing (mathematics) Electronic mailing list Sound effect 3 (number) Bit Instance (computer science) System call Flow separation Category of being Computer animation Personal digital assistant Order (biology) Revision control Right angle Cycle (graph theory) Object (grammar)
Point (geometry) Functional (mathematics) Context awareness Group action Game controller Greatest element Hecke operator Observational study Code Multiplication sign Patch (Unix) Correspondence (mathematics) 1 (number) Water vapor Parameter (computer programming) Mereology Declarative programming Number Expected value Revision control Protein folding Object (grammar) Energy level Software testing Utility software Pairwise comparison Social class Mobile Web Module (mathematics) Multiplication Optical character recognition Patch (Unix) Namespace Sampling (statistics) Line (geometry) Control flow Data management Expected value Computer animation Personal digital assistant Order (biology) Chain Pattern language Object (grammar)
Context awareness Functional (mathematics) Computer file Patch (Unix) Multiplication sign Open set Mereology Mathematics Object (grammar) File system Energy level Software testing Utility software Endliche Modelltheorie Installable File System Physical system Social class Module (mathematics) Multiplication Distribution (mathematics) Matching (graph theory) Patch (Unix) File format Bit System call Data model Computer animation Software Right angle Iteration Object (grammar) Quicksort Local ring Freezing Library (computing)
Category of being Context awareness Matching (graph theory) Computer animation Set (mathematics) Line (geometry) Codierung <Programmierung> Parameter (computer programming) Object (grammar) Category of being Error message Social class
Slide rule Functional (mathematics) Multiplication sign Patch (Unix) Execution unit Parameter (computer programming) Mereology Modellbasiertes Testen Software testing Category of being Loop (music) Social class Physical system God Patch (Unix) Sound effect Bit Database Category of being Type theory Computer animation Social class Object (grammar) Resultant Reading (process) Library (computing)
Computer animation
we're going to get started with Helen and managing marks was without fear the everybody my name's helen and as a freelance programmer I've been using Python for quite a few years and some of those Europe Eindhoven and then I'm really excited to be speaking here and I'm really enjoying the out having a great weekend so I decided to write this all current commercially rights pipeline UK last year and and I did that because not because I felt like I was an expert on looking not because of its some that I tried working with quite a lot and I struggled with and found that this was happening my way around and not really taking the time to understand it probably and then I started a new projects and it had lots of API interactions in databases the I only and things that were quite a good case for marking and I groups and so wanted to start doing things nicely and write lots of tests and and try and understand this properly so something that I've struggled with and learned a lot about and I want to show you the things on land so what is a mock the model is a faint objects that replaces a real objects and main is for this is in unit testing not so a way to control and isolate the environment in which your test run so why would you want or 1st of all you want tests to be deterministic if they're finding it should be because there's something wrong with your code not because of some external factors like this set being that you need to be able to to test if you don't trust them yourselves ignoring them knocking gives you more control over the inputs to the testing and simulate different environments code and make sure it responds directly to all kinds of scenarios it's also fast it's in memory operations so it's generally quite a lot faster than the things it's replacing like like network polls and file systems and the speed of tests is really important again S to slow yourself running so I'm actually to talking about them not library that comes with Python and this is called the unit test of markets some that there are about unit testing you can use it with that any any framework it works with pi test and other things so used to be a stand-alone library it was written by Michael flawed and it was brought into Python in 3 . 3 if you're using earlier versions you can still use the stand-alone version and that's actually maintained and kept up to date so by 3 . 3 last year system otherwise it's a lot and then it will so as to start with a look at more objects you find yourself working with these quite a lot so the import not with the capital and that's the model class he set up with whatever values you wanted to have a say here I'm setting a value and then I can get back as a property of the if you try to use the property but you haven't said that some of them all and you try to use 1 of its properties that's also another so you is not all the way down and you can kind of dig down and set the values of of these mocks and and get back Marx called and when you call them all it returns another mark so you got more marks if you want to change what your more returns then you can set its return value and when and when you call it and you will get you get back the value of the set so he dig down as far as you want into this structure in you set just go goes deeper wants and this is all about setting up the rights of the right environment for your codes a couple another way that you can
set up and what you what does when it's called the side effects so a side-effect can be an exceptional so you signed an exception class the side effects on the market and then when the market school so you get an exception raised it can also be a function to study you can override the behavior that I don't find I use this a lot but it is there's needed and and side effect can also be a list of things you want to have now this is useful if you want if your code is going to be making multiple calls to this not and you want a different thing to happen each time so I can set up side effect with the value exceptions and then in the event so the 1st time I call it that value 2nd time will raise the exception 3rd time isn't about and I tried to call again are going to stop iteration because around on the side effects and if you wanted to go from don't ever it's just that it's reports and you can you can have cycle or something so why don't you all are
doing things to my stay on recording everything that's happening to them they hold all the tools that are made and all the arguments that passed in and you can get information you can query that information using B's assertion holds I and most of them I want you to say which arguments you expect them to people so got so cold with which looks at the last school that was made it doesn't care what happened before that you've got a circle once with which is the same but it makes sure that it was an equal 1 you got so any calls which just looks at whether that goal was ever made those arguments doesn't care about the others research has calls which and you can give a a list of calls and you can specify whether you care about the order then you got a certain not called which just he detected was called and you need to be a little bit careful about lost 1 because so the citizens in Python 3 . 5 create a you call it and then of so it was called you get session and that's kind of what we might expect we did the same thing in Python 3 . 4 you create a mark you call it son called but what happens is that you get back another mall and it a that's a test it will just rather than would fail and the reason for that is that certain calls didn't exist before Python 3 . 5 so when we try to use it it just behaves like another any anything that we might try to access on the mark so yeah that tosses standards maybe a little bit dangerous if you do he's like all make sure you know which version of python if people are going to be developing with than yeah maybe just don't can't and so that's a little bit dangerous and has called me out a couple of times probably because I wasn't doing green GDP property which is best that python 3 . 5 thankfully that does something about it so we get these safety checks there were found in if you try to called something beginning with a certain force right because you know you tigers and it will complain so we've got a circle which is being added by frequency we try to use that 1 and 3 . 5 we get actions and that that's kind of good was telling us that something's wrong OK say and if you really have things that begin with a search and you want to use them then you can specify unsafe used to switch that off the now when you're looking at when you are using these assertions peoples on the you generally have to say with arguments you want to be constant you you're expecting to be constant and they generally that is to say about that sometimes you you might not care about what 1 of the arguments was maybe your arguments to the complexity you testing in separate tests and so you can use model any that's and you can just pass that in past in as In case of an argument and not just says I don't care what the value of this argument is if you want even more control over current use comparison objects so these are really a special not being there just announced Python thing with magic methods which I like to think of it talks about this week have if you haven't objects 1 instance of a class that implements the EQ magic method then you can implement a custom custom comparisons and prospects in as a check when in your session effects say for example here and
checking whether by functions called with multiple 5 the waters and then the multiple for which it wasn't and government but the last implemented strengthen described point wrongs y you can
have even more control over and the inspection of your folds and maybe you want to look at a lower level same all this smoke called how many times cold and get access to overall access to all the the argument that it's called with but so we looked at a lot of features of mobile objects let's have a look at how How would fit in your text and as the general part pattern will be creating a mark and making sure it's in the right place then will be setting up the values on the bottom of your test then we run the code on the test and then we checked the our expectations have been met so 1st of all let's have a look at how we get them all into place best we use path so this tells Python where to put them all you get posterior marginal or class or function or anything that can be looked up the supply and it was set things up so that when the participants and it will it will give you back rather than the the back things that will give you back a and you get access to that not in your text so you can manipulate the malt and and kind get injected into your code action now the objective actually gives you is a magic more which is like a mock object but it has some of the magic methods set up for convenience but we don't worry too much about that so there are a few different ways you can access patch depending on what the situation is 1st of all we have a declaration so example function which entities into to be testing this it some users requests to contact hot API it fact of Jason document for a user and extracts that uses number of followers and returned so it just looks like the when we want so we attach our test method using the Western Ghats we get a multiple object passed into the test method as a parameter we can then manipulated the values of democracy chain all the way down to return values of objects and will return values and that's not what we're going to pretend is being returned from this API and then we can run our assertion and and check the right value you can also leave it up to the top of your test classes you got 1 and a half and have have it's a class decorated people test methods of catching the same thing then that's quite handy you can stack multiple patches the easy careful about the order in which you do that's the kind of works from the inside out so the bottom-most patch corresponds to the parameter so patch can also the context manager so we get more into a context and not survives the patch active for the lifetime of a context and so this gives you more control over the over their lifetime will not give it to them and that's what you need and if you have any kind of clashes with all the stuff clever things with parameters like quite of 1st things that we can be quite handy there but I tend to use the different versions and when can because I think that the lines of code and these may occur longer in a case study you can you can have even more control with and by calling start and stop on the object and like that shown by fine that's used to match the can be useful 1 thing I particularly struggle with In mocking was found patch cops and the impact from the look up countertops work and what have to use when I'm actually and I left found that mocks ones appearing where I expecting to getting very confused so I just want to look how how this works so I've got my that his function again and it's in a module called the might it had and the test out so the test imports because told yeah utils import requests samples into its own namespace from back to test the current request still we somewhat we mean set and then we call that followers yet formalist looks up request still get because you can't have it will get so you content behaves as much expected to and passes you
consider a slightly different example when I started from a questionable get module level rather than looking on side function the test is always exactly the same apart from the import so if I'm holding out tools to that important get from requests then test practice requests to get that's not minutes to calls get followers that follows called gets gets already been looked up on request and it is looked up before we passed size what's going to happen is we can get a slight delay I then we get something that doesn't match the values set and that's because it was talking to the network because we not you might say we have to but that's the answer to that is that we attach the wrong thing and this is how we fix that impact what's the patch within our module has already that's so a patch on the module itself because the only think would get so the attention of and that works the the Python don't say ensuring patch the name used by the system under test know the useful thing is patched objects you can use this to attach a mock economy parts of an object you already testing example here got simple Greek function on my user class that's as happy birthday to you it's that birthday on testing function amount multi is that so I get I get these objects by Patriots by passing the object itself the method I want the name of method over the past and the return value of wants to to have and when I when I get inside that that context and the users voice that they will return will is magic more that will return true so that we might as well the time
to be quite tricky date is a data model is written in C so you can't mock individual bits the trying to get something like this you cannot the whole thing but it does mean you got to kind of change all the way down to the BD once and then that choice at with the mocking of that so using that is not you're not because they based method so you can that I modify the return value of today and then that and possibly nicer way doing that is the freeze gonna library is critical you can install free Scott and it comes with the decorator called freeze time anything that takes all sorts of interesting human-friendly format you can pretend that dates and times and so on and that's it so it's these people to use but I library knocking of our system is also quite tricky and this utility method called not open which helps you out here you give it and we develop practical read data which you can tell me what date you want to pretend you file has and it was set up a special model that has on the right all the right to file system files stuff a 100 because we using the disability and we need to do something slightly different when we patch that and we we need to use creates Austria because we on right the built and we need to kind of create a local copy of open after consent to pick up on but then once you once we've got that we can open and we don't file as 1 little problem with this and you can't iterates over the and you
all fun encode probably has a nice iconic interface like that you want ask then you've got to more by the more you get back from more open and channel the way down to the it's a matching method and set of lines you want and you know that if you're doing that in the context managing to get it ends up writing as well the if you want to more property and some person class again made is that they property now if you drop to catch
the object facts doesn't work and you get this error not entirely sure what I think something it's something is is the way that properties and here you need special handling for that and for that a parameter called
new callable which lets you say what type of more you want to create and it's a special class school property not set up just for this purpose and the other thing that is you need to patch the costs rather than the objects which might be a little bit limiting so far as I know that's the only way to do that so I just got a little example of Watson model-based tests and it's as of course and it's very created we tried function in the past because another function and it tries to tries Ivanova call it until it succeeds he gets a database or it will wait a little bit and then try again and doubling to so when we think about testing this and I can see to big reasons for mocking festival got this because trying to sleep which it fits in our test then it's going to slow testing down and suddenly we want to be able to simulate databases without that we don't want to cause related this is a test that I've written for this this system this function so we patched time that's that gets rid of delays for us we have to mark object parts in as a parameter and we create another inside the function and we set at MIT this is this is going to be that the function that can be running time we set up 3 side effects to failures followed by success we call on every trial function and then we have a look at what happened the 1st detected was called 3 times because we have 2 failures and successes and then Moxley and set has schools we're just checking that which were checking the calls the 2 but the the delays that were made making sure that it was doubled this time and we check the result that came back from what we set up as as a returned by the function so that's just some example for you and I'm
nearly half time so I had some FIL that stuff about 10 when the mock community for additional and I'm going to post the slides online afterwards and and also got some recommended reading for you and testing data by possible is very good and that looks quite a lot about that why smoke and then the answers of walking out and help drive this design and God not fast tests that test is rated talk about some of the things were watching and the units as somewhat libraries can possibly cover every feature of of mocking the sign of pride but but this is a very few 103 on Twitter and and and that is also a repository of and but in this you know it's quite slides acquaintance so this and indirectly examples and
thank you the main thing that