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
0:01 into three rough categories
0:03 three types of tests that we're going to have
0:06 when we're testing our web application.
0:08 Now recall I did mention this is not a general course
0:11 or chapter on unit testing
0:13 it's specifically focused on testing the web applications.
0:17 So there's probably other types of tests
0:19 but in our world of testing our web app
0:22 these are the three that we're going to focus on
0:24 and I'll touch on a few more just really quickly.
0:27 So at the lowest, simplest level
0:30 let's start there
0:31 we probably want to test our view models.
0:33 Remember view models are central
0:34 to the data exchange with the user
0:37 with the template, with the view
0:38 and a lot of the validation that we have
0:41 the data validation that's in there
0:43 resides hopefully in these view models.
0:46 So what makes testing them special?
0:48 Well remember internally very often
0:51 they implicitly use a request object.
0:54 So somehow we have to provide this view model
0:57 a fake request or a pre-populated request
1:00 with the data that we need it to work with.
1:03 So this is pretty straightforward.
1:05 We'll also see that sometimes
1:06 these view models talk to the database
1:08 that might be a problem.
1:10 Typically we want to test in isolation
1:12 control very carefully what the database would have said
1:15 and then make sure that the view model responds accordingly.
1:18 So we're going to address that as well in these tests.
1:20 Moving up in a richer, testing-more-at-once side of things
1:24 we might want to test the view method.
1:27 This is the actual thing that has the app.route decorator
1:30 or the blueprint.route decorator
1:32 we'd like to call that as if it were a web request
1:34 not through the whole system
1:36 but we'll just call that function.
1:37 It is a function, we can call it.
1:39 But remember this also works with requests
1:43 maybe even with response
1:44 it might also talk to the database.
1:46 It has a bunch of stuff going on working with Flask
1:49 so we're going to need to carefully spec that out
1:52 or provide it something that will work
1:55 in the way that it expects.
1:57 We also might want to create the entire web application
2:01 let the register blueprint, the database init
2:04 all that kind of stuff get up and running
2:07 and then we're going to feed
2:08 to the overall application a particular URL
2:11 make sure that it finds its way to the right view method
2:13 and that it does the right thing.
2:15 So we'll see that with the testing infrastructure from Flask
2:18 we can actually create a fake request
2:21 as if we were actually running the web application
2:23 we're not going to do it
2:25 not really firing up a server
2:27 going to create a test version
2:28 but then we can simulate
2:30 a browser sending requests to it
2:32 and make sure we're getting the right behavior there as well.
2:35 So these are the three core types of tests
2:37 that we're going to focus on in this chapter.
2:39 On the left in terms of even simpler
2:41 we might have algorithmic tests
2:43 and tests working with other parts of our application
2:46 that are not particular to the web framework.
2:48 And on the right we might even go farther
2:50 and actually deploy our web application
2:52 to say a test server
2:54 QA server, something like this
2:56 and then use Selenium to make
2:57 legitimate requests against it
2:59 and even run the JavaScript
3:01 and interact with it that way.
3:02 So there's lots more we could do
3:03 but these are the three that we're going to focus on
3:05 in the unit test world around web applications.