Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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 27,510 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.