Merken

Testing the untestable: a beginner’s guide to mock objects

Zitierlink des Filmsegments
Embed Code

Automatisierte Medienanalyse

Beta
Erkannte Entitäten
Sprachtranskript
you'll just the and does the ball with Andrew Burroughs with Dunedin few of welcomed by now that arose as much as and deliberately and what I did I work for a gelatine of nearly 10 years and it still is a systematic 1 hedge fund written on were in the business of using can put Peter organs to invest on behalf of clients we do that in town in Python if you got code base pairing present of any language managing billions of dollars and trading around the world clock and around the globe then you get a one-house some good tests and we find that mocks the marking play a part in providing good test hence the and it was about time itself we FIL plug we host the predator and talks we run a competition encouraging students to get into Python amazing Charlie 29 aligned talk and we do a whole addresses stuff we use lots of missiles immigrants the proposals check out the health and my boss would love it if you want to follow us on Twitter he's even tho they are my back b Anderson Cancer get lost if if you see me remember about and you can't hear about and the summit and culture a bit about the topic and the looks and we really example-based and can be some theory and definitions as around opinions and I'm not to go until all the deep and workings of market and the full richness of API and Helen pretty good talk yesterday actually which when the of this is a beginner's guide has 2 years so if you want more meat I should watch on you tube is really good and the answer introduce blocks and give you enough to get started if you're not already using it and all my examples are in part test and part 3 book Duchamp estimate but really you hardly notice if you know already used as allergies and all my samples are available on my k of that you can getting itself so why you hopefully you not
like the guy with the variance and your arthritis interest but if you're not doing tests I'm I'm hoping to be warmest excuse right unit tests and the excuse that mocking is somehow mysterious how long enough for you then have to spell out method if you are writing tests I hope to give you the tools to make testing complex system is both easy and fun so I said the
um code they they talk would all be very example but is 1st example I shall find some common ground I forget after will be locked is constant you for 5 days to get the 1 thing we have in common is that the expertise in turning conferences so all my 2 examples of based on a mythical system that models and coding cops coding conferences sort this is the 1st example is compass speaker and this class just like I did comes out welcome to the world has a whole audience and their and induces which and with this we got
1st definition system on the test you may see this in the documentation when you read around the topic and it means exactly what did you think it means is the code that were testing so this class and typically this great function is our system on the test FIL so how do we tested
this is not very long and it looks like it should be easy test we can just create an instance of get would probably got a class about lying around October is already pass in see it does the right something is peasy unfortunately just like
in real life or conference delegates are pretty enabled by tweeting continuously just reference 1 of overloaded code and you need to look at bit and highlight the bit that's most important green so feel free to read the whole lot but if you wanna save time skip bring so we use this class in our test then we would 1 of 2 things happen either our tests would and failed because of the weather running doesn't have access the insect incentives behind firewall or doesn't have the connection details it needs the passwords keys to access replies calling all the the tests will pass and we was spam all our loyal followers with test suite so either way we don't want to happen but this is part the right so
it's easy we can just make something that looks like a conference delegates if it quacks like a constant delegates it's probably a duck so we can just make test and the class make instance of pass into our analysis on the test make sure the right thing happens and that totally works and you can go home and do that and we can stop at all right now the talks meant to be boxer made is the best way and it was here that he could be a lot of work if we have to do this every time we wanted to essentially Markowetz and name and class object in October so we can use the model
library if you in Python 3 . you can pose a community system previous to that you compute install mark to rolling back port and then import from all were able to create an instance of the mock objects and passed in to are great function and then were able to assert the right kind of thing happens how local and remote event FIL so as a quick
beetle actually look at what we've got these mock objects are the most important thing you need to know about my objects is that everything on a mock object is another mock object every attribute is a mock objects every methods is a mock object every returned by your calling it isn't as an mock object the term I method among projects the actually term I use methods of our project is is mocks all the way down until you hit the turtles what can talk
about that I was not I mean when I 1st started in this I was quite confused and I worked as a team of people who use a whole load words that all seem to be interchangeable but had the innocence of the different meanings so before we go on forever is within kind of define some terms in case of Ukraine's Python from another language we reach use more than 1 language or eat like was you work a team of people who just use completely random terms all the time it was sum was going on the 1st term is a testable at think of this extendable and I'm in a film it and it it is a very general term just means any pretend object is interesting and as I said in Python we would therefore what we would use marks and Marx a definite testable so there's a 1st translation thanks takes
on blocks operates thanks to you that is when you think something you use a real implementation but it's taken some social so maybe you using memory databases the real thing so that's not all of about you might get the mean values in
Python dummy values a sensible somewhat covers and we use these they have to pay the argument list to trace the flow of data through our code and you'll see that it's mocks now as well
so we just see what action it said that it has no real kind implementation any pretense implementation it really recalls everything that happens to and you you get a certain later if you want to and closely
associated and Python use the same object use MOX to stops there in the can wide world and talk about this and still has more implementation behind it makes more of an effort to attend to the system the test that is the real deal and we can implement a stubborn Python using the mark I mean it was sort of the side-effect policy that and and despise spies mocks that as far as we're concerned that's that's not display
FIL so we can all that kind of meet behind us let's go back to our example in Python we use Action Search mechanics which is sigh answer term to mean that 1st we arrange artist environment then we calls are systems Test act on it and then we assert that and the the so all the behavior we expected and it's really important to notice that what we're doing here with Marx is we're actually assessing on the behavior so we're sitting on what got cold not what state that the final system also here are used 1 of many asserts that built into marked a user circled once there's a whole range of of I'm not I'm going to all in here you should check of docs looks really good all colors so yesterday and went for a good number of them and provide a good overview I'm going to show you my 2 favorites and slightly in the spectrum so-called once they've does exactly what it says on the tin we assert this this model was called want these arguments and underscored less 1 if someone wants all the arguments and the other
the spectrum the kind of superpower of uh of these sessions is not calls and what locals does is he recalled every call to mark and old child mocks and accords the order of the so in the example above and understand in traditional finite I make various calls and its children and then I get to see all more calls on a could settlement engine and you notice that if it's found everything I did and it knows the order that happened in that we really powerful and the promotion of the can be contest that and the the flip side
of it had downsides because all of them is being mocks all the way down is that if you pass a mock object into your system test and there is a type of analysis that the calling speak to its calling spoke to all it's just calling a method that is you may implement yeah doesn't exist the test may still pass because Adam Opel create those methods that is actually it's on the fly for you if you want to limit and you mock stuff only having the same interface as a real code you can do that by specifying a suspect In the constructive lot this is the interface essentially you want for your for a lot In the example idea the boss might to trained to market has a sense because composited and try to get to it to snow loudly in India this session and it fails he deftly can't do that complexes
and also look at the heart example before we saw outcomes in the past and future highlighted that it we can use in our tests because it was reading what if we want what we saw just knocked out completely was if we want to and actually test this class so those same problem is going to treat all the same problems so how we do it organism of course but last time we were able to
just pass a mock objects into the system the test this time we can't do it the thing we want to model which is almost certainly simple tweets M. isn't passed in its importance so how are we going to get R mocks into this we do
it using patch and patches of great tool it's and what it does is it replaces the specified object with a mock and then at the end of the patch it puts it back to normal so completely because tracks so we have to do is where was perhaps substituted that traits closest to the test and then a set of 4 1 hour and that more gets created and injected into the right college the 1 gotcha is that you
have to get the string the specification of what you want patch correct and we have a customer which how will the code is used in the system the test so and the search for if for example instead of importing simple traits we did from simple preacher import tweets then not only with the call site change was so also with the patching M. in my experience with the number of people introduced into MOX this is the thing that people always struggle to round is worth taking a moment to think about what happens since you when we're doing from somewhat region portrayed we're creating new variable in this module called tweets that it is a reference to the code in the symbol Teresa the module but only if we were to patch simple treats the tweets we are and replacing the code is in between 2 books our reference our tweet variable in this module still points the OK so what pattern among them so we have to get it right you don't need to run many times for your kind of spot where doing and so that but it's worth looking out for worth thinking about another thing to
notice for this example are showing is that I'm using a sense so I mentioned before the sentinels all kind of Python with a dummy dummies and what we do here is you pass essential into initializer and we check that it comes out in the call to tweets is a bit like and dropping balls in the top of some sort of machine seeing which hole the bottom amounts of and we really handy another use for them is just a pad argument lists calls when the values aren't important to the kind of testing I read a sentence
by the way the variance used in the rest of the let's have a look take a bit further insofar as fiction application and look at the Radisson 3 so that we built around a cost tweeter API what it's doing is is having to sort a simple retry functionality on top of the underlying got implying API would return false if it failed to send a message well as you can see this code is truck is picking up on that trying 5 times 2 and send a message regardless and we can write tests for this
and we want to see I said before that some the return value of every called of them all is another mark but you can override that you can't you set the return value so here right said the temperature falls and that means every course Street will fail and then I can assert that and old that they shot it tries but fails and also gives
also notice this example 5 using patch this time as a decorator instead of convex for the set of these 2 things are completely interchangeable and you can use them as they would have the same 1 is you might use patch as a decorator and especially if you look you at Apache more than 1 thing is if you using as context manager your code would slow as you add more more patches decoded slowly despair facades screen was of course stuck in an office decorated you have a problem constraint we can do by of the work that His lock-in all the
locations of arrival if
you want to test this retry functionality we don't always fail and we wanted to succeed sometimes so we we want to succeed on the 3rd attempt we kind of a term body but we can do a side effect so as a side effect to list and the 1st calls it a failure the 2nd calls failure that call calls can be true success side
effects are likely to values but they basically just more magical um so there's 3 types magic supported but the sequence proceeds with subsequent calls to that they would not return different varies from a list in turn you can set aside exceptions so every call to the not and raise an exception and that we really handy if you want to test and various scenarios va means actually manufacture various amounts and the final most powerful form is set in the side effect because of functional and some callable and the return value of that function becomes a temporary mother and I was the 1st to a lot of would no caveats that 1 I do like and this is powerful book i is what kind of raising the red flag that if you doing this a lot and you might want is looking at code and just make sure that this not some a better way to structure to make it easier to test it seems like you haven't used some quite if you if you do this you know your tests all the time maybe you're you know bring this in light of that you can construct a different cities the test have said that is
useful and powerful so let's have an example of action let's give are composed of gets the ability to chat to each over a coffee so here we got some sort of implementation where are difficult to hold a conversation and do whatever they like and we can test this by creating essentially a stub images those for kind of
MOX but with more effective implementation we create a function um strange speaks to which holds 1 side of conversation we set up the side effect strange among all now it's probably a style to the very Madam President of marks and and we present our test and we can test that currently wanted but this can look a bit I mean it's not the prettiest test I would argue that it looks like it's in know you have to find of set of running in a search for then ML big chunk of code rooted in the course of doing that so little plug my own
a lot trademark actions is built in terms of market and to imprint something it just makes it kind of tritium and nicer to create side effects on amounts so is this kind of fluent interface so you say when it did come up is called with these took values then return this and these all quite simple examples it supports and quite a complex things if if it looks nice to you and check it out it's on the house is on the redox having to makes the text read a bit more story and uh and um it makes I'd better documentation for the protesting but yeah you might very the so we now about Moxon
patching Sentinels and we're ready to go save the universe but before we can do a warning we will we can be with the dark side of Over mocking what I
mean so I think most people agree it's uncontroversial we will mark that positive guys will put will mark out course mail servers web services will mark out things to make the code and their undeterministic like randomness in time and we won't lockout stuff that is built into the language another mark out numbers and lists tuples strings and stuff that in your world is as good as built-in so we're not and if you do lot number hand down-market out leave it in the um and this probably in whatever you do the modular types which is you know like the oxygen you briefing you should be taken this still leaves a lot of overtime and what we do if you
read the internet you'll find people ranting on all sorts of things that you know about this 2 schools of thought on this particular problem the the classical TDD approach which says that we should only mothballs double think objects we really have to we should try to use the real objects but if we can and as a Marcus approach which would value having unit test testing a single unit has been the most important thing so and they would not everything
would stand kind of pragmatically towards the market stand without being crazy about it but we rely on not once smegmatis over marked 0 tests of brittle the changes are coded expensive to maintain but tempting because they're easy to write and the restore statistics a quick example
we can add a feature to our delicate which is the good to break talks Beijing talks is good is good because it's good conference organizers you should read this talk will be kind of festival and you should maybe consider using ISO standard for 2 ratings which as we all know is the number of kids hidden pictures middle multiplied by so how useful it was on class presentation so we how do you
test this code we could go crazy we provide we promotes instead of numbers this is contrived horrible example we would never do this but to show how the how the money could be let's look the worst case the past moxie and then our asserts and of looking like some kind of we twisted inside out representation of the original power it's it's hard to the whole much better test are
carried in this case we have meaningful examples in each case and you should do that when most of the take
the summarized do test do use MOX easier found and patch is brilliant I I love sentinels like I did also use functionals from the side effects of occasional treats they are powerful but don't overdo it never over love each other and thank you very much
thank God but I believe that we have some time for some questions so do you have any I yes and the disclaimer is question was why don't I have a large disclaimer and the answer is yes I did and it's a side effect of work in the finance industry afraid but basically to summarize it it says if you use this talk as investment advice don't but do read it down but summary I don't want undermine small print OK any other versions indeed the reasons you 1 but I was just wondering what's the difference between magic look amount of science and I might have skipped over that and say here I use magic locks madam oxygen systems mocks but they also mock most of the the so called magical Dundar method so that can be operator overloading a python so because here I wanted to test that things will multiply by Durán added to each other and that's that then the mole and don't done add methods and and use magic em as quick Signed patch always puts measurements but really you don't even need to worry about the difference they just discover it in any other questions we don't have any questions of this talk will be followed by a light folks so in the mean and so so that's all I think 1 was more of a hundred
Schätzwert
Signifikanztest
Softwaretest
Bit
Elektronischer Programmführer
Selbst organisierendes System
Adressraum
Formale Sprache
Güte der Anpassung
t-Test
Datenmanagement
p-Block
Kombinatorische Gruppentheorie
Physikalische Theorie
Code
Computeranimation
Metropolitan area network
Client
Twitter <Softwareplattform>
Benutzerschnittstellenverwaltungssystem
Mereologie
Stichprobenumfang
Punkt
Elektronischer Programmführer
Baum <Mathematik>
Softwaretest
Signifikanztest
Webforum
Komponententest
Klasse <Mathematik>
Physikalisches System
Quick-Sort
Code
Signifikanztest
Komplexes System
Informationsmodellierung
Uniforme Struktur
Rechter Winkel
Total <Mathematik>
Varianz
Signifikanztest
Physikalisches System
Lineares Funktional
Klasse <Mathematik>
Physikalisches System
Signifikanztest
Code
Computeranimation
Einfach zusammenhängender Raum
Softwaretest
Signifikanztest
Suite <Programmpaket>
Videospiel
Bit
Firewall
Klasse <Mathematik>
Signifikanztest
Code
Computeranimation
Objekt <Kategorie>
Message-Passing
Informationsmodellierung
Reelle Zahl
Rechter Winkel
Mereologie
Passwort
Schlüsselverwaltung
Message-Passing
Instantiierung
Analysis
Lineares Funktional
Physikalisches System
Hecke-Operator
Term
Ereignishorizont
Computeranimation
Portscanner
Objekt <Kategorie>
RPC
Rechter Winkel
Turtle <Informatik>
Programmbibliothek
Projektive Ebene
Instantiierung
Attributierte Grammatik
Gewichtete Summe
Formale Sprache
Implementierung
p-Block
ROM <Informatik>
Term
Signifikanztest
Computeranimation
Objekt <Kategorie>
Arithmetisches Mittel
Last
Reelle Zahl
Festspeicher
Translation <Mathematik>
Wort <Informatik>
Reelle Zahl
Parametersystem
Mailing-Liste
Reelle Zahl
Code
Parametersystem
Gruppenoperation
Implementierung
Mailing-Liste
Reelle Zahl
Datenfluss
ROM <Informatik>
Datenfluss
Signifikanztest
Computeranimation
Datensichtgerät
Gruppenoperation
Zahlenbereich
Implementierung
Hecke-Operator
ROM <Informatik>
Term
Punktspektrum
Signifikanztest
Computeranimation
Metropolitan area network
Spannweite <Stochastik>
Informationsmodellierung
Lesezeichen <Internet>
Code
Signifikanztest
Kraftfahrzeugmechatroniker
Parametersystem
Güte der Anpassung
Systemaufruf
Physikalisches System
Quick-Sort
Portscanner
Objekt <Kategorie>
Verbandstheorie
Witt-Algebra
Parametersystem
Reelle Zahl
Kantenfärbung
Datenfluss
Programmierumgebung
Aggregatzustand
Signifikanztest
Stellenring
Systemaufruf
Physikalisches System
Punktspektrum
Komplex <Algebra>
Gerade
Systemaufruf
Code
Computeranimation
Objekt <Kategorie>
Metropolitan area network
Benutzerschnittstellenverwaltungssystem
Datentyp
Attributierte Grammatik
Ordnung <Mathematik>
Schnittstelle
Analysis
Objekt <Kategorie>
Softwaretest
Signifikanztest
Message-Passing
Elektronische Publikation
Twitter <Softwareplattform>
Selbst organisierendes System
Klasse <Mathematik>
Physikalisches System
Objektklasse
Computeranimation
Signifikanztest
Umwandlungsenthalpie
Web Site
Momentenproblem
Mathematisierung
Zahlenbereich
Systemaufruf
Symboltabelle
Patch <Software>
Physikalisches System
Modul
Code
Computeranimation
Objekt <Kategorie>
Patch <Software>
Variable
Weg <Topologie>
Menge
Twitter <Softwareplattform>
Mustersprache
Zeichenkette
Signifikanztest
Softwaretest
Parametersystem
Lineares Funktional
Bit
Elektronische Publikation
Kontrollstruktur
Singularität <Mathematik>
Systemaufruf
Mailing-Liste
Kartesische Koordinaten
Patch <Software>
Quick-Sort
Code
Computeranimation
Virtuelle Maschine
Message-Passing
Twitter <Softwareplattform>
Login
Passwort
Message-Passing
Varianz
Graphiktablett
Nebenbedingung
Konvexer Körper
PASS <Programm>
Kontextbezogenes System
Code
Signifikanztest
Computeranimation
Office-Paket
Patch <Software>
Message-Passing
Datenmanagement
Menge
Rechter Winkel
Touchscreen
Soundverarbeitung
Lineares Funktional
Message-Passing
Soundverarbeitung
Systemaufruf
Mailing-Liste
Petri-Netz
URL
Term
Signifikanztest
Systemaufruf
Computeranimation
Soundverarbeitung
Softwaretest
Signifikanztest
Lineares Funktional
Folge <Mathematik>
Gruppenoperation
Soundverarbeitung
Implementierung
Systemaufruf
Ausnahmebehandlung
Mailing-Liste
Hecke-Operator
Quick-Sort
Code
Computeranimation
Metropolitan area network
Bildschirmmaske
Funktion <Mathematik>
Code
Datentyp
Datenstruktur
Lambda-Kalkül
Große Vereinheitlichung
Bildgebendes Verfahren
Signifikanztest
Soundverarbeitung
Lineares Funktional
Bit
Umsetzung <Informatik>
Mereologie
Gruppenoperation
Soundverarbeitung
Implementierung
Term
Signifikanztest
Code
Computeranimation
Metropolitan area network
Menge
Schnittstelle
Formale Sprache
Zahlenbereich
Programmierumgebung
E-Mail
Code
Computeranimation
Metropolitan area network
Web Services
Typentheorie
Datentyp
Randomisierung
Server
Mailbox
Wiener-Hopf-Gleichung
Boolesche Algebra
E-Mail
Zeichenkette
Mittelwert
Statistik
Komponententest
Mathematisierung
Singularität <Mathematik>
Klassische Physik
Turing-Test
Quick-Sort
Computeranimation
Internetworking
Objekt <Kategorie>
Einheit <Mathematik>
Informationssystem
Signifikanztest
Selbst organisierendes System
Güte der Anpassung
Klasse <Mathematik>
Selbstrepräsentation
Zahlenbereich
Objektklasse
Kombinatorische Gruppentheorie
Bitrate
Systemaufruf
Code
Computeranimation
Kontrollstruktur
Bitrate
Standardabweichung
Leistung <Physik>
Soundverarbeitung
Signifikanztest
Metropolitan area network
Patch <Software>
Lineares Funktional
Funktion <Mathematik>
Code
Patch <Software>
Signifikanztest
Computeranimation
Arithmetisches Mittel
Soundverarbeitung
Nichtlinearer Operator
Patch <Software>
Subtraktion
Grundsätze ordnungsmäßiger Datenverarbeitung
Hochdruck
Versionsverwaltung
Physikalisches System
Baum <Mathematik>
Einflussgröße

