Building Data-Driven Web Apps with Flask and SQLAlchemy Transcripts
Chapter: Testing web apps
Lecture: 3 types of web unit tests
Login or purchase this course to watch this video and the rest of the course contents.
0:00 Now we're going to group our tests into three rough categories three types of tests that we're going to have when we're testing our web application.
0:09 Now recall I did mention this is not a general course or chapter on unit testing it's specifically focused on testing the web applications.
0:18 So there's probably other types of tests but in our world of testing our web app these are the three that we're going to focus on
0:25 and I'll touch on a few more just really quickly. So at the lowest, simplest level let's start there we probably want to test our view models.
0:34 Remember view models are central to the data exchange with the user with the template, with the view and a lot of the validation that we have
0:42 the data validation that's in there resides hopefully in these view models. So what makes testing them special? Well remember internally very often
0:52 they implicitly use a request object. So somehow we have to provide this view model a fake request or a pre-populated request
1:01 with the data that we need it to work with. So this is pretty straightforward. We'll also see that sometimes these view models talk to the database
1:09 that might be a problem. Typically we want to test in isolation control very carefully what the database would have said
1:16 and then make sure that the view model responds accordingly. So we're going to address that as well in these tests.
1:21 Moving up in a richer, testing-more-at-once side of things we might want to test the view method.
1:28 This is the actual thing that has the app.route decorator or the blueprint.route decorator we'd like to call that as if it were a web request
1:35 not through the whole system but we'll just call that function. It is a function, we can call it. But remember this also works with requests
1:44 maybe even with response it might also talk to the database. It has a bunch of stuff going on working with Flask
1:50 so we're going to need to carefully spec that out or provide it something that will work in the way that it expects.
1:58 We also might want to create the entire web application let the register blueprint, the database init all that kind of stuff get up and running
2:08 and then we're going to feed to the overall application a particular URL make sure that it finds its way to the right view method
2:14 and that it does the right thing. So we'll see that with the testing infrastructure from Flask we can actually create a fake request
2:22 as if we were actually running the web application we're not going to do it not really firing up a server going to create a test version
2:29 but then we can simulate a browser sending requests to it and make sure we're getting the right behavior there as well.
2:36 So these are the three core types of tests that we're going to focus on in this chapter. On the left in terms of even simpler
2:42 we might have algorithmic tests and tests working with other parts of our application that are not particular to the web framework.
2:49 And on the right we might even go farther and actually deploy our web application to say a test server QA server, something like this
3:03 So there's lots more we could do but these are the three that we're going to focus on in the unit test world around web applications.