Handling test data that depends on temporal data (that might change while the test runs)

Marcelo de Moraes Serpa celoserpa at gmail.com
Thu May 15 13:21:34 EDT 2008


Hello,

So, I have this particular method, generate_chat_dir_string, which should
generate a string in the following format:
 "md5hexstring-day-month-year-hour-minute"

This string will be used to create a directory in the filesystem.

I'm trying to adopt the TDD approach, so, I'm starting by testing all the
methods of the Chat class, including this one. However, an issue just popped
up, this is the test code:


   1.   def test_generate_chat_dir_string(self):
   2.         import md5
   3.         import random
   4.         import datetime
   5.         md5_handler = md5.new()
   6.         users = [{"user":"pedro","atendente":False},{
   "user":"joão","atendente":True}]
   7.         #salt = random.random().to_s()
   8.         #md5_handler.update(salt)
   9.         now = datetime.datetime.now()
   10.         ts = now.strftime("%d-%m-%Y-%H-%M")
   11.         for user in users:
   12.             md5_handler.update(user["user"])
   13.
   14.
   15.         seed = md5_handler.hexdigest()
   16.
   17.         final_string = seed + "-" + ts
   18.
   19.         method_generated_string = self.chat.generated_chat_dir_string
   (users,seed)
   20.
   21.         self.assertEquals(final_string,method_generated_string)

As you can see, it generates a specific set of data and assembles a
final_string. It then feeds the same data to generate_chat_dir_string and
compare to see if the final_string is equal to the string generated by the
tested method.

However, they might not be equal becouse of the temporal data. In this case
I'm not using seconds, but in a bizarre situation a minute could have passed
and the test would faile becouse the two strings would have a different
minute portion. Something like this:

"b2ef9c7b10eb0985365f913420ccb84a-30-10-2008-10-31"
"b2ef9c7b10eb0985365f913420ccb84a-30-10-2008-10-32"

How could I handle this issue?

Thanks in advance,

Marcelo.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080515/36950f61/attachment.html>


More information about the Python-list mailing list