Metadaten

Formale Metadaten

Titel Testing the untestable: a beginner’s guide to mock objects
Serientitel EuroPython 2016
Teil 141
Anzahl der Teile 169
Autor Burrows, Andrew
Lizenz CC-Namensnennung - keine kommerzielle Nutzung - Weitergabe unter gleichen Bedingungen 3.0 Unported:
Sie dürfen das Werk bzw. den Inhalt zu jedem legalen und nicht-kommerziellen Zweck nutzen, verändern und in unveränderter oder veränderter Form vervielfältigen, verbreiten und öffentlich zugänglich machen, sofern Sie den Namen des Autors/Rechteinhabers in der von ihm festgelegten Weise nennen und das Werk bzw. diesen Inhalt auch in veränderter Form nur unter den Bedingungen dieser Lizenz weitergeben
DOI 10.5446/21099
Herausgeber EuroPython
Erscheinungsjahr 2016
Sprache Englisch

Inhaltliche Metadaten

Fachgebiet Informatik
Abstract Andrew Burrows - Testing the untestable: a beginner’s guide to mock objects Mock objects can be a powerful tool to write easy, reliable tests for the most difficult to test code. In this session you will learn your way around Python 3’s unittest.mock package starting at the simplest examples and working through progressively more problematic code. You’ll learn about the Mock class, sentinels and patching and how and when to use each of them. You will see the benefits that mocks can bring and learn to avoid the pitfalls. Along the way I’ll fill you in on some of the bewildering terminology surrounding mocks such as “SUT”, “Stub”, “Double”, “Dummy” , “mockist” and more and I’ll give a brief plug for my own mockextras package that can enhance your mock experience.

Ähnliche Filme

Loading...