Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Django Basics Test Time Model Tests

Oleksiy Ovdienko
Oleksiy Ovdienko
447 Points

Is it good if test in this video doesn't passes, crashes with error on self.assertLess course.created_at not less than

Is it good if test in this video doesn't passes, crashes with error on self.assertLess course.created_at not less than now ?

2 Answers

No... if you followed the videos up until now and added the code as Kenneth did, that test should pass without an issue.

If you're worried that the times you're comparing are too close together, you could add a delay by using time.sleep():

learning_site/courses/tests.py

from django.test import TestCase
from django.utils import timezone
import time  # don't forget to import the module first

from .models import Course


class CourseModelTests(TestCase):
    def test_course_creation(self):
        course = Course.objects.create(
            title="Python Regular Expressions",
            description="Learn to write regular expressions in Python"
        )
        time.sleep(1)  # sleep/delay for one second
        now = timezone.now()
        self.assertLess(course.created_at, now)
Aleksandar Kosev
Aleksandar Kosev
4,603 Points

Ok so my test fails when i don't have time.sleep(1) and passes when i have it, what does that mean:O?

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 28,716 Points

Some test environments on your local host will be so fast that when you run now it will be in the same millisecond as the creation step. This happens on my home computer.

Your test is fine.