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

My test fails since both objects are created at the same time

My 'now' and 'course.created_at' have the same time up to millisecond. I noticed that my test was run in 0.001s while the one on video took 0.003s. So is it possible that my test run so fast that both objects have the same time?

4 Answers

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

Unlikely but, sure, it could conceivably happen. You could import the time module and then do time.sleep(2) between the two object creations. That'll insert 2 milliseconds of wait time.

Caleb Simmons
Caleb Simmons
8,670 Points

Is there a reason to not use assertLessEqual? I have followed along with my local machine and assertLess failed almost every time. I am worried that using time.sleep in this kind of test would prohibit using something like cProfile on my code if I ever needed to.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Using assertLessEqual would be fine.

time.sleep() takes seconds actually, not ms. So something like 0.001 is faster, so you're not waiting for your unit tests.

Thanks! It solved the problem

Team GDiz
PLUS
Team GDiz
Courses Plus Student 24,942 Points

I ran into the same problem. time.sleep(2) fixed it for me too :).

mykolash
mykolash
12,955 Points

As far as the issue happens cause of system speed (SSD, fast enough processor, unloaded system, etc) - two actions are being processed at the very same mili-second. I've resolved it by creating a loop-function, making a thousand of divisions of floats. Which creates timestub needed for the system to differentiate the actions.

def timestub():
    [0.9 / (counter + 1) for counter in range(1000)]