#100DaysOfWeb in Python Transcripts
Chapter: Days 81-84: Unit testing web apps
Lecture: Testing challenges of web applications

Login or purchase this course to watch this video and the rest of the course contents.
0:00 You've seen that it's really important to test our web applications, but it also turns out that testing web applications is especially hard.
0:09 How's that for a double challenge for us, huh? The reason is our web applications are often interacting with other resources.
0:16 They're interacting with databases, other web services and they're interacting with the web framework itself
0:21 which typically would rather run in a web server instead of inside of some kind of unit test. Let's look at this code.
0:27 Remember our bill tracker application, this lets users they're our users, they can log in there's users that have bills, these bills can either
0:36 be paid, partially paid, or unpaid. And so what this view method that we're looking at here does, this is looking at
0:43 the details for a particular bill, and it lets the user enter payment towards that bill. Okay, so, if we look at it like this you might wonder
0:51 well why is this extra hard to test? Remember there's things that we might want to interact with like web services, or databases, or so on.
0:58 All of those make it hard, and actually working out the web framework itself, those web frameworks don't like to just be created and called
1:06 they've got to sort of be started up and all that kind of stuff. So, if you look carefully at this you'll see
1:10 there's a lot of things that make working on web apps hard. So, here we have this read logged in user cookie from a request object.
1:17 Okay well first of all, apparently you have to be able to work with cookies, and we have to be able to pass a proper web framework request.
1:25 But how do you get one of those? Normally that's automatically done, right the framework does that when a HTTP request comes to it.
1:30 Well not this time, not in your test you got to figure that out, 'kay? So, that's line one, line two we're passing this bill details view model thing
1:38 which we'll talk more about view models in a little bit it's a concept that we can use to simplify our code
1:43 and we'll cover it, but I know we haven't talked about it yet, we're going to cover it next but I want to talk about this concept first.
1:48 So we're going to pass this request off to this view model and it's probably going to do database access inside itself, as well.
1:54 Okay so that's a challenge, 'cause we don't want to talk to the database, we don't want to actually issue a real
1:58 HTTP request, we want to just run a unit test but then we're going to return a response, that's not so bad
2:03 but it is one of these web framework objects. Here we have our view model working with this post dictionary, that's the form that was submitted
2:10 okay well how do we do that in a test; we have our repository which talks to the database adding a payment, well how do we call that function
2:16 without actually interacting with payments 'cause we just want to call those details post function all this is going to happen, we can't control that.
2:23 And finally, we're going to return a redirect and we want to check those kinds of things. So you can see testing on the web is hard
2:28 'cause there's so many dependencies. Other web services, databases, files and of course the web framework itself.


Talk Python's Mastodon Michael Kennedy's Mastodon