The Challenge5:45 with Kenneth Love
Let's talk about the what, the why, and the how of creating this Tacocat project. We'll talk about running the tests and how to figure out what the tests expect of you.
TestCase- A group of tests about a certain area of the project
test_- A test method that actually tests a part of the code
You run the tests with
python app_tests.py. Anything with a
. is good, anything with
F needs more work.
Hey there. 0:00 So, you just finished a course about building an entire app, a Flask, 0:01 and a database. 0:04 Congratulations. 0:05 But before we end this course for real, 0:07 I want to give you one more challenge, and I'll warn you now, it's a bit of a doozy. 0:09 Attached to this video is a new workspace. 0:14 You should go ahead and launch it, because it's going to be really important for 0:16 solving the following code challenge. 0:19 In the workspace is a directory of Python tests. 0:21 I want you to create a Flask web app, 0:23 in the workspace that passes all of the tests that I've included. 0:25 Now, I know you don't know how to run the tests yet, 0:29 but we'll cover that in a minute. 0:31 First, let's talk about what this app needs to do. 0:33 The app needs a user model with an e-mail address and a password. 0:35 The password should be hashed with bcrypt. 0:39 The app should have views for signing up, logging in, and logging out. 0:42 The app should also have a model for a taco. 0:46 The taco should have a user, a protein choice, a cheese choice, a shell choice, 0:49 and a free-form text field for further options. 0:54 And finally, users should be able to browse other users' recent taco creations 0:58 on the homepage. 1:02 I put more details into a readme file in the workspace, 1:03 to spell out all of the nitty-gritty details. 1:06 And, the tests will tell you when something isn't working. 1:08 I want you to know up front that this is going to be a lot of work. 1:11 We're doing it in workspaces so 1:14 you can save your progress over multiple tries or days. 1:15 You don't have to complete this step. 1:19 You've still learned so much about Flask. 1:21 But if you make it through, you'll know for 1:23 a fact that you can start building whatever apps you dream up. 1:25 Learning to build things from a spec, 1:28 like my description a bit ago in the readme file, and 1:30 learning to write code against tests, is a giant part of being a real developer. 1:32 On that note, let's go see how to run the tests right now. 1:37 All right, so 1:40 let's talk about what all is involved in this, cuz this is, this is different. 1:40 We haven't done this before. 1:45 So in your workspace you're gonna see there's a readme file, and 1:47 this contains the brief like, what you would get from a client or 1:50 a project manager or you know, somebody in that kind of role. 1:53 Somebody who tells you what it is that you're gonna build. 1:58 Or, maybe, this is something you wrote up yourself, 2:00 because you're building a project for yourself. 2:02 So the brief is more conversational, kind of, just explaining some of the stuff. 2:05 At the bottom, there are bullet points that outline, basically, 2:10 the, it must do this, must do this, must do this, kind of thing. 2:14 Beyond that, though, there is a app_tests.py file with all the tests. 2:18 First of all, though, let's talk about some stuff. 2:24 There's forms.py, models.py, and tacocat.py. 2:27 Tacocat is, of course, the actual application. 2:30 Now, these are all empty. 2:33 There's nothing in here. 2:35 You've gotta fill that in yourself. 2:36 But, I did provide you with HTML, and inside static, there is css and 2:39 images that look good. 2:44 It'll end up producing something like this. 2:47 So let's talk about these tests for a minute. 2:51 If we look at the tests, here in app.tests.py, 2:52 there are quite a, quite a bit of code in here. 2:57 And if you haven't ever seen test cases written for Python, or period, 3:00 these are going to look fairly foreign and and, not necessarily unreadable, 3:05 but a little, a little different, a little weird from what you're used to. 3:09 So, for example, let's look at the first one. 3:13 So we have test cases, and each test case deals with a certain area. 3:15 And then inside each test case, 3:20 any function that starts with test is a test for a certain thing. 3:21 So in this case, we're testing the creation of a user. 3:26 So we call this create_users, which tries to create two users. 3:29 And you can see that when we call create_user, 3:32 we're A, we're expecting there to be a method called create_user. 3:35 B, we're expecting the user to have an email and a password. 3:40 This create_user method tells me that we're going to be doing hashing, and 3:45 we have to do hashing. 3:49 And if we look at the test, this test_create_user, 3:50 we can see here that we've created the users. 3:53 We make sure that it made two users. 3:56 And we make sure that the first user or 3:58 any of the users, the password does not actually equal the string password. 4:01 So that confirms to me that we have to hash the user's password. 4:06 So that's probably the first thing to try and do. 4:09 Inside models.py, you wanna make a user. 4:13 You wanna give that user an email and a password. 4:15 And you want to provide a create_user method that will create users, 4:19 and that hashes their passwords, probably using bcrypt. 4:25 We're not testing to make sure that it's bcrypt, 4:29 but that's I would assume you'd want to use. 4:31 So what does it look like when you have all the tests passing? 4:33 Cuz right now, if I try to pass a test by running python app_tests.py. 4:37 I immediately get a failure. 4:43 I did not pass that test. 4:45 But if I come over here to one that I prepared ahead of time, 4:48 much like the magic of cooking on television, 4:52 then I see a bunch of dots, there will be 11 of these, I do believe. 4:57 Takes about ten seconds to run. 5:02 And I ran all 11 tests and they were all okay. 5:04 So all of my tests are good, all my tests pass. 5:07 Yours, not even one runs yet. 5:11 So, make each of them pass, run apptest.py every time you, you write some code. 5:14 And soon, you'll have them all passing, and then you're all done. 5:21 So, once all of your tests pass, you can move on to the next step. 5:24 Which is a code challenge, that's expecting the app that you just created. 5:27 You can copy and paste the contents of the files from the workspace 5:31 into the files in the code challenge, and you should pass, right away. 5:34 I know that this is a brand new way of doing a code challenge, and 5:38 a much bigger code challenge than you normally do. 5:41 But I'm sure you'll be able to make it work. 5:43 Good luck. 5:45
You need to sign up for Treehouse in order to download course files.Sign up