Building Data-Driven Web Apps with Flask and SQLAlchemy Transcripts
Chapter: Testing web apps
Lecture: Special challenges of the web
Login or purchase this course to watch this video and the rest of the course contents.
0:00 And you'll see that there are some special challenges to testing web applications. They often have more dependencies
0:08 and other systems tangled up in their code than most. Here is the register_post method. This is the method that we roughly wrote
0:17 when we want to have a user come to the site and create an account. They're going to go to the form where there's a get version that shows in the form.
0:24 They're going to fill it out and they're going to hit post. This code is going to run. What if we want test this behavior? Or if we want to verify
0:32 that its user can register for our site if that account email address is not already taken and all the details are filled out.
0:39 Let's say if the email is not even filled in then we get the right error message and so on. We want to test this code.
0:46 What are some of the challenges here? Well, look carefully. Over here in RegisterViewModel remember it imports Flask inside and it goes flask.request.
0:57 So, implicitly it expects request to be there and to be setup and to have values and not be empty. Things like that. Wen we call vm.validate
1:06 we see it as actually going to request.form and we then get to pull out things like the full name and the email and so on.
1:12 We need that to be correctly populated. In this user service when we try to create the account it's probably going to the database
1:21 just like Validate also would when it wanted to verify that the account didn't already exist. This is going to go to the database
1:28 and maybe insert some data if that worked. It might also do some other kind of check as well here. Maybe it does other things
1:34 like maybe it registers the user at some other API like for example, adding the user to our mailing list or sending them an email.
1:43 We don't want that to happen. We don't want any of those things to happen. We want to test in isolation. We don't want to depend on the database.
1:50 We don't some random user to get an email every time we run this automated test. Things like that. We want to avoid those. Here, when we call login
1:59 this is probably working with the response object and calling set_cookie. Finally, at the end we're doing some kind of redirect with Flask.
2:06 What does that need set up to work? So, as you can see there's a relatively simple function has all sorts of tangled interconnections
2:15 with the web framework with the database and potentially other services like email and external APIs. How do we test this?
2:24 We'll see that Python and the test frameworks have everything that we need to make this work. But, it's going to take a little bit of special attention
2:31 and some cool techniques.