Django Testing Tutorial - Testing Views #3

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back to the Django testing series today we are going to be testing our views if we look at the views top I file you can see that we have one view called product underscore list which is frankly a list view then we have a project detail view which again can be accessed where they get a post all who scroll down a delete request so that's what we are going to be testing and of course also this great view that's going to be important as well in the test Alice caboose top I file I'll be creating a class called tests views which supplies from test case I think that creating one test views class is going to be enough in this case because we don't have that many tests we need to cover abuse and of course we also need to import test case from Django tests import test case and while we're at it we can also import the test client that we are going to be using to mimic how a client would access abuse the Django test client isn't the only way of testing views you can also for example use the request factory object and that we are going to do another episode about entirely so yeah just keep that in mind it's not the only way to go about testing views it's just a way and the request factory is going to be covered in a later episode let's make some more imports from Django top URLs let's get the reverse function then from budgets top models we want the project's the category and the expense model and then we also want to import JSON the first method we are going to create is called tests underscore project lists get which takes in itself we can create a clients instance as you can see to test particular view we only need to send a get request so let's set the response equal to clients don't get and then the reverse of list the way I like to structure the code is that I first have set up codes so that's my setup code that have all the test code and then after that all the assertions so in this case we want to assert yourself top assert equals let the response stop status code is 200 so that we were able to access this list flawlessly and then let's also use the self-taught asserts templates use method which as the name says it asserts that a certain response contains in this case T budget slash project - list dot HTML template let's run this using manage to PI tests budget and it was successful as you can see and one rule of thumb is that you never want to trust a running tests so make sure to somehow mess it up in this case I'm going to assert that the temple name is budget slash product list or HTML s whatever just trying to mess the tests up and we'll get an actual error and if we try to change it back then this result again so this tells us that the tests actually working and not that it was somehow skipped or something like that I'm going to be creating a class called setup which takes in itself this function is going to be run before every single test method so we can use it to SNM says set up a certain scenario let's add self-talk clients equal to a client instance and now we can remove it from this function and instead of saying client ok let's say self talk client okay so we can access the client we created from the setup method then has also set self dot lists underscore URL equal to reverse of list and in here we can say self dot lists underscore URL and we're going to be adding more to the setup method as we start adding our tests ok let's get to the next one called tests underscore project detail gets we can basically copy this code over paste it right underneath inside of our new tests product detail get method and change this to detail URL as you would guess we would need to set self dot detail underscore URL equal to traverse earth detail however look out that when reversion detail we would need to pass in a slug so let's do that via ox and set X equal two projects one if we wouldn't do that and run our tests you would see that there's no reverse for detail with no arguments found and that's why we need to pass in a slug for this to be able to resolve to some URL and the arrow you can now see if we go back to our test is that we assert that the response code is 200 but of course we try to navigate to a detail URL which doesn't exist because the product with the slug of project 1 doesn't exist because we're going to be using the product one instance in other tests as well let's just instantiate it in the set up class project the object store creates what set the name to project 1 and the name if we go back to our modest or PI file it's going to be used to generate the slug from so we can see in safe method we said self-taught slug equal to slug of I of self dot name and therefore if we set the name to protect one the slack is also going to be project 1 in this case that's why this is going to work just fine let's set the budget to 10,000 doesn't really matter and we can save this object under self dot product 1 if we now rerun our tests ok of course they're going to see this error because the temple that we used isn't quite the same as up here we're going to need to change that to project - detail door HTML and then we are just fine but again remember that this was only one possible route inside of our views in this case we checked for the get method but now we also need to of course validate the posts and the delete methods and what's going to be happening as soon as we request them let's start out again with a brand new function that's quite important because you really want to be testing in isolation to make sure that you can pin down the bug as precisely as possible it doesn't do us too much good if we only have one test method which we try to bog everything in and then it ends up not helping too much with actually chugging down bugs so let's create a new one called test underscore project detail post adds new expense and for us to be able to add a new expense if you can remember from how the application works we actually need to assign it a category so we need one category for every expense therefore we're going to create a category while we set the project to self top project one just what we instantiated in the setup method and I set the name to development now let's set the response equal to self told clients to post this time the URL is going to be self though detail underscore URL and let's set the title to Spence one the amount to 1000 and the category to development so as soon as we send the requests we want to make sure that of course a new expenses edit but before that let's see what happens to the response we want to assert if we go back to the view stop I file you can see that we return a redirected project so a real direct is a 302 response so we want to assert that the response total status codes is 302 and now we want to cert that the expendables edits to our product therefore we are going to call self to assert equals self dot product one and then we're going called or expand sis and dope first so the first expense that belongs to this particular project should have to title earth expense one just what we specified over here let's run a test and see what happens and now let's try to mess it up by saying that the title should equal response to or expense to em we see in assertion error telling us what went wrong which of course we expected so that's working great now this however is the scenario when everything works out what I want to do however is also add one which checks what happens if we have no data it says it with our post request so test project detail posts no data and in this case we want to make sure that no expense was added let's copy everything from response to the session over and now we are going to remove the entire data that we passed the post request instead of a certain that the expense was added successfully let's surf to assert equals that self top product 1 don't expenses the count is 0 and as you can see it ran through because in that case if we post without any data we want to make sure that no expense was added let's move on to our delete method and we want to test project detail delete deletes expense to set this up we need a category again and of course we also need to create an expense that we can delete in the first place expense that object store create with the project self top product 1d title equals expense one that's at the amount to 1,000 and the category we need to set equal to this category that we created category 1 and a video category one equal to this new category okay again that our set up work let's set now response equal to surf dog clients so elite self-taught detail URL remember we are still accessing the detail URL just under the new method and then pass in JSON dog dumps with a dictionary we want to dump in this dictionary with the ID of 1 which is the ID of our expense that we have created in that case we want to now assert that the response status code is 2 or 4 as specified here so self to assert equals response response to status codes 2 or 4 and then self to assert equals self dot product 1 the expenses the count is now 0 because if we have one to start off and then delete it then we shouldn't have any expenses anymore and this rent successfully as well awesome let's try to change this up and it says zero isn't one so we can make sure that this test is actually running and that is good and similar to what we did before I also want to copy this over and say test product detail delete no ID so what happens if we don't pass an ID in that case we want to remove this bit right here and insert that D and we want to make sure that the status code is 404 and that we still have one expense and what this is going to do is expose a little error inside of a coat and you are going to see that testing is very good at doing that so let's run it and we get some sort of JSON decode error so let's wrap this in a try except block you want to try all of that and except we want to return an HTTP response for these status is equal to 404 let's run it again and see what happens and it now run through correctly you can see writing this test on top of the one that runs through correctly expose an error that we had an occurred which is of course good for us I probably read it a bit differently today but whatever last but not least let's test our test project create post view and in that case we can get the URL inside of this function directly because we are only going to use it once so that's going to be fine reverse of AD then I set the response equal to self door clients door posts under this URL we can set the name to project two because remember that we create project one over here already and this method runs before every other one so creating another project with the name of project one would result in slack collision which we don't want and therefore let's now set the budgets to 10000 again doesn't matter and the category string is going to be designed come up development now let's set projects to so the project that we just created equal to project two objects Topcats with the ID of 2 and then we want to assert that project 2 dot name is equal to product - just like specified over here of course what this did is also created two projects categories and therefore we also need to test that they were created say it first on the scope category equal to category the objects dog gets and he eats put one and then we can assert that the first category dog projects is equal to product 2 and also that it's name is equal to design just like specified in a category string that's the first category that we want to create then let's copy this over and insert the same thing for the second category this time just put in here development and of course this should be the idea of two now let's run it this over here looks a bit clunky but I think it's just fine for this tutorial you can split it up if you want to oh whatever but in this case I'm going to keep it together because it pretty much tests the product creation with the categories for this one product so I think it's not too bad anyway I hope you like this episode the code we produced isn't perfect by any means I'm sure you can find a lot of ways to improve it but it's going to be just fine for this purpose in the next part we will jump into testing models and until then make sure to leave a like if you enjoyed check out the Jang the tip card if you haven't already and I'll see you inside of the next one Cheers
Info
Channel: The Dumbfounds
Views: 77,650
Rating: undefined out of 5
Keywords: django testing, django testing tutorial, python testing tutorial, python testing automation, django testing unittest, python testing unittest, django testing views, django views, django views testing
Id: hA_VxnxCHbo
Channel Id: undefined
Length: 16min 27sec (987 seconds)
Published: Mon Dec 17 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.