Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The problem look to be that you are comparing two values that were assigned with the time 'now' at two different points in time.</p> <p>Writing unit tests to work with automatically generated dates is always tricky when trying to assert exact date values due to the ever changing nature of time. However, there are a couple of techniques that can be used to help create reliable tests in these scenarios:</p> <ul> <li><p>If you are trying to assert '<code>nfse_no_banco.data_emissao</code> contains the time now', instead of trying to assert an exact value you could assert that the time field value falls within the last x milliseconds of time. This allows you to gain a fair level of confidence that the value in the field was 'now' at the time it was assigned, but the downsides are (a) your test could unreliable if the execution time of the test happens to take longer than x milliseconds and (b) the test would return a false positive if for some reason the value was incorrectly assigned a time very close to now due to a programming error (which is highly unlikely).</p></li> <li><p>You can monkey-patch <code>datetime.datetime.utcnow</code> to your own version of the method that returns a pre-set value for testing purposes, and then assert that value was assigned to <code>nfse_no_banco.data_emissao</code>. The downside is that it adds a little complexity to your test setup and teardown. However, it should result in a good test if the goal of your assertion is to verify that the field has been assigned the time now.</p></li> <li><p>You can simply assert that the value of the field is not null (using <code>self.assertNotNull(nfse_no_banco.data_emissao)</code>) - although this is a much weaker assertion, in cases where you are using some framework functionality (such as <code>auto_now_add=True</code> in Django) then often this will suffice - of course the major upside to this test is it's very simple and reliable.</p></li> </ul> <p>The best approach really depends on what you are trying to assert. From your question it appears that you are really trying to assert that <code>nfse_no_banco.data_emissao</code> was assigned the time now, and you are doing this yourself (rather than relying on a framework to do it for you), and therefore the second approach would make most sense.</p> <p>Below is pseudo-code showing how you could do this in your test:</p> <pre><code># Create a constant with a fixed value for utcnow NOW = datetime.datetime.utcnow() # Define a test method to replace datetime.datetime.utcnow def utcnow_fixed_value(): return NOW class MyTest(TestCase): def setUp(self): # Replace the real version of utcnow with our test version self.real_utcnow = datetime.datetime.utcnow datetime.datetime.utcnow = utcnow_fixed_value def tearDown(self): # Undo the monkey patch and replace the real version of utcnow datetime.datetime.utcnow = self.real_utcnow def test_value_is_now(self): lista_nfse = Nfse.objects.all() self.assertEquals(lista_nfse.count(), 1) nfse_no_banco = lista_nfse[0] ... self.assertEquals(NOW, nfse_no_banco.data_emissao) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload