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
0:02
to testing web applications.
0:05
They often have more dependencies
0:07
and other systems tangled up in their code than most.
0:11
Here is the register_post method.
0:14
This is the method that we roughly wrote
0:16
when we want to have a user come to the site
0:19
and create an account.
0:20
They're going to go to the form
0:22
where there's a get version that shows in the form.
0:23
They're going to fill it out
0:24
and they're going to hit post.
0:26
This code is going to run.
0:27
What if we want test this behavior?
0:30
Or if we want to verify
0:31
that its user can register for our site
0:34
if that account email address is not already taken
0:36
and all the details are filled out.
0:38
Let's say if the email is not even filled in
0:41
then we get the right error message and so on.
0:43
We want to test this code.
0:45
What are some of the challenges here?
0:47
Well, look carefully.
0:49
Over here in RegisterViewModel
0:51
remember it imports Flask inside
0:54
and it goes flask.request.
0:56
So, implicitly it expects request to be there
0:59
and to be setup and to have values
1:01
and not be empty. Things like that.
1:03
Wen we call vm.validate
1:05
we see it as actually going to request.form
1:07
and we then get to pull out things
1:09
like the full name and the email and so on.
1:11
We need that to be correctly populated.
1:14
In this user service
1:16
when we try to create the account
1:18
it's probably going to the database
1:20
just like Validate also would
1:22
when it wanted to verify that the account
1:24
didn't already exist. This is going to go to the database
1:27
and maybe insert some data if that worked.
1:29
It might also do some other kind of check as well here.
1:32
Maybe it does other things
1:33
like maybe it registers the user
1:36
at some other API
1:37
like for example, adding the user to our mailing list
1:40
or sending them an email.
1:42
We don't want that to happen.
1:43
We don't want any of those things to happen.
1:45
We want to test in isolation.
1:47
We don't want to depend on the database.
1:49
We don't some random user to get an email
1:52
every time we run this automated test.
1:54
Things like that.
1:55
We want to avoid those.
1:57
Here, when we call login
1:58
this is probably working with the response object
2:00
and calling set_cookie.
2:02
Finally, at the end
2:03
we're doing some kind of redirect with Flask.
2:05
What does that need set up to work?
2:07
So, as you can see
2:08
there's a relatively simple function
2:11
has all sorts of tangled interconnections
2:14
with the web framework
2:16
with the database
2:17
and potentially other services
2:18
like email and external APIs.
2:21
How do we test this?
2:23
We'll see that Python and the test frameworks
2:25
have everything that we need to make this work.
2:28
But, it's going to take a little bit of special attention
2:30
and some cool techniques.