Keynote - Testing in Django

Video in TIB AV-Portal: Keynote - Testing in Django

Formal Metadata

Keynote - Testing in Django
Title of Series
Part Number
Number of Parts
CC Attribution - 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
The Django documentation section on testing starts with this: “Automated testing is an extremely useful bug-killing tool for the modern Web developer.” Nobody can argue with that. Testing is an integral part of modern software development, and Ana’s talk will offer an in-depth overview of how the Django testing framework evolved; showcase some common techniques, tools, and best practices; talk about speed improvements; and guide you through a real-world example of testing a Django app. Testing is fun, isn’t it?
Statistical hypothesis testing Computer animation Food energy Statistical hypothesis testing
Arithmetic mean Integrated development environment State of matter Term (mathematics) Touch typing Video game Self-organization Twitter Identity management
Computer animation Multiplication sign Bit Right angle
Statistical hypothesis testing Point (geometry) Mobile app Functional (mathematics) Multiplication sign Parameter (computer programming) Client (computing) Revision control Term (mathematics) Integrated development environment Software framework Endliche Modelltheorie Social class Task (computing) Execution unit Bit Unit testing Cartesian coordinate system Statistical hypothesis testing Data management Computer animation Integrated development environment Software framework Quicksort Resultant
Statistical hypothesis testing Dependent and independent variables State of matter View (database) Interface (computing) Gender 1 (number) Client (computing) Client (computing) Web browser Unit testing Inverse element Template (C++) Template (C++) Revision control Computer animation Personal digital assistant Personal digital assistant Extension (kinesiology) Task (computing) Library (computing)
Statistical hypothesis testing Point (geometry) Functional (mathematics) State of matter Set (mathematics) Database Client (computing) Rollback (data management) Database transaction Software bug Revision control Different (Kate Ryan album) Social class Task (computing) Default (computer science) Block (periodic table) Gender Interface (computing) Rollback (data management) Client (computing) Database transaction Database Tablet computer Computer configuration Computer animation Personal digital assistant
Filter <Stochastik> Statistical hypothesis testing Computer animation Gender Interactive television Phase transition Software framework Insertion loss Bit Error message Field (computer science) Number
Statistical hypothesis testing Implementation Interactive television Set (mathematics) Database Parameter (computer programming) Replication (computing) Data dictionary Revision control Mathematics Strategy game Software framework Task (computing) Default (computer science) Multiplication Database Unit testing Connected space Computer animation Integrated development environment Phase transition Triangle Website Right angle Quicksort Resultant
Statistical hypothesis testing Server (computing) Service (economics) Equaliser (mathematics) Set (mathematics) Numbering scheme Client (computing) Mereology Database transaction Number Attribute grammar Latent heat Personal digital assistant Software framework Dependent and independent variables Concentric Server (computing) Structural load Projective plane Database transaction Database Statistical hypothesis testing Computer animation Query language Personal digital assistant Factory (trading post) HTTP cookie Family Communications protocol
Point (geometry) Statistical hypothesis testing Default (computer science) Group action State of matter Multiplication sign Software developer Database Database transaction Database Bit Rollback (data management) Database transaction Sequence Statistical hypothesis testing Computer animation Personal digital assistant Personal digital assistant Order (biology) Right angle Freeware
Statistical hypothesis testing Mobile app Computer file Patch (Unix) Real number Electronic mailing list Client (computing) Basis <Mathematik> Client (computing) Line (geometry) Directory service Statistical hypothesis testing Revision control Latent heat Message passing Computer animation Term (mathematics) File system Endliche Modelltheorie
Statistical hypothesis testing Server (computing) Computer file Closed set Real number Multiplication sign Execution unit Client (computing) Client (computing) Unit testing Regular graph Revision control Computer animation Integrated development environment Personal digital assistant Video game
Statistical hypothesis testing State of matter Texture mapping Multiplication sign 1 (number) Number Revision control Mathematics Strategy game Mehrprozessorsystem Different (Kate Ryan album) Personal digital assistant Endliche Modelltheorie Error message Plug-in (computing) God Social class Module (mathematics) Multiplication Block (periodic table) Gender Projective plane Stress (mechanics) Database transaction Database Substitute good Statistical hypothesis testing Connected space Word Process (computing) Computer animation Personal digital assistant Atomic number Resultant
Statistical hypothesis testing Point (geometry) Area Module (mathematics) Context awareness Parity (mathematics) Block (periodic table) Similarity (geometry) Unit testing Parallel port Revision control Type theory Data management Computer animation Computer configuration Software framework Quicksort Traffic reporting Plug-in (computing) Resultant
Statistical hypothesis testing Email Dataflow Building Functional (mathematics) Translation (relic) Event horizon Number Formal language Data management Computer configuration Software framework Task (computing) Email Weight Structural load Projective plane Instance (computer science) Line (geometry) Unit testing Statistical hypothesis testing Data management Computer configuration Computer animation Integrated development environment Personal digital assistant Volumenvisualisierung Asynchronous Transfer Mode Library (computing)
Statistical hypothesis testing Point (geometry) Email Email Suite (music) State of matter Direction (geometry) Set (mathematics) Database Total S.A. Unit testing Number Computer animation Integrated development environment Order (biology) Resultant
Statistical hypothesis testing Database Insertion loss Client (computing) Database transaction Revision control Hierarchy Personal digital assistant Query language Social class Addition Server (computing) Interactive television Client (computing) Database transaction Database Line (geometry) Unit testing Thread (computing) Statistical hypothesis testing Computer animation Personal digital assistant Query language Video game Point cloud Social class
Statistical hypothesis testing Axiom of choice Server (computing) Context awareness Group action Dependent and independent variables Structural load Set (mathematics) Web browser Client (computing) Event horizon Template (C++) Factory (trading post) Utility software Exception handling Dependent and independent variables Matching (graph theory) Client (computing) Computer animation Factory (trading post) Chain HTTP cookie Object (grammar) Middleware Resolvent formalism
Statistical hypothesis testing Random number Texture mapping Multiplication sign Keyboard shortcut Mereology Rule of inference Computer animation Different (Kate Ryan album) Internet service provider Factory (trading post) Factory (trading post) Endliche Modelltheorie Library (computing)
Statistical hypothesis testing Randomization Group action Presentation of a group State of matter Multiplication sign Set (mathematics) Mereology Software bug Strategy game Spherical cap Different (Kate Ryan album) Single-precision floating-point format Personal digital assistant Endliche Modelltheorie Category of being Exception handling Collaborationism Software developer Electronic mailing list Database transaction Instance (computer science) Hypothesis Statistical hypothesis testing Category of being Internet service provider output Endliche Modelltheorie Quicksort Sinc function Point (geometry) Functional (mathematics) Implementation Real number Collaborationism Electronic mailing list Hypothesis Authorization Energy level Module (mathematics) Focus (optics) Gender Projective plane Coma Berenices Limit (category theory) Word Computer animation Personal digital assistant Web-Designer Factory (trading post) Integer Local ring Library (computing)
Statistical hypothesis testing Logical constant Point (geometry) Metre Implementation Group action Sequel Code Multiplication sign Source code Branch (computer science) Computer programming Machine vision Twitter Number Revision control Cross-correlation Insertion loss Operator (mathematics) Logic Process (computing) Implementation Condition number Addition Logical constant Electronic mailing list Operator (mathematics) Line (geometry) Statistical hypothesis testing Category of being Computer animation Logic Personal digital assistant Statement (computer science) Resultant Conditional probability
Statistical hypothesis testing Web page Dependent and independent variables Game controller Code Multiplication sign Decimal Sheaf (mathematics) Mereology Statistical hypothesis testing Data model Human migration Computer animation Hypermedia Internetworking Universe (mathematics) Software framework Task (computing)
Statistical hypothesis testing Functional (mathematics) Run time (program lifecycle phase) Feedback Software developer Feedback Total S.A. Statistical hypothesis testing Number Uniform resource locator Computer animation Energy level Cycle (graph theory) Loop (music) Social class
Point (geometry) Statistical hypothesis testing Dialect Asynchronous Transfer Mode Multiplication sign Execution unit 1 (number) Set (mathematics) Number Data model Semiconductor memory Object (grammar) Factory (trading post) Flag Funktionalintegral Endliche Modelltheorie Robot Execution unit Gender Feedback Range (statistics) Database Bit Unit testing Instance (computer science) Flow separation Statistical hypothesis testing Computer animation Personal digital assistant Password Factory (trading post) Object (grammar) Resultant Asynchronous Transfer Mode
Statistical hypothesis testing Dialect Execution unit Functional (mathematics) INTEGRAL Gender Multiplication sign Execution unit Design by contract Unit testing Function (mathematics) Statistical hypothesis testing Revision control Computer animation Personal digital assistant Social class
Greatest element Functional (mathematics) Dependent and independent variables Code Decision theory View (database) Product (business) Number Bit rate Universal product code Different (Kate Ryan album) Endliche Modelltheorie Proxy server Task (computing) Form (programming) Key (cryptography) Projective plane Code Counting Client (computing) Database Product (business) Category of being Computer animation Personal digital assistant Network topology Calculation Object (grammar) Abelian category
Statistical hypothesis testing Point (geometry) Inheritance (object-oriented programming) Database Branch (computer science) Instance (computer science) Function (mathematics) Mereology Product (business) Number Endliche Modelltheorie output Router (computing) Task (computing) Form (programming) Interactive television Database Product (business) Computer animation Personal digital assistant Function (mathematics) Calculation output Object (grammar) Routing Router (computing)
Statistical hypothesis testing Programming language Functional (mathematics) Connectivity (graph theory) Constructor (object-oriented programming) Database Function (mathematics) Parameter (computer programming) Product (business) Web 2.0 Computer animation Different (Kate Ryan album) Function (mathematics) Calculation Core dump Software framework Object (grammar) output Resultant Dean number Form (programming) Social class
Computer animation Different (Kate Ryan album) Function (mathematics) View (database) Calculation Software developer output Function (mathematics) output Extension (kinesiology) Form (programming) Stability theory
Statistical hypothesis testing Complex (psychology) Building Group action Theory of relativity Connectivity (graph theory) Gender Coma Berenices Database Unit testing Statistical hypothesis testing Data management Computer animation Term (mathematics) Logic Funktionalintegral Object (grammar) Form (programming)
and the and i on the wrong
I and I'm in a very
uh no and but there learning how great re-energizing full of energy of just repeating myself in full of energy to get started with a date will allow Rio and yes we are the and they get started with a very long talk uh from overview of testing in Django and within Django I'd like to say
thank you to the organizers and volunteers who make dangle con US happen because you and I we have no idea how much effort it takes to bring the conference life not only that but also to create a welcoming environment where we all the state so this is the ground thank
the you might be wondering why I'm an the and this is my Twitter handle it if you want to get in touch with me of there's nothing you probably haven't heard about me because there's nothing outstanding that I did in my life Dinarelli I'm the kind of person goes to work every day and tries to be the but the best developed I can become are usually say I'm mean of long-term successful and have a relationship with Python have been doing Django for the past couple of years and in terms of community involvement I usually do little things so and not currently I'm co-organizing uh London by ladies out and I How it's organized a identical con Europe last year and I sometimes mental people and most often I give talks at conferences I recently joined a company called
street team as a senior Selfridge again
so yes questions I won't be taking questions on stage and there are 2 reasons for that our 1st is we actually might run out of time and the 2nd 1 is it never works out for me I'll be honest at 1 stage I'm a bit nervous and by the end of the talk uh it's getting a little bit tricky to think straight and to give a proper answer to a question and I feel like I'm actually doing a disservice so as they get offstage uh the agenda on all the rest of the conference until Friday so come and out comes we have questions comments on the back of the happy to chat the right so now that we got to know
each other a little bit better Alytus started with testing we're going to take a trip all the way from version 1 . 0 to 1 . 11 and see how the Django test framework evolve over time it all started about 10 11 years ago sometime before getting goes out of the term this they can get creative and unit test framework for and user Django applications and it lists all sorts of benefits that you can have while having an integrated framework to test your apps but is this killer argument as an added incentive this is a feature that is present in rails and and that's how I started we started with a management command test it accepts syllabus at the time that when 2 test so OUT adult test class the test method
we get a test run on a test run is also a complex variable that points to a function that Russia tests that means that from the very beginning anyone can write their own test and his discovery that is strong incentives the test environment and it looks up the task involves does that buying and models that right at the very end the test runner tear downs the test environment and returns the results at this point you might be squinting and thinking models of by the she serious well 2 years ago apparently doctests were very popular and they will to test for a lot so models of y where the place where model an application-level test out meant to be and everything else was left to testify client makes its appearance
as a basic state full browser minicam mimicking style that constructs requests passes them to the views and returns a response by pressing all networking altogether and interacting directly with the with the interface and it's called client it is the same kind that we have today is just a more limited 1 if you're
writing your tasks you using Python unit test library and you've probably heard of test case for gender also has its own version of the task is which is an extension of the unit test this case because it can also do extra things like light fixtures and the inverse of mailbox and initialize client and clean clean the data after each test it provides a couple of a set helper methods for example I set redirects and template use the ones that is still have today version 1 . 1
comes shortly after with the
revolutionary set of HTTP methods last so aggressively denies are not get the next big thing but gender that into the future and as to the client bullets had to delete and options along with a
test case we also get a transaction Descartes this is where we need to make a clear distinction what is the difference between a test case and a transaction test case so a test
case acts like a bug because it wraps in test into an atomic block it enters the transaction in runs the test the test does Massey things that the database and finally the test case rollbacks this transaction and you use a clean slate for the next test because of that the task is will always prevent nd commits war rollbacks to the database because uh it once you sure that at the very end where rollbacks the transaction that task restores the database to its initial state and this is a big performance booster to the test cases and for us to writing out tests if you want to use transactions that this is where you need to use transaction test case it will clean up after each task by flushing the database at the start of each test and by the way the test case from the previous version 1 . 0 was doing exactly that 1 . gender defines a new interface but astronomers so with switching from a function to cause the class should have a method called run tests that will act as an entry point by default Django provides Django tested run so this is not
really related to the evolutionary of gender test framework but it's there it's a city about that I really like so I decided that it and so this that's 1 returns the number of errors last the all some together and we're Django does it spits out this number of field test that assists access so if you have if no tests have failed then it will naturally acts it with a 0 which is a success if you have 1 test phase of and this is going to be 1 which is a failure that that's correct you have quantity field has the fine well if you have 256 it suddenly becomes a success at a loss this is the consis adds the number is an 8 bit number so it overflows into 0 we have 256 and talking from experience it's not very hard to get 256 filters a so we fix it we do face sex at 1 so now will
always either get a 0 success or 1 for a failure 256
now is a failure as it should be the let's take a look at the definition of the test runner because something change from the previous fashion this any argument fail fast it allows you to terminate the test run on in the 1st phase them I find it very handy and he'd be implementation has been removed ever since from the triangle test frame 1 because it's now available to out within the Python unit testing but good to know it exists don't
has support for multiple databases with a replication strategy so you can have a primary database and multiple replicas can be created this is how it looks like in dB settings bizarre default database and there's a replica if the task mirror site from the replica dictionary is missing then there won't be any replication happening during your tests just the default database is created and tested against if you want to test of replication that you need to set up a test new and if you want to use it today settings have changed to um should look like this so let's go back to our database when the test environment is configured a test version of the replica is not created instead the connection to replica will be redirected to point a default so as a result the rights to the default will appear on the replica but not because there in this sort of data replication happening between the 2 but because it's actually the same database 1
. 3 we can now do a set course at equal and a certain number of queries which is handy for the test case we
also see as all the tools that declined in body so the role of manufacturing request is turned over to the Request factory and obtaining the response was left to the client the Request factory becomes a part of the public documented API so how do you manufacture request for the Request factory and simple you just have created a whiskey request object out with a set of whiskey and request specific attributes like this protocol the request method the load cookies neural scheme that's pretty much it doesn't do anything else very simple very attractive fish so the 1st in the beginning it seemed like such a great idea you get both tests and go and you get of concentration in the long run it became a nuisance to be cut to develop it also became apparent that don't test aren't really good at providing neither high-quality documentation new comprehensive tests that's why doctors will be discovery discouraged to be used in the future and is a good example of a big project that came from a long stage with the doctors and just prove that this relationship can't work in 1 . 4 emotional and when windfall the family of a test case courses receives and accept an extension of so we also we get a simple test case and the lights out as a simple test case is very light out because it doesn't hit the database my services case runs an HTTP server so that you can use a testing framework like selenium out for you and to and tests and it is based on the transaction test case question 1 . 5 is a
time when points and free supports landed in Django well we talked about the transaction
test case and we say that it's flushing the database before each test well at the time of the test run has become popular for example letters and developers figured out that there's a bit of a mess a transaction test case so it has been updated so that now we flushed a database not before each test but after let me show you so if you foster database uh before that as you get the following sequence of actions you flush around the transaction test case and then you can run your test cases what happens is that right after running the election test case you get some dirty state by writing you test cases actually start with a not clean database that's why uh Django default test runner is always rearranging tests so that the test cases 1 1st and then the transaction test cases to come and even if even though the transaction does case he's some stating that actually doesn't matter because we're going to destroy the database said we can solve this problem in a different way we can just flush after and then all all problems are solved it doesn't really matter what order we're running our tests 1 . 6
so there's 1 more HCP method added to the client basis patch and if you thought this list is complete it is not much of 1 . 6 see various
improvements we give a new test run and that is able to the k test not only in terms that by models of by by anywhere from being installed installed perhaps as long as it matches specific file patents you just discovery also means that we have no more in models that line here a senate-passed also removed so now we can use real file system for Dr. directory pass this also allows running tests that are not inside a Django installed apps and finally doctors won't be discovered anymore and we say farewell to them 1 . 7 2 free versions
before 1 . 7 introduce unit test to unit testing is the back forward of the unit test features that work with older versions of white because gentle the time was supporting wife and 2 . 4 it was found during unit test is 1 . 7 Django dropped support for older versions of Python and so it removes the unit tested and we just use the regular unit test and at 1st when life sentence case was introduced it was serving all the static files as if you were running your server which equal straight from this point on life sentence case was so only files from the static great trying to simulate the real production environment as close as possible if you want to so all of the static files then you can use the static life so the test case In 1 . 8 we get to the last known age method to the test client can you tell me which 1 is that we want our missing the the it should this that someone had this is
traced this is the last 1 and once this 1 was defined uh the client is complete this case and it's this
strategy was a performance goes down and around version 1 . 8 if you are the intricate changes way introduced that made the test cases even better before that thing version 1 . 7 a test case there is wrap each destined to a transaction but it did in the following way you and to the atomic block and you would load the fixtures that run the test you answered the atomic uh in other words roll back then you close of your database connections and you'd read that for every single test that you have it With the arrival of gender 1 . 8 we can do something better so we actually run all of our uh tests from a test case inside of a different atomic block that there is going to happen only once and what we are doing we are limiting the fixtures and we're closing the connections at a single time rather than doing it for every other test so now every other every test within also runs within its own transaction book that we get also the benefit of closing the connections and loading all fixtures at a single time rather than doing it every time 1 . 9
who at work school works on large projects knows the pain of waiting for test to finish my experience that would take the pain starts with about 20 minutes sometimes it's foolish he that's how large it is I'm and most of us know that the quick easy win but that is to paralyze our tests and also that's the time when you find out how many non-pure tests you don't 0 1 . 9 comes with a built-in support for parallelizing using Python multiprocessing module in fact and was tested is also paralyzed how does it work the test runner sponsors and number of workers so you can specify how many people think up the max number of course that the have and for each process I will get a his Eric its own database so each each worker gets its own database that we have for work is you get get for databases does discovery builds a tested our which is partitioned into chunks of test case uh some classes what is it those substitute of stress and run them once they are finished that pick up the next 1 the finished that because the next 1 until they're done and they return a result it if you start with a version of Django prior to 1 . 9 then you can use knows multiprocess plugin but it's also using Python multi processing model for the main difference between the Django ones and the news multiprocessor is that you get only 1 database so I do get shared state and because God sometimes can get this all our failures and errors 1 . 10 my favorite feature that was introduced in
1 . 10 is tags you can tag your test creating buckets for certain types of tests that you'd like to run together where you'd like to exclude from your test run similar features implemented by other
test from as like nose and by testing cannot engender 1 point and you always have the option to use knows a trip plugin or the widest as custom markers so and finally 1 . 11 we get all sorts of speedups and improvements in fact with every version we continuously tried to get back them in in in different areas of the testing framework and but there's is 1 feature that caught my eye which I think is nice to know about so starting with Python 3 . 4 unit test module adds the context manager hold some test it says test executes the enclosed block as a subtest in this example when happens is that the test won't and adds it all its 1st failure when i equals 1 because 1 is an odd number it will actually run through all of the options and it will I read in the will return all of the results for all of the values of the i so now we can go 1 . 11 you can do that you can subtest when running tests in parallel and jangle report all the results correctly if your with quite test you've probably this probably looks familiar to you because look quite as has parametrized we went through the
history of the GenGO test frame like and with touchstone no different topics here and there but let's see the flow of running the tests you as a whole all what happens when you run managed by test
so let's fill the test that into multiple pieces and analyze each 1 of them and see what happens under the hood we're both the management commands test and if we zoom in
came back and it was so in this pretty much a straightforward implications so we're getting the test runner and with creating an instance of it passing a bunch of options and we run the test that's the mean inside 1 task so the 1st line in run the setting up the test environment that will do a bunch of things so the main free uh keep weights of this method is setting up the lock e-mail back and so that all the e-mail that was sending during task is stored into this established that we can inspect and we can the access it easily this is a very reduced e-mail back and in its functionality we also get Django um the GenGO test render it is replaced by an instrumented test render that it sends signals to let others know uh about various events that happened during rendering and finally we deactivate translation so that during test you project will the test with respect to projects that language good the next step after setting up the test environment is to build the seed of tests the heavy load of building this year is done by the Python unit test our library but generates stands at a because it allows you to specify the tags and number of who was mode and all the different things so it builds up a pseudo test this is an aggregation of instances of this case crosses all connected in a single place and and there's nothing more about it the next step we set the
databases we're the checks then we run the test which is finally completely delegated to the unit test text test runner and for the cleanup Whittier down databases
afterwords we need to tear down the test environment this is pretty much the reversed order of what we did in the set of test environment so we need to bring back the original e-mail back on the original test and direct and finally we do leave the state and the mailbox master the results of the tests sued by combining the number of total thickness and Paris and this I should probably look familiar to you at this point
the recaps on the different have test classes and how they relate to each other so this is the
hierarchy and a simple test case is a very tall subclass by the transaction test case which is of course by the test case and loss of case finally specialized our version of licenses case which is static life a test case in St. as so many times simple
test case is very very fast because it doesn't interact with the database he can't can't query can't say come to any of those things it has access to the test client those so you can think of it as a slightly more advanced version of the usual Python unit test this case transaction tax case is not possible but it allows the these queries and transactions in this case it faster than the transaction test case but not as fast as simple test case it will allow you to do database queries but will restrict transactions myself as case out will act as a transaction test cases and it will launch a line http serving a separate friend from the from that's running the tests and finally the decided licensees the specialization that in addition to everything else will say also clouds kind is a very
handy utility when your testing agenda laps
and the freeing all actors involved in browser mimicking on the Request factory Klein handler and the client we talked about the request factory being the most straightforward and after it constructs requests and it encodes data client on the other hand performs more actions 1st of the client is stateful so it retains cookies enhanced sessions for the duration of the test client it also does a have few other things and to response so we talked about the instrumented test endurant I will declined is listening to those events um events like template rendered and request exception and that's why it is able to list all of the templates and template context that was used to render your response it also sets up a couple of other things on the response for example the original request of the client object Jason resolve match it can also handle redirects and build a redirect chain that we can expect the main goal of the Klein handler is to return a Django response with the whiskey request attached to it it load all the middleware that setting settings it will disable this user checks which can be enabled back if you want to any choice to emulate as close as possible server and browsing behavior and yes
we've talked about all the different tools that Django provides the testing but how do we achieve quality in our tests how to make how do we make sure that the test we right I reliable future growth and fast enough reasoning about quality is not easy besides our and intuitive human understanding of what quality is the are some tools that can help us
let's start with something simple introduce factory Apoidea test lots of people like it because in because it has this shortcut methods for creating models it generates random yet realistic data because it's based on a different library and they fast providers that based on certain rules create this random yet realistic data the important part that is that have every time you run the test you'll get different values compared to what you get with fixtures and there is a slim chance that factory boys might catch um about the if you are
interested in this kind of stuff might suggest looking at the Pythons hypothesis library is doing property-based testing and it takes the idea of random input to a whole new level so the idea of a property based testing came from a Haskell library called quick check and hypothesis is the Python implementation you can use and so the way you do it you use uh given decorated which can inject randomly generated data to get test you specify what kind of strategies do you want so in this case we're using a strategy that text which is very similar to you the factory Borys of providers and what it does was we decorate a function with the given and all functions that this test will run multiple times usually about 200 and it will test your function with all sorts of different AJ uh edge cases that usually uh that off to easily sleep away from and developers attention since gender is an established way of doing web development he in the Python world hypothesis comes with an extra module was support for Django and are similar to factory boy we can create models populated with random data and to be notated with the hypothesis we always have received the created model examples so this is a
real world example of how hypothesis can be used for testing Django apps if you look at the very top of the following books that we have is 1 important in word that is the most important and this is the 1st case we're using the hypothesis test case to be able to wrap each hypothesis testing to and transaction rather than running all 200 of them in 1 single transaction % will get and last so let's remove the importance and focus on the test so this is a slightly stripped down example that I've borrowed from a presentation and made and and Django local local user group in London made by the author of hypothesis states so we're testing what we're testing here we're testing that if the project model instance can respect the limit of uses a single project in how you can specify collaboration in the fight on this project that's what have free people in normal so what we're doing we're injecting 1 particular instance and a list of random amount of user models from 0 to 20 then we check if the project is at its collaborate limit and we try to add another user we expect an exception to be raised because we don't want to accept more users than we have in our collaboration and otherwise adding a new user should work just fine and when you run the testified if hypothesis encounters that the test didn't succeed with a set of randomly generated data it will tell you falsifying example so in this case we have a project with the limited part with a collaboration limit of 1 and we've been able to add to users where the 2nd 1 didn't trigger an exception which means there's something wrong you have a the we use the catch with all that that's a best cap when is simple and stupid and 1 should not have bugs in their tests with hypothesis tests can become more complex and you might even reach a point where you will need tests for your tests which is ridiculous so when using and but when used properly and timely property-based testing can be a very valuable tool uh to keep in mind as this good coverage is
probably the most known and widely used metric and it's about a week ago I checked gen goes test could coverage and ran against sequel identical 75 % we use the thing this coverage is generally accepted metric you have low could cut code-coverage that yes you definitely need more tests probably not testing your code property but if you have high code-coverage I think that you test of the good but maybe you just kind of group them in a way that he hit all of the lines of your code but he didn't from roughly test so unfortunately for him In my opinion Hiko coverage doesn't necessarily apply high quality of tests we can do better we can do better with mutation testing condition testing is a concept that was on introduced in the seventies and it involves changing the code of a program in some small way like little tweaks and observing what happens when we run the test on this modified version of code and this is a very powerful idea I so it's it's so simple and yet so brilliant In by the mutation testing is implemented by a package called modifying and modifiable operates on the Python HTTP implementations so how does it work you can buy a mutation uh modified from the command line and you need to specify a target and you use testing test obviously needs to test your target could so he you target source source code has an if statement and the statement has a logical Brent and to saying he food and Bob men do this my wife takes this logical operand and and inverts it into an old so now come get code becomes a slightly modified version of the original and we call it a mutant now what did we run the test on the mutant if the test fails on the Newton this is great we say that the mutant was killed by our test which adds . stall final mutations With the mutant survived notice his vision of it means that it doesn't really matter for using and all along the test fast anyway we must we're probably not testing what we should be testing and what I can do a lot more there's a fairly long list of mutants you can replace arithmetical operations Greek continues statements additional operands constants logical um the operators and again did you can even do the the whole conditional branches had decorators and do a lot more this is what a stripped-down example of uh a test run looks like you get mutation score which is a percentage in this case that's 2 point 1 and it also tells you the total amount of mutant that generated the number of meters that killed surviving content time that and I take item this tall and I started playing with the Django test and see what results I guess so unpleasant trend is the
correlation of the high coverage with politicians for for example Django duration details has mutation score 89 and its annotation score is 100 per % this looks really nice this doesn't always hold true for example Django encoding eUtils annotation score is 64 % while test code coverage is 100 per cent said you can see that mutations score can actually I give you better insights into what how good the tests are gentle tutorial is
excellent reviews don't need to search the internet for the media tutorials courses because you can go to the Chengdu official web page you get the GenGO official tutorial and whole section of it is dedicated to testing because testing in the jungle world is an integral part of developing out in my 2nd year at
university I discovered Django it's also the 1st time that I wrote a test was a very silly 1 just making a request to the URL expecting a 200 response in been know why why I wrote this test is because when I call the tutorial and I created my Django I also got this flight tests that by and that caught my attention this tutorial explaining exactly what I need to do to get started with testing my code so testing framework is a lawful tool to motivate people to write tests especially especially beginners DEC on testing for it contains a section named when testing more is that me it says that he has not you can if you like to project and spinning out of control Kunst AE in Greece so they can have so many tasks but you know just let it spin out of control it's fine let them grow when testing more is better is
attractive than when the number of tests growing your total test runtime becomes and so on and Django gives you all of the tools and encourages you to write very high levels of functional style test so the speed of the test can suffer and this has all the locations was
slow test you get a slow feedback loop the slow feedback loop you go a slow development cycle uh sometimes if class ourselves so people just stopped running them and that's
why 7 tips on how to make you test uh not be slower than they actually need to be and number falls short of a the the the the number 1 use fast and less secure
password hashing during test like and 5 this is where gender does for its so I tests abuse simple test days when possible if you don't need the database if you just writing simple test use simple test cases brilliant our test data you don't want to mutate any of the objects that you create in it because it's going to be equal just once and here's the shocking bullet point number 4 use MOX everywhere and I'm joking I'm not serious 2 strangers size that yes this is a day there's nothing he hurriedly bad with Marx in themselves marks are really good tools they need to know how to use but if you using marks for with the intention of speedups then this probably something wrong in your lab design and not in your tests marks a really good for isolation so use them for isolation the very careful what gets created in the set of methods method are doomed save model objects if it's not necessary and try to isolate your unit test from the rest of the taste them from the rest of your tests the and I would like to talk a bit more about the lost the points so last year I learned that this is
called the Thai food so it title like this where we create some model instances all red flags that need to be identified and removed or reduced a set of doing create how we did in the previous example where we can do we can create an in memory model just like that's just like this and similarly uh and in memory model can be created using F-actin built method or you can create a stub object which is just a couple of at tributes using the stock the last 2 are taking from the factory boy the cavity here is that you can't always do that for example when you have a many to many relationship you just have to save the model and finally isolate unit test from the rest of the test the test it might look like this it's a makes so unity unit like tests of functional integration testing and but it would have been a lot more useful and would allow creating a faster feedback loops if we separate unit test from the rest because you can run unit tests all the time we can even run them in and watch mode the really fast they can give you the good it is to give you the results instantly and then you can write on the rest of the test less often
1 way to do it is to use simple test case classes we test and tag them with a special label of if your gender 1 1 time you start with an older version you can separate them in a different folder so that you can run them in total isolation from the rest of the test even better you can make them simple unit test for pi test and which have no idea of what Django ears invite more of them but don't neglect functional and integration test because obviously they have 1 that validate the contracts between your units to you definitely should have both so your
friend runs a little bakery business and they sell freshly baked bread and there are other similar nipples behind you to help them build an internal out for them what the output
needs to do is to help in figuring out what should be the correct the T. value for their products and this will say sometimes they're count because of taxes and so we start with a basic 90 schema where we get the product model and put a question a model where the one-to-one relationship between them the product questionnaire is the key to all the team decision making it's a little add on where we define the yes-or-no questions and we build a decision tree to eventually landowner defined so as this products zero-rated Katie or is it a standard rate the T will need if you and will need a model for the view will display the questioner and answers will be saved on the product model for on the Approval question model for possible future the calculations in the same view will converge to a final decision and save it to the project with the known the value is going to be really easy to compute the final price of the product so I'll be showing
example code different approaches on how to solve this problem where at the very top we can have the production code and the bottom is going to be harder task yeah social number 1 can you once we know the form is valid we check if the product is a biscuit and is a coated in chocolate so in that case we going set of 18 to 20 so this is a costly view because of its properties if you have an allergy terwards cost is making just translated in your mind function based as for the task what we're doing we're creating a product object we are making the request bypass that passing some data and then we're refreshing the product from the database and we're checking if the VT equals to 20 in reality we understand
that the if else is going to be much bigger we can split into multiple methods but the essential part is that the entry point of this calculation is going to be in the and for each branch we can have 1 test in each test will do the same thing it will create a Praat object and will make the request and will check this heating value and lb lb we don't just repeating ourselves and we go we going to do it for i in this case it's a bread and flapjacks and other items so to test each 1 of those items and to calculate and to check the heating value we will also be testing the rout the router will be interacting with the database to eventually sent an input to receive an output if you're thinking that you can remove the database interactions with the locks I think Marx and not a solution in this case because we can do better solution
number 2 the conditional gets moved to a more appropriate place the so in the task where again creating a product uh model we're instantiating the form we're passing and some data checking in the form is valid going to save the form refresher products from the database and check if the p h equals 20 the to test it will
again to create a product object but we no longer but we're no longer testing the routing which is an improvement over the solution of 1 by form we send an but the answers to the questions to the output the 3rd and last solution
most the entire decision-making to an old boring I from class or a function if you want to it has a method called calculate pt that does exactly what it says and in the test for instantiating the calculator calling the method on a bunch of arguments and they're expecting the result to be 20 somehow we forget that Janco is a web framework not a programming language different constructs can live outside of core Django components at the TI calculator has no idea what Django is and it doesn't need to yeah
when testing we're not testing the relative when not testing the URL all we did was sending an input to receive an output from what we did is called the coupling
the company has various benefits reusability as a developer I can pull out the VAT calculate and build a separate out that calculates the tea and helps others making around the VAT values extensibility if my friend decides to expand their little business to any country and this place has different VAT values it's going to be much easier to obtain them out in 1 place in the VAT calculate rather than going for all of the views of forms and trying to update it there and finally to stability you saw how easy it was to test the VAT calculator this approach of
isolating business logic allows creating more and faster unit test and keep the functional integration tests at manageable light so tests have more than than than we think at test has the capability to drive better at design in terms of gender it's about moving complex logic away from any Django related components if it's a complex behavior that is indirectly tied to the database templates forms of use and build objects that performed those complex actions unit test them thank you very much for your undivided attention and happy testing everyone and the
higher here more food and 1