Thank you for the replies.<br><br>@Gabriel: I understand what you are saying. It all depends on what you want to test. In this case, I wanted to test the algorithm of the method.  The test_generate_chat_dir_string is meant to do just that: Check if the generate_chat_dir_string generated a "valid" (according to the specs) string for a "chat dir name". <br>
<br>It is not a integration nor a functional "unit" test, but it is a "real" unit test in the sense that I created a test for each method and I exercised the methods the following way:<br><br> 1) Got some data to test and...<br>
 2) ... Generated an output "manually" in the test method that I knew were the expected return of the tested method;<br> 3) Fed this data to the tested method and compared the manually generated result with the return value of the method.<br>
<br>It is like testing a method that does a simple division:<br><br>def test_divide<br>  divdend = 10<br>  divisor = 2<br>  expected_result = 5<br>  self.assertEquals(mathprogram.divide(dividend,divisor),5)<br><br>The techniques you explained in your message sound more like functional testing or integration + functional testing where multiple aspects of a single functional unit are being exercised. Or am I wrong?<br>
<br>@Erik:<br><br>I solved the problem by replacing the datetime.datetime with a custom instance in the test method body:<br><br>def my_test_method<br>   now = datetime.datetime.now()<br>   class my_custom_datetime(object):<br>
        def now(self): return now<br>   datetime.datetime = my_custom_datetime()<br><br>works great. Thanks to Zalamander @ #python on EFNET ;)<br>  <br> <br><br><div class="gmail_quote">On Thu, May 15, 2008 at 10:12 PM, Erik Jones <<a href="mailto:mage2k@gmail.com">mage2k@gmail.com</a>> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><br><br><div class="gmail_quote"><div><div></div><div class="Wj3C7c">On Thu, May 15, 2008 at 12:21 PM, Marcelo de Moraes Serpa <<a href="mailto:celoserpa@gmail.com" target="_blank">celoserpa@gmail.com</a>> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Hello,<br><br>So, I have this particular method, generate_chat_dir_string, which should generate a string in the following format:<br> "md5hexstring-day-month-year-hour-minute"<br><br>This string will be used to create a directory in the filesystem.<br>



<br>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:<br><br><ol><li><div>
  def test_generate_chat_dir_string<span>(</span>self<span>)</span>:</div></li><li><div>        import md5</div></li><li><div>        import random</div>
</li><li><div>        import datetime</div></li><li><div>        md5_handler = md5.new<span>(</span><span>)</span></div></li><li><div>
        users = <span>[</span><span>{</span>"user":"pedro","atendente":False<span>}</span>,<span>{</span>"user":"joćo","atendente":True<span>}</span><span>]</span></div>



</li><li><div>        #salt = random.random<span>(</span><span>)</span>.to_s<span>(</span><span>)</span></div></li><li><div>        #md5_handler.update<span>(</span>salt<span>)</span></div>
</li><li><div>        now = datetime.datetime.now<span>(</span><span>)</span></div></li><li><div>        ts = now.strftime<span>(</span>"%d-%m-%Y-%H-%M"<span>)</span></div>
</li><li><div>        for user in users:</div></li><li><div>            md5_handler.update<span>(</span>user<span>[</span>"user"<span>]</span><span>)</span></div>
</li><li><div>        </div></li><li><div>        </div></li><li><div>        seed = md5_handler.hexdigest<span>(</span><span>)</span></div>
</li><li><div>        </div></li><li><div>        final_string = seed + "-" + ts</div></li><li><div>        </div></li><li><div>
        method_generated_string = self.chat.generated_chat_dir_string<span>(</span>users,seed<span>)</span></div></li><li><div>        </div></li><li><div>
        self.assertEquals<span>(</span>final_string,method_generated_string<span>)</span></div></li></ol>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. <br>



<br>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:<br>



<br>"b2ef9c7b10eb0985365f913420ccb84a-30-10-2008-10-31"<br>"b2ef9c7b10eb0985365f913420ccb84a-30-10-2008-10-32"<br><br>How could I handle this issue?<br><br>Thanks in advance,</blockquote></div></div><div>
<br>Add another parameter, with a default, to the method's interface:<br>
<br>def test_generate_chat_dir_string<span>(</span>self<span>, now=None)</span>:<br>    if not now:<br>        import datetime<br>        now = datetime.datetime.now()<br>    .....<br><br><br>That way you can feed it values when testing to validate the calculations but leave it up to the datetime module to fill in the used value in production.<br>

<br><br></div></div><br>-- <br>Erik Jones<br><a href="mailto:mage2k@gmail.com" target="_blank">mage2k@gmail.com</a><br>
</blockquote></div><br>