Merken

Introduction to pytest

Zitierlink des Filmsegments
Embed Code

Automatisierte Medienanalyse

Beta
Erkannte Entitäten
Sprachtranskript
the
high and moaning and so the 1st session stays convened doesn't apply test tell you that is also the maintainer the point changes like this you know and you
so I usually what I worked at a place called personal comment below time tracking and send the application and in order to manage that we
write a lot of tests and we use Python so today I'm going to try to show you some good things with and house you can use so that when you write your test so these are kind of the topics of 1 of uh cover today
and then I'll show you look like this this and then I show you how to how you can write tests with less boilerplate code international some useful plug-ins and the plug-in system of this and animals again and discuss discuss how the test discovery and and then I'm going to show a feature right this is called fixtures which is really and I'm going to discuss how you import your existing projects to make use of scientists in an easy way so
1st of testing the kind of thing that I'm going to talk about today
is automated software tests that verify that your software works correctly and this can be anything from small fast running unit tests to more higher-level tests that test your entire system and or anything in between so before we get started how many of you are familiar with the unit test module in the standard Python the chips with yeah you use it you tend yeah you know roughly how it works I take and then that how many of you are already uses have or have used by OK some the title is a
full-featured testing tool for Python so that means it does everything from test collection to run the tests to give you the output of the weather which test failed and passed
and it also has some other nice features that help you maintain it has to them help you organize in a good way and you can run anything from those short unit tests to more system-level testing would practice it how it scales like both directions that uh you can also use it if you do TD deep it's very nice for me and it's easy to integrate with the continuous integration systems such as Jenkins that works out of the box so here are some
people that are happy with my Python uh and it's used in some also some bigger organizations in both commercial and open-source projects some uh
1 important 1 thing that I find really nice about test is that it allows you to write tests with less boilerplate code and to show you what I mean by
this I'm gonna show you a little and demo so the so
I have a a test file here the which contains a very worrisome because that end users that can make here yes it's uses unit test so this is probably familiar to most of you so we basically us and make sure that the the return value of this function is what we expect it to be and I can run this
but the by invoking the unit test module and it collects test and runs it so if I instead you might have what we do is we invoked attested from the pilot test command so if around that uh it finds has an runs in the about this is the output of that is slightly different but it's the same thing so I want you to notice that scientists can run the existing unit tests that just because they are but we're going change this 1st to make to show some features of flights so the 1st thing I'm going to change is the assertion instead of using an assertive equal understand use planar search statement that's built into Python so changing this and just use the equal operator to make sure that the opposite of what is expected 1 and the next next thing is that we don't actually longer need to subclass from the unit test test cases so we can simply estimate without and in fact we don't need to we're not anymore required to red or all our test cases in class so we can just remove the class to and since we don't have the test case in working instrument the unit of system work so this is what we're left with a so let's just say this
so it works the same I just want to show you what happens if we get a failure so it's really important that you get useful feedback on your face when you when you test is thus not passed so let's assume we we introduce a bike here because of some garbage to their output and we say and we run the
so what you you see here is that you
get a very nice the 10 out the
thing it's colors in them and
that shows you exactly what the
wrong value was somewhere it fails the so this was the code before
and this is after codes so
what I did was used the search statement is that as 34 uh and Python can handle pretty much anything you throw at the search statement is it's very smart you can you give you free instance if you check for a dictionary key or something like that and it doesn't exist it will give you a very nice of so it's hard to find a case where it doesn't work well and also some we did not have to class from test anymore and we didn't have to
put the test is thing that class at all but we can still do that if we like and that can become become kind of nice to to group protests and we know that this suit by running the tidal test which collects and runs that has
so 1 thing about ITER is it has a very powerful plugin system so uh in your own project you have a lot of books into practice that you can use to customize how tests are collected how they are picked up and how they are invoked and then you have the same kind of books the interplay testing your own projects that third-party plug-ins has access to so these are some plug-ins uh that extend the capabilities of fighters uh and there are a lot more but these are the probably the most popular ones the pipe thinks this uh plugin that provides distributed testing so you can actually run your test somebody directly on have them distributed to remote machine or you you can run them locally on your own machine in parallel processes has to have some nice speedups I'll come back to Texas later and this part is giant plugin which helps to integrate with the with jungle which I the testsuites and the make them run directly there's also uh support for running twisted played twisted tests and a lot lots of other inference if you do not capturing you can install their capture
so how to actually run the tests showed interview mode biased invoking pilot test you run all the tests and I win
recursively search all directories find testifies so this is this is usually what you do at around entire test uh but then you can also limits that tests your undermining
locations bias specifying the violence of the test files that you want to run and this of course works very nice with the standard have
liked have completion in in yourself so it's very convenient to use you can also
match you can also select test based on that tests and names so stands for a keyword and this can be used to find a specific test if you don't want to type 2 directories so as essentially a part recursive center director is sometimes specified and by default it looks into the files that matches these factors and you can of course change that if you like if you're project structure looks different but that's kind of same default there is
another way of organizing your
tests with by this you can use the marking functionality and as you can make up arbitrary marks and I just made up this slow marked for instance if you want to mark your all the tests that are very slow so and then you may not want to run them or you may want to run them you may want to filter out you can put any of you can put multiple marks someone tests if you like so this is how you around only the tests with that specific mark and in this case this might actually be more useful because you may not want to run them that often there
are also markers that are built into practice for instance the escape if market so you can also
annotate some extra data within the market for each task so in this case that this test will simply be skipped if you run it on my was but you can implement you can implement your own the logic with markers by yourself it's in your project at specific things project and so I like to take
some time to go a bit into a feature in pipe test that is called a fixtures so fixtures is a mechanism for injecting
objects that you need for your tests so in a in a very structured way as you probably have no idea what I'm talking about right now so I'm going to try to to to give a this so we
have to test series which use as an it do you know what so many missed by the way uh yeah it's so it's it's a way to remote control the web browser and that can be very useful for tests some using that and as as an example here as so I have to test they will navigate to these websites and they will look at the title of the web sites and verified that it there that makes this in then so as the next package manager websites and the pi testing is in the fight this website then so I want you to what happens here so when type finds this the test we and the Web driver argument in the argument list it was and try to construct this value the value for this argument so in order to do that it looks for and fixtures that named when driver so it works by looking under the name of the argument itself and in order to register how these features are constructed use you create a function with the same name and then you undertake that function with the tightest fixture decorator so that this function should return the objects that we want to have passed into the text so in this instance I used create the Firefox Web during stance and I also have a some code that will run and in the process of retired out when the tests are finished how to clean up after this tests and so I uh of this driver quick methods a method that will be invoked after the test and this is the driver so this is what happens for the 1st test and then for the next test the same thing will happen it will be constructed once again and past in and then the tests were around and then they went driver will correct OK so let's let's just run this and see what
happens and the hope for and the wireless with me OK so we can see that OK so it seems to
work well but it was very fast so and we have Firefox start up close down and start up for another test so that's so that's what we expect that 1 of very important point of features is that you move there the dependency construction out of the test case themselves so the test case is not concerned about how they then a test features are set out and this has some nice the implications so we can for instance you saw that Firefox started twice let's say we want to we want to use the same instance of Firefox for for both tests at that is really simple uh and actually everything we have to do is to add a we have have to tell my just how and this fixtures this should be scoped or how it should be cached so we established impact will then cash this fixture object for the entire set of test session so this means that if we try to run together might be very
fast but we should only see 1 and I want Firefox window showing up
became to was surface but did you see it can certainly the tests after very nice for in a lot of cases for instance creating a database let's imagine your tests need a database with a ski mask and everything in them and then you can create that 1 before Walter strong and then having to be used for every test and that the other thing that we when we separated the creation of the fixtures is that these tests don't need to be concerned about what kind of browser we use we can for instance change this to crawl and it will have a just work and we can even generates more tests invocations we can generate new test by parametric parametrizing this fixture so we can actually trigger each test to run in multiple browsers just by changing this fixed a definition so another thing
I want to talk about I mentioned the tightest x this plugin in early there is a way to run your tests in parallel so that can be that can
give you a very nice speed because most people have moved the decorum machines these days so the good thing is it's really easy to use it so if I want to if want to speed up these tests so we can just run them in parallel so I already installed applied to stakes this plugin so that was dash and we can then construct fighters to run these parallel so I just stuck to process and this will this will probably be with that very fast but also see if we can
yeah the views so there are 2 Firefox browsers at the same time so these tests now ran in parallel and so that's a very easy way to speed up their runtime of
attested just install Titus text this and you run it with dust and to the rest of some my own testsuite it usually takes about 2 minutes to run and when I run it on my machine with the air in for processes it's about 30 seconds so it can give you a very nice speedups so
once again that it's the dependency injection and it's based on the name of the arguments and the name of the feature function and that's how they not together and how how they're found variance I
was you can whole new right yes so so in this case you you if you want to have both Firefox and Chrome around you specified implementation of of this fixtures itself so uh instructor I have I don't have time to show you that not about you can tailpipe has 2 for each at each time a test needs this fixture invoke it twice once with Firefox ones with true uh and you can also you can also use multiple fixtures just add more and more fixtures to the argument list of of you can you might want to have a database you might want to have access to something else to or just another object so you can you can use multiple fixtures in argument is and you can also so the web driver fixture in in in spring in this case it can depend on other fixtures to uh but I want to go more into detail about features this is just that it's very powerful and it can be used in a lot of different scenarios yes yes I'm and
then I'll tell you about it and so um I also show that next this but it's still it doesn't have anything to do with fixtures but it's a nice way to speed up the tests so and this is how easy it is to get started with the just a soliton than uh around would do that and come on
line you cannot do all sorts of and remote distribute the tests with it to but this is a simple case so
I want to talk a bit about how you can import if you want to switch to fight us how it how you can a good strategy strategy for doing that and so I showed you that the unit tests was
just the test case was just picked up on a by typists and there there's also so actually support for no style has so if you use most currently you can probably just which it should mostly work out of out of the box and if you saw the widest Jan plug in Europe I test your jungle testsuites that should also long as they are so that in that way you can start using the Python commands to to run your tests and then you can gradually great new tests in this life despite and you can make use of features and then you can change your existing tests time you can just leave them as they are but the them even the unit tests and most tests that they will get benefits such as the parallelization from this so you don't have to go and thrown at us away and start over in order to start using Python it's you can switch gradually yes so I highly
recommend that if you want to know more about fixtures that you go to the 6th floor is thought to this afternoon about fixtures so I just scratched on the server based on what you can do with fixtures and he would show show you more advanced users of so don't don't miss that for an and also there will be a training with only attractive who's the main author applied test uh so that would be on Friday natural it's still possible to sign up for the
trends of check it out if you're interested and I will do some spending on the tightest Django bindings and plugin on the indispensable feel free to to join in there if you like to get started with it see what it's all about so we have
to to free to just come and talk to me if you have any questions after or I guess we whether it's time for some questions minutes of equal where you started to test
fixtures let let us for the requests argument come from area that's a good that's a good question so at the request argument in this especially fixtures that
you can use to get information you can introspect their surroundings you can get for instance if you need access to a command line argument from within the fixture it's available on the request uh and the request object so you can leave it out in the feature definition if you like if you don't have any use for it uh but it provides the access to 2 pi test basically so you that you can communicate with it so it's a special fixtures that gets passed into this picture more them
right another convinced I must say so my question is what features of light that you would say you use every day and you wouldn't find and other things like your best nose and stuff like that because just love having a class inheriting from the best of this case is not a very strong and how many of you have I would say that the fixtures is really unique feature uh and it's it's hard to to show you how powerful they are in this short time but then you can combine them in different ways and combine the caching of the scoping of them and you can parametrize them and you can really have you have have a very good structure of how tests I invoke so I would say that that is the up to the biggest uh like feature tests when if you compare it to 2 other frameworks kind and questioned home once more difficult it gets to the black hole tests with 5 test so in unique test you need to do much more at so you don't have this nice features but you do everything to explicitly and here you have a lot of features that are nice but the height and and so the work it is done by itself but a you use it every day so it is gets more difficult to problems here in your text a well I've used it for a while so I have but that I wouldn't say it's harder to the divide it and there are all sorts of you can you can run like a pilot test collect only and it will show you how it collects the tests and you can have all sorts of different outputs can have very verbose outputs if you really you can get a lot of information about the favorite failure so in in that sense I guess you get can get more information out of high tests that with standard unit test but not I don't see it as a problem I I I don't really I see that it makes it harder to the bottom of thanks any more questions who
fled is taking a cell that recently in 1 of the problems and things like that is the case discovered so it's 1 of the plant is easy to engage in all of can entering the consulate thank the 10 by efficient all equally the example that the hand that looked up parts only can by necessary TV impacts they contained so was on and that it was in London is it anything can find a case that haven't whether it
should run tests that are marked to use them off the greater part of its system have if the occasional slightly less failed packed full any kind and by then realize that I had not OK I don't take anything you can think of this as you think you should have gotten an exception them and there it you misspelled the the patch decorative something that should have favored during the collection phase here that is affiliated say try 1 OK I not sure in a particular case but if if there are errors during like any important things or standard error is like the they should be shown in the so you see a flag to show the skid tests for yeah so the reports gifts and a command-line arguments against yeah they control the skid tests thanks anymore in the OK thank you very much
Softwarewartung
Signifikanztest
Punkt
PCMCIA
Code
Mathematisierung
Signifikanztest
Weg <Topologie>
Statistischer Test
Kartesische Koordinaten
Ordnung <Mathematik>
Computeranimation
Signifikanztest
Statistischer Test
Statistischer Test
Projektive Ebene
Plug in
Physikalisches System
Textbaustein
Code
Computeranimation
Signifikanztest
Softwaretest
Komponententest
Software
Statistischer Test
Physikalisches System
Modul
Ganze Funktion
Computeranimation
Funktion <Mathematik>
Metropolitan area network
Zentrische Streckung
Einheit <Mathematik>
Quader
Selbst organisierendes System
Statistischer Test
Kontinuierliche Integration
Projektive Ebene
Physikalisches System
Computeranimation
Richtung
Signifikanztest
Bitmap-Graphik
Lineares Funktional
Demo <Programm>
Komponententest
Objektklasse
Elektronische Publikation
Code
Signifikanztest
Computeranimation
Statistischer Test
Statistischer Test
Textbaustein
Demo <Programm>
Signifikanztest
Nichtlinearer Operator
Befehl <Informatik>
Komponententest
Klasse <Mathematik>
Mathematisierung
Einfach zusammenhängender Raum
Physikalisches System
Objektklasse
Modul
Signifikanztest
Ebener Graph
Computeranimation
Einheit <Mathematik>
Diskrete-Elemente-Methode
Demo <Programm>
Funktion <Mathematik>
Signifikanztest
Rückkopplung
Einfach zusammenhängender Raum
Signifikanztest
Computeranimation
Demo <Programm>
Funktion <Mathematik>
Statistischer Test
Diskrete-Elemente-Methode
Reverse Engineering
Codierung
Einfach zusammenhängender Raum
Kantenfärbung
Signifikanztest
Computeranimation
Demo <Programm>
Signifikanztest
Suite <Programmpaket>
Befehl <Informatik>
Klasse <Mathematik>
Gruppenkeim
Befehl <Informatik>
Computeranimation
Data Dictionary
Statistischer Test
Schlüsselverwaltung
Störungstheorie
Instantiierung
Demo <Programm>
Inklusion <Mathematik>
Signifikanztest
ATM
Prozess <Physik>
Inferenz <Künstliche Intelligenz>
Logarithmus
Plug in
Physikalisches System
Signifikanztest
Computeranimation
Eins
Motion Capturing
Virtuelle Maschine
Statistischer Test
Suite <Programmpaket>
Statistischer Test
Mereologie
Projektive Ebene
Informationssystem
Leistung <Physik>
Signifikanztest
Statistischer Test
Elektronische Publikation
Statistischer Test
URL
Verzeichnisdienst
Signifikanztest
Computeranimation
Standardabweichung
Signifikanztest
Matching <Graphentheorie>
Mathematisierung
Elektronische Publikation
Signifikanztest
Teilbarkeit
Computeranimation
Statistischer Test
Statistischer Test
Mereologie
Datentyp
Projektive Ebene
Rekursive Funktion
Datenstruktur
Default
Verzeichnisdienst
Portscanner
Statistischer Test
Multiplikation
Statistischer Test
Maskierung <Informatik>
Datenfluss
Signifikanztest
Computeranimation
Instantiierung
Umwandlungsenthalpie
Task
Signifikanztest
Kraftfahrzeugmechatroniker
Statistischer Test
Bit
Projektive Ebene
Mathematische Logik
Signifikanztest
Computeranimation
Objekt <Kategorie>
Statistischer Test
Reverse Engineering
Einfach zusammenhängender Raum
Signifikanztest
Computeranimation
Demo <Programm>
Signifikanztest
Lineares Funktional
Parametersystem
Fernwartung
Web Site
Prozess <Physik>
Browser
Reihe
Web Site
Mailing-Liste
Bildschirmtext
Signifikanztest
Code
Computeranimation
Objekt <Kategorie>
Benutzerbeteiligung
Druckertreiber
Datenmanagement
Statistischer Test
Datentyp
Ordnung <Mathematik>
Druckertreiber
Demo <Programm>
Instantiierung
Signifikanztest
Konstruktor <Informatik>
Punkt
Web Site
Benutzerbeteiligung
Signifikanztest
Computeranimation
Objekt <Kategorie>
Diskrete-Elemente-Methode
Menge
Statistischer Test
Bildschirmfenster
Ganze Funktion
Druckertreiber
Instantiierung
Demo <Programm>
Signifikanztest
Browser
Datenhaltung
Mathematisierung
Plug in
Web Site
Signifikanztest
Computeranimation
Verdeckungsrechnung
Statistischer Test
Flächentheorie
Druckertreiber
Instantiierung
Demo <Programm>
Virtuelle Maschine
Sichtenkonzept
Statistischer Test
Browser
Regulärer Ausdruck
Plug in
Parallele Schnittstelle
Signifikanztest
Computeranimation
Demo <Programm>
Lineares Funktional
Parametersystem
Virtuelle Maschine
Prozess <Physik>
Injektivität
Zwei
Gasströmung
Installation <Informatik>
Signifikanztest
Varianz
Computeranimation
Quelle <Physik>
Signifikanztest
Parametersystem
Subtraktion
Datenhaltung
Gasströmung
Mailing-Liste
Signifikanztest
Computeranimation
Eins
Objekt <Kategorie>
Statistischer Test
Multiplikation
Benutzerbeteiligung
Druckertreiber
Benutzerführung
Statistischer Test
RPC
Statistischer Test
Soliton
Signifikanztest
Gerade
Computeranimation
Signifikanztest
Bit
Komponententest
Statistischer Test
Güte der Anpassung
Strategisches Spiel
Plug in
Ordnung <Mathematik>
Parallele Schnittstelle
Computeranimation
Autorisierung
Signifikanztest
Schnelltaste
Wellenpaket
Twitter <Softwareplattform>
Natürliche Zahl
Kraft
Server
Plug in
Signifikanztest
Computeranimation
Signifikanztest
Parametersystem
Flächeninhalt
E-Mail
Computeranimation
Signifikanztest
Subtraktion
Komponententest
Klasse <Mathematik>
Signifikanztest
Teilbarkeit
Quick-Sort
Framework <Informatik>
Computeranimation
Objekt <Kategorie>
Statistischer Test
Caching
Minimum
Information
Datenstruktur
Instantiierung
Funktion <Mathematik>
Standardabweichung
Patch <Software>
Modallogik
Statistischer Test
Fahne <Mathematik>
Mereologie
Zellularer Automat
Ausnahmebehandlung
Physikalisches System
E-Mail
Stichprobenfehler
Verkehrsinformation
Phasenumwandlung
Computeranimation
Code

Metadaten

Formale Metadaten

Titel Introduction to pytest
Serientitel EuroPython 2014
Teil 41
Anzahl der Teile 120
Autor Pelme, Andreas
Lizenz CC-Namensnennung 3.0 Unported:
Sie dürfen das Werk bzw. den Inhalt zu jedem legalen 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.
DOI 10.5446/19941
Herausgeber EuroPython
Erscheinungsjahr 2014
Sprache Englisch
Produktionsort Berlin

Inhaltliche Metadaten

Fachgebiet Informatik
Abstract Andreas Pelme - Introduction to pytest pytest is a full featured testing tool that makes it possible to write “pythonic” tests. This talk will introduce pytest and some of its unique and innovative features. It will help you get started with pytest for new or existing projects, by showing basic usage and configuration. ----- This talk will show introduce pytest and show some unique and innovative features. It will show how to get started using it and some of the most important features. One of these features is the ability to write tests in a more “pythonic” way by using the assert statement for assertions. Another feature in pytest is fixtures – a way to handle test dependencies in a structured way. This talk will introduce the concept of fixtures and show how they can be used. No previous knowledge of pytest is required – this talk is for people who are new to testing or has experience with other Python testing tools such as unittest or Nose.
Schlagwörter EuroPython Conference
EP 2014
EuroPython 2014

Ähnliche Filme

Loading...