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 trialFarid Shukurov
7,657 PointsMy 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
Treehouse Guest TeacherUnlikely 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.
Farid Shukurov
7,657 PointsThanks! It solved the problem
Team GDiz
Courses Plus Student 24,942 PointsI ran into the same problem. time.sleep(2) fixed it for me too :).
mykolash
12,955 PointsAs 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)]
Caleb Simmons
8,670 PointsCaleb Simmons
8,670 PointsIs 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
Treehouse Guest TeacherKenneth Love
Treehouse Guest TeacherUsing
assertLessEqual
would be fine.Alexander Campbell
10,533 PointsAlexander Campbell
10,533 Pointstime.sleep() takes seconds actually, not ms. So something like 0.001 is faster, so you're not waiting for your unit tests.