Building Data-Driven Web Apps with Pyramid 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
Let's focus on some of the special
0:02
challenges we might have in testing web projects.
0:05
Here is a POST view method that's going to handle
0:09
the postback, when we're doing registration.
0:11
So, this is how users actually register on our site.
0:14
And, you look at it, you can see there's things
0:15
we might want to test, this interaction with the ViewModel
0:19
this error handling, the actual creation of the account.
0:22
But, look carefully. You'll see there are certain areas
0:26
where this is much harder than
0:29
just some sort of algorithmic thing.
0:31
We're creating a register ViewModel
0:33
and, oh it takes a request.
0:35
That's an actual Pyramid request.
0:37
With various dictionaries like the Post, Get, matchdict
0:41
headers, URLs, all sorts of stuff.
0:45
Hmmm, how do we create one of those?
0:47
That could be tricky. Validate.
0:49
Remember the register ViewModel actually goes back
0:51
to the database to make sure the user that's trying
0:53
to register isn't already registered.
0:56
How do we talk to the database?
0:57
We don't actually want to go to the database in our test.
1:00
What we want to do is run these tests in isolation.
1:03
So, how do we make that still do something meaningful
1:05
without actually going to the database.
1:07
That's really tricky. We're going to create an account.
1:10
Well that's definitely going to go to the database, right.
1:12
Maybe it's also going to send like a welcome email
1:15
who knows what that user service thing does.
1:17
It might do all kinds of things we don't want to happen.
1:20
So, we don't want an email to get sent to a random person
1:23
every time these automatic tests run, right.
1:26
We want to make sure that doesn't happen
1:27
but we might want to verify that the email would
1:30
have been sent correctly.
1:32
Similarly login, what does that mean?
1:34
This redirect that probably throws an exception
1:36
but in a positive way, right, throws a HTTPFound exception
1:40
for /account. So, all of these things are special challenges
1:44
of working with the web when we're doing testing.