Note that there are some explanatory texts on larger screens.

plurals
  1. PODjango test.TestCase vs unittest.TestCase
    primarykey
    data
    text
    <p>I am wondering a few things about best practices regarding supplying data for your tests.</p> <p>From the <a href="https://docs.djangoproject.com/en/1.5/topics/testing/overview/" rel="nofollow">docs</a></p> <pre><code>from django.test import TestCase from myapp.models import Animal class AnimalTestCase(TestCase): def setUp(self): Animal.objects.create(name="lion", sound="roar") Animal.objects.create(name="cat", sound="meow") def test_animals_can_speak(self): """Animals that can speak are correctly identified""" lion = Animal.objects.get(name="lion") cat = Animal.objects.get(name="cat") </code></pre> <p>Like the example above I want to save a few models in the <code>setUp</code> method of the testcase which I later use in the particular testcases. I could certainly provide a json fixture for this, but I rather create the models in the code for transparency this time since its very few.</p> <p>When I use <code>django.test.TestCase</code> each individual test is running in a transaction according to the docs. But <strong>no data</strong> from my <code>setUp()</code> is present in the database. I ran it in a debugger and connect to the temp database and checked it.</p> <p>When using <code>django.utils.unittest.TestCase</code> the data is present. Alas, the method is <strong>aggregating the data</strong> since its running for each individual test.</p> <p>I understand why <code>unittest.TestCase</code> is aggregating the data due to not flushing the database. What I do not understand is why the data is not present when using <code>django.test.TestCase</code>. Even in the end of the <code>setUp()</code> method its still blank.</p> <p>Is <code>django.test.TestCase</code> not supposed to save any data or am I doing something entirely wrong? </p> <p>EDIT 1: Did a clean project with this and it started working as I suspected it should. The <code>django.test.TestCase</code> filled the database with the data in the <code>setup()</code> method for each individual run so I guess theres have to be something quite fishy with the setup on my part. I´ll apply Occam's razor and see if I can find out what it is.</p> <p>EDIT 2: Cleaned out the models and tests files and ran the tests again. Setting the debugger to halt after the <code>setUp()</code> but theres still not anything in the database. The objects do have an <code>pk</code> and would assumably be saved to the database. Checked the <code>ModelState</code> object thats inherited to the <code>Model</code> and according to the <code>adding</code> attribute its saved to the database.</p> <p>Added this debug code to the <code>setUp()</code> method:</p> <pre><code>from django.db import connections print connections.databases {'default': {'ENGINE': 'django.db.backends.mysql', 'NAME': 'test_DATABASE_temp', 'TEST_CHARSET': None, 'TEST_COLLATION': None, 'HOST': 'localhost', 'USER': 'user', 'TEST_NAME': None, 'PASSWORD': 'password} </code></pre> <p>But when adding:</p> <pre><code>print Animals.objects.all().count() 2 </code></pre> <p>I get the response that there are in fact objects available. But where are they? Getting a bit obsessed with this now I must admit =).</p> <p>I can proceed now, but still lacking a satisfying answer.</p>
    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