I have an existing Django (1.4) project, with multiple apps and extensive
business logic, that I need to write tests for.  Based on a day or two of
reading of the core Django docs and numerous blogs (each of which go about
things subtly differently!?), I have made a start.  As part of the seeming
"best practice" setup, I have also installed django-nose and coverage.

Currently, I have problems with my first test not working and also with
coverage seemingly not finding my test.

I have a separate `tests` directory in my project, with sub-directories;
each corresponding to an app.  Each sub-directory then has a models, views,
and functions Python files; acting as placeholders for the test code I
think need to write.  In the root of the `tests` directory, I have an
__init__.py file that has a number of lines that look like `from
myproj.app1.functions import *`.

I have changed the settings.py file to look like this (so that the tests
use a "fast" sqlite database):

if 'test' in sys.argv:
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME': 'test_db'
        }
    }
else:
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            # etc ... normal setup


The first test, in the first functions.py file, looks like this:


from django.test import TestCase
from django.core import management
from app1.models import default_Property  # function to be tested

def setup():
    management.call_command('loaddata', 'app1/fixtures/initial_data.json',
verbosity=1)

def teardown():
    management.call_command('flush', verbosity=1, interactive=False)

class FunctionsTestCase(TestCase):

    def _fixture_setup(self):
        pass

    def test_default_Property(self):
        self.assertEqual(default_Property(), None)


The default_Property() function in app1 is just set to `return None` for
now, so that the above test should work.

However, the test fails.  I get this strange error:

======================================================================
ERROR: test_default_Property (myproj.app1.tests.FunctionsTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/django/test/testcases.py",
line 508, in __call__
    self._post_teardown()
  File "/usr/local/lib/python2.7/dist-packages/django/test/testcases.py",
line 522, in _post_teardown
    self._fixture_teardown()
  File "/usr/local/lib/python2.7/dist-packages/django/test/testcases.py",
line 847, in _fixture_teardown
    transaction.leave_transaction_management(using=db)
  File "/usr/local/lib/python2.7/dist-packages/django/db/transaction.py",
line 52, in leave_transaction_management
    connection.leave_transaction_management()
  File
"/usr/local/lib/python2.7/dist-packages/django/db/backends/__init__.py",
line 115, in leave_transaction_management
    raise TransactionManagementError("This code isn't under transaction "
TransactionManagementError: This code isn't under transaction management
----------------------------------------------------------------------

I could not find an obvious solution to this, as I do not even have
TransactionMiddleware enabled in my settings.

The other issue relates to coverage - it does not seem to recognise I have
written this test and still flags it as `red` ... Currently I am using
`coverage html' - but how I get it work properly?

Any help getting started overcoming these issues will be helpful, so I can
get on with the actual business of writing tests.  (As a side-note, if
there is a good existing code base, for a Django-based project, that has
test code I could look at, I'd appreciate a link to it.)

Thanks
Derek

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to