Model Tests4:16 with Kenneth Love
Data is usually one of the most heavily used parts of an application so making sure your models work correctly is super important.
assertLess(a, b) checks that
a is less than
django.utils.timezone is Django's timezone utility that takes the
TIME_ZONE setting into account.
python manage.py test runs all of the tests for your apps.
[SOUND] A good habit to get into is that you don't give code to anyone that you 0:00 haven't tested and no, clicking around your site isn't testing but 0:04 if you've taken the Build a Social Network with Flask or 0:09 Python Testing courses, you've already been exposed to testing. 0:12 Just in case you haven't, let me, briefly, explain what testing is. 0:17 When I say tests, I mean new code that runs the code on our app and 0:22 makes sure that it works the way we intended. 0:25 We do this, usually, by checking the output of the code or 0:28 how it changed the state of a variable. 0:31 Since you run tests pretty often, 0:34 they're a great way to make sure that your code hasn't broken an existing feature. 0:35 Some people write test before they write code too. 0:39 And write new code, until the tests all pass. 0:42 Whether you write the tests after the code, like we're doing now, or 0:44 you write your test first, as in test driven development, 0:47 having a good base of tests gives you a peace of mind that just can't be beat. 0:50 Let's go see how to test a Django app. 0:53 >> Django actually gave us a file for tests when it generated our app. 0:56 Isn't that nice? 1:00 Let's look at tests.py inside of our courses directory, and 1:01 there's nothing here. 1:06 Yep, we gotta write it all ourselves, which is cool, that's fine. 1:08 The easiest thing to test, I think, is a model. 1:11 So let's go, let's pull up our model, here's our models. 1:15 All right, we're gonna write a test for it. 1:19 So the first thing that we need to do is we need to import our course model and 1:22 then we'll write a test for that. 1:26 So we'll say from .models import Course. 1:27 Tests are written as a class for each thing that you want to test. 1:32 That thing would be a model or a view and 1:36 then that class has methods for each aspect of the thing that you want to test. 1:39 So we'll say class CourseModelTests, and this is a (TestCase). 1:46 And, inside of this class, our tests all have to start with the word test. 1:53 So, we can have methods that test stuff and 1:58 methods that do other extra work that we need. 2:01 I don't know that we're gonna need any of those extra methods right now, though. 2:03 So first of all, let's write a test that creates a course and 2:07 make sure that it has the right created at date. 2:10 First though, we probably want to import date time from Django's utils library. 2:13 From django.utils import timezone. 2:18 Okay, now we can write our test. 2:23 So we'll say def test_course_creation and 2:25 since the method takes (self) and 2:29 we're going to say course = Course.objects.create and 2:33 title will be Python Regular Expressions. 2:39 That's a great title. 2:44 And the description will be Learn to write regular expressions in Python. 2:45 And then we will end our parentheses and 2:53 then we are going to say now = timezone.now. 2:57 And then we are gonna do self.assertless, 3:03 which asserts that A, course.created_at, 3:08 is less than B, which will be now. 3:13 Because there's a microsecond, just a little bit of time, 3:19 between those two things happening, so it's just enough for our test. 3:23 So let's make sure our test is true. 3:28 We come down here and we cd into learning_site python manage.py test. 3:31 And what this does is it runs one test, which hey we only wrote one test. 3:41 And that one test was good, it passed, it was okay, no problems at all. 3:46 So our test is a good test. 3:50 Now we want to run these tests a lot. 3:52 We wanna run these tests whenever we change something. 3:55 So you're gonna write a lot of small tests like this. 3:58 This is a great one, though and I'm glad that it passed. 4:01 Testing models is usually the first step in thoroughly testing a Django app. 4:05 Our test so far is really simplistic, and only covers a single model. 4:09 Why don't you write a test case, and test for the step model now. 4:12
You need to sign up for Treehouse in order to download course files.Sign up