Integration Testing For Flask Applications - Python API Testing

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what is going on guys welcome back in this video today we're going to learn how to do professional integration testing for flask applications in Python so let us get right into it [Music] all right so we're going to learn how to do professional integration testing for flask applications in Python today but before we get to the code I would like to talk a little bit about the idea of integration testing and how it differs from the other types of testing that there are and for this I found a graphic from a Blog Post online this is the graphic and to be safe here's also the source catalon.com this is the blog post where I got this graphic from and this graphic illustrates quite well the idea of unit testing integration testing and end-to-end testing in this pyramid here the higher you go into pyramid the more integration you have the more comprehensive tests you have the slower the tests are and the lower you go the more isolated simple and faster they are so unit tests I have videos on unit tests on this channel already unit testing means testing individual units one function one unit is tested we see whether this one particular unit works correctly but in applications you have multiple units working together you have one function the second function a third function function and they call each other they are executed in a certain order and they don't work independently they have to to work together and this is what you test with integration tests here with API tests you test how well the individual units work together and then you have uh one level above that how the UI also works with the back end so you might have a backend you have individual units individual endpoints and functions and then you have tests on how they work together but you then also want to test okay how does the front end work with the back end so how are the requests sent from the front end to the back end then the back end produces a result in how well is this result displayed in the front end is everything the way it should be and then on top you have of course manual testing which is uh you just testing your application as a user which is the most high level way of testing today we're going to talk about this integration test layer here so we're going to take individual units and we're going to test them as as a whole thing as so so how well they work together and for this we're going to build a flask application a simple to-do list application on this channel I have a video where I show you how to implement a full to-do list application with checking and with deleting and creating and uh and inspecting to do's and stuff like that today we're going to have a very very simple version of a to-do list application and of course you can also skip that part because you don't really need to watch the process of the development of the application if you're only interested in the integration testing so you can hopefully find timestamps down below that allow you to skip the flask application development part and you can jump directly into the testing we're going to keep it simple so I'm going to just create here an app py file and by the way of course we need to install some packages if you don't have them installed yet pip 3 install flask obviously and we're also going to use pandas so what we're going to basically do here is we're going to build an application that allows you to add to Do's to remove to do's and to download the to-do list as an EXO file and the idea of the integration test is now to not just test whether the add to do function Works whether the remove function works or whether the download function works it's to test the process of adding to Do's removing to do's and then downloading the file and seeing if the content of the Excel file is what we're expecting so a unit test would be testing just the add to do function what happens when I call the attitude function with these parameters is there a new to do Works what happens when I do something that's not loud fails okay perfect and the integration test is what happens if I first add to-do's then remove to Do's then download the list does everything work correctly that's the idea of an integration test so going to start here by saying from flask import flask with a capital f render template request url4 and send from directory and we're going to also import pandas as PD then we're going to say the app is a flask application with underscore underscore name underscore underscore the template folder is going to be templates and then we're going to say to do's is an empty list and we're going to start with a simple index endpoint so app dot route is going to have just a slash the root endpoint here and it's going to have the index function which is just going to return a render template call to the index HTML file that we're going to code here in a second and we're going to pass to do here so that we can display the to-do's in the HTML file and the HTML HTML file will be here in a directory called templates in here we're going to have the index HTML file we're going to change this here in a second but that is basically the idea here then we're going to have an endpoint or a route called slash at and this is going to be a route where we can send post requests to so methods equals post the function here is going to be at and what we're going to do is we're going to say to do equals request not route sorry request dot form we're going to get from the form that we're going to have in our index.html file we're going to get the to do fields and we're going to say to Do's append to do that's basically what we want to do and then we're going to return a redirect to URL for index and I think we need to also import redirect here so that's the add function and then we also have the remove function uh let's just copy the structure here so that I don't have to retype everything we're going to say remove the function is going to be called remove we're not going to use post we're also not going to use delete because I don't want to use forms I'm just going to have a link it's going to send a get request of course if we're doing this properly in an actual application you should use delete the method to delete to delete and post to create and get to see or to to get essentially and put to update but we're just going to use get for delete now because we're senders so we're gonna just say here Dell to do's and actually this is going to take a parameter it's going to take an index parameter which is an integer and this index will just specify which to do we want to delete and actually we're going to have to do this from 1 to n and not from 0 to n so we're going to say index minus 1. and we're going to redirect to index as well and then the last endpoint that we're going to have is going to be the Excel download so we're going to call this just download to Do's so slash download produce uh it's going to be a get endpoint we're going to have a function download and we're going to say the data frame is going to be equal to a pandas data frame and it's going to be based on a dictionary it's going to have the fields to do underscore ID and the to do ID is going to just be a list of range of length off to do's then we're going to say to deuce or to do is going to be introduce then we're going to take that we're going to turn it we're going to export it to an Excel file which is going to be just to Dot xlsx and then what we're going to do is we're going to send that so we're going to send this to the user so the user can download this so we're going to say send from directory then we're going to pass a point here current directory and we're going to say to do start at xlsx and then finally here if underscore underscore name underscore underscore equals underscore underscore main underscore underscore it and then we're going to say app run debug equals true all right so that is our flask backend we're also going to have an index.html file we're going to say here to Do's as a title and we're going to keep it simple we're going to have an ordered list so that we have uh the numbers so that we know which one uh we're actually looking at and why is this not automatically formatting correctly so we're going to have here a simple Ginger template we're going to say percent percent in between we're going to say 4 to do introduce and since we pass to Deuce here with render template since we do that we can easily just uh we can easily just access to Do's here we're going to also have a percent and for and in between what we're going to do is we're going to have individual list items and the list items are going to basically just display the to do so two curly brackets to do and we're going to have a link afterwards with href being equal to and then two curly brackets URL underscore four and remove is going to be the endpoint and what we're passing here index is going to be the loop iteration that we're currently at so Loop dot index yeah so that's the idea and the text of this will be removed now let me just see if this makes sense with the index yeah it should work okay so we're going to have the loop iterations the loop index will be passed as an index for remove here and uh then we're going to subtract one to actually remove it from the list or to remove the correct to do from the list I hope this makes sense I'm going to test it here in a second um and that's basically the only thing that we need now is we need a little form to create a new to do so for method equals obviously post and the action is going to be two curly brackets the URL for we're gonna say uh what was it at and then all we want to have here is a simple input of type text which is going to have the name to do and then we also need a button so input type submit which will have the value add to do and now really the last thing that we need here is a href and then URL for two curly brackets then parentheses There You Go download to actually download the to-do list all right so that should be it uh quite simple I can now run this and if I open this up in my browser we should be able to use this application so a b c d now let's see if I can remove them correctly there you go okay again a be C D and I can now download this I can put it on the desktop and you will see we have to do ID and to do so this works I now did the high level testing here so the highest level in the diagram which is where is it this one the manual testing but now we're going to have an automated integration test this is what we're going to build now here so I'm going to create a new python file and I'm going to call this flask integration test and here now we're going to import a couple of things we're going to import IO we're going to import unit test we're going to import pandas as PD and we're going to import from app which is our app.py file here from App we're going to import the app itself and now we're going to create a class flask integration test case this is going to extend from unit test DOT test case even though we're doing an integration test we're going to use unit test.test case for The Inheritance here we're going to define the setup function so setup self and here we're going to set the config so we're going to say app and then actually app.config and we're going to set the testing field so testing like this we're going to set this to true so that the application knows it's in testing mode and we're going to say self.client is going to be equal to app.test client so there's this function this method test client that returns a test client that we can use to send requests to the application so this is the setup and then we will Define just a simple test case now you can Define multiple test case for this video we're going to just Define one simple test case and this is going to be the test underscore at underscore remove underscore to do's and download Excel and usually test names are very long because they explain everything you're doing ADD test remove test or actually add to do remove to do download Excel see if they're correct whatever so should fail should not fail whatever so that is our test name here and we're going to say 4i in range let's go from one to six we're going to now say self.client this is our testing client that we get here we're going to send a post request first to the endpoint slash at and then we're going to say data equals and this is going to be the post data that we send to the at endpoint and all we're going to do here is we're going to send to do is going to be a formatted string to do and then the index of the iteration so just to do one to do two three four five that is the first part of the test so this is one unit if we just test this if we just test if this post request produces new to-do's that would be a unit test of the add endpoint now we're going to also say self dot client get again this should usually be delete but we're going to say get remove two and then we're going to say response equals self.client dot get I'm going to send a get request to slash download to do's and we're going to assert now so self-assert equal first of all that the response status code is equal to 200. that the content type so the response dot content type not content range content type is equal to and now I have to type this for my second screen because it's quite long application let me just make sure I'm not blocking this with my camera application slash vnd dot open XML formats Dash Office document dot spreadsheet dot Chic so we just want to make sure that this is an Excel file this is what this means uh so that's the assertion here and afterwards with and now we want to see the content of that with IO bytes IO response.data this is the Excel file itself as buffer what we want to do now is want to say data frame equals PD read Excel from the buffer so from the file and then we want to assert the following assert list equal and we want to see that the list to do one to do three to do for to do five this is what we expect to be part of the data frame column to do that that is actually the same thing as data frame to do dot values dot to list want to know that this is actually the same so this is the integration test let's run it and see what happens and in this case it fails why why does it fail because I mistyped the spreadsheet yeah it should be spreadsheet ml sorry now it still fails why does it fail because uh oh because I have a space between the to-do's but you can see already why these tests are useful because they show me that what I'm expecting is not what I'm getting now it should work there you go test past and uh everything worked fine resource warning okay that's not too important but our test case uh succeeded we can also run the full class here if we have multiple tests and you can see everything went correctly which means that this test uh worked the way we expected it to work and this is very useful because as you can see we're testing three components and how they work together first I add to Do's then I remove to Do's then I download these to Do's as Excel files and in the end everything is the way I want it to be and if I now change something because I want to extend a feature I want to change something about this application I can just rerun these tests if I have multiple of those all the time to see if something that worked before now doesn't work anymore so now I can see okay all of this works as expected now I made some changes in the code base and if two tests fail all of a sudden I know that I messed up something and I can immediately see that without having to go through the application and test every single process myself again so this is the value of integration tests and they differ from unit tests because your test multiple components multiple units at the same time so that's it for today's video I hope you enjoyed it and hope you learned something if so let me know by hitting a like button and leaving a comment in the comment section down below and of course don't forget to subscribe to this Channel and hit the notification Bell to not miss a single future video for free other than that thank you much for watching see you next video and bye
Info
Channel: NeuralNine
Views: 6,589
Rating: undefined out of 5
Keywords: flask testing, flask python, python flask, web application, web development, flask integration test, integration test, integration testing, flask integration testing, python integration test, flask integration tests, python testing, flask tests, api test, python api test, flask api test, flask api testing, python api testing, unit tests, unit testing, python, flask
Id: Y5CA79rHEYQ
Channel Id: undefined
Length: 19min 50sec (1190 seconds)
Published: Mon Sep 11 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.