Building Data-Driven Web Apps with Flask and SQLAlchemy Transcripts
Chapter: Testing web apps
Lecture: Getting started with tests
Login or purchase this course to watch this video and the rest of the course contents.
0:00 Now, it's time to start writing some code and getting our tests in place. So, let's go and open our new project. I've copied this over
0:08 as you can see into Chapter 14 testing. We're going to work here. Now, probably the first thing that we want to test is this register thing.
0:18 We've already talked about that it's kind of tricky. I think it's a good example. We also have this register_get. Now that doesn't really do anything.
0:26 Theoretically, our view model could come up with like a list of countries and do a bunch of cool stuff to verify that
0:32 it's going to give you drop downs and what not. It doesn't do any of that stuff So we're not going to test it. We also have this index one
0:38 which doesn't really do anything, just verifies the user. Now, that would be great, and we can test that.
0:43 But I think the most instructive part will be to test this part of our application here. Now we have a couple of options.
0:50 Recall, we talked about testing the view model that would be this part talk to about testing the view method that would be
0:57 directly calling this function. And we've also talked about an integration test where maybe we fire up the web app
1:03 we issue a request in this URL with a post. And it will figure out what parameters have to be passed to the method and so on.
1:11 So we're going to start by doing the simple thing working with this RegisterViewModel. Now, let's get going by creating the section for our tests.
1:19 So, we're going to create a new folder for organizing our tests as we already talked about, over here. I'm going to put it next to our project.
1:29 And in here, again, we're going to do some organizations. So I'll have account_tests, plural. We're having more than one test.
1:36 Now we could test this with a built in unit test framework we could build it, test it with pytest we could test it with something like Nose
1:43 or you know, other ones. However, in the flask documentation all the examples are using pytest. So we're going to use pytest as well.
1:52 So, in order to use pytest, we have to install it and say that it's a requirement. And actually, there's a few things we might want to use.
1:58 So I'm going to come over here. And in our development one, this is only for dev it's not a production thing. I'm going to put 4 libraries, pytest
2:07 pytest-cov, webtest and clarity. And then we're going to go down here to the terminal and make sure that we pip install those.
2:16 All right, looks like I already have them, that's great. So, those are installed for our project and ready for us. When we're writing tests with pytest
2:25 what we've got to do is come over here and just create a function and has to start with the word test_ underscore and be a void method.
2:34 So, we can put whatever what. Let's put an example here like this and then we could print test test example, like so.
2:42 And then the way you test with pytest is you say assert and then you could assert something like one plus two equals three.
2:51 Now, how are we going to run it? So we come over here and we'll right click and we say run, says run pytest in there. Now sometimes it won't say that.
3:00 Sometimes it will. We run it, looks like it's working great. If it doesn't, you can come over here and say Edit
3:05 add Python tests, pytests, and then swipe the script path and then script, swipe that. Okay, so that's what you could do
3:13 but it looks like it already discovered it for us. So here you can see in our example. We printed out test example and just look at all the results.
3:20 It looks like it passed. And you should always verify that if it's not passing you're also detecting that.
3:26 So here you go. When I had it wrong, it says it looks like we expected three but we got four, not the same. Alright, so I'll put this back.
3:35 This is not really our test. This is just a super, super simple example to show you how to get things set up and running with pytest.
3:41 But I'll go and leave it here for since it's a course, I wouldn't leave it in a real lab.