django test with mongoengine

1,115 views
Skip to first unread message

kuno

unread,
Sep 30, 2011, 5:11:21 PM9/30/11
to MongoEngine Developers
Hi all:
glad to be here :)
Recently, I am working on a django project, and I choose
mongoengine as the orm for my database.

But it seems that django's testing framework only works wit sql db,
because I always got

<====================
Ran 0 tests in 0.000s
====================>

and My testing class is looks like this:

<=======================================
from django.test import Client
from django.utils import unittest
client = Client()

class ExampleTestCase(unittest.TestCase):
def get(self):
response = client.get('/')
self.assertEqual(response.status_code, 200)

========================================>

Can anyone help me figure out what's wrong with these code
or
knowing how to runing django test with mongoengine?

thanks
--kuno

vanders...@gmail.com

unread,
Sep 30, 2011, 11:03:28 PM9/30/11
to mongoen...@googlegroups.com
This is probably because you're not connecting with your database before the test.

This test case class that ships with mongoengine should do the trick.

Cheers

2011/9/30 kuno <neo...@gmail.com>



--
Vanderson Mota dos Santos

Eric van Zanten

unread,
Dec 31, 2012, 12:19:54 PM12/31/12
to mongoen...@googlegroups.com, vanders...@gmail.com
Hi there:
I'm running into a similar issue, only the test runner is complaining about django being improperly configured:

django.core.exceptions.ImproperlyConfigured: settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation for more details

Which is weird because I can fire up the Django development server and shell just fine. 

I grabbed the test case linked to above and have my unit test inheriting from it in the way that you'd expect:

from mongoengine.django.tests import MongoTestCase

class MyTestCase(MongoTestCase):
    def a_test(self):
        ...etc...

Any idea what might be happening? I tried only leaving the apps that rely upon MongoEngine in my settings.py as suggested over here and got the same results. 

Thanks for any help,
Eric

Eric van Zanten

unread,
Dec 31, 2012, 12:42:39 PM12/31/12
to mongoen...@googlegroups.com, vanders...@gmail.com
Hello again:

I found what seems to be a solution: create a custom TestRunner for Django like this one. In case that link changes or the owner deletes it or whatever, here's the code: 

from django.test.simple import DjangoTestSuiteRunner
from django.test import TransactionTestCase

from mongoengine import connect

class TestRunner(DjangoTestSuiteRunner):
    def setup_databases(self, **kwangs):
        db_name = 'testsuite'
        connect(db_name)
        print 'Creating test-database: ' + db_name

        return db_name

    def teardown_databases(self, db_name, **kwargs):
        from pymongo import Connection
        conn = Connection()
        conn.drop_database(db_name)
        print 'Dropping test-database: ' + db_name

In your TestCases, you then just have to override the _fixure_setup method like so:

from django.test import TestCase

class MyTestCase(TestCase):
    def _fixture_setup(self):
        pass
     def other_tests_should_run(self):
        ...etc...

Hopefully that helps someone else.

Eric
Message has been deleted

Hoàng Nguyễn Ngọc

unread,
May 5, 2015, 3:22:29 AM5/5/15
to mongoen...@googlegroups.com, vanders...@gmail.com
I do the same but it still fire exception for me. 
Traceback (most recent call last):
  File "/Users/hoangnn/PycharmProjects/jamja/.pydev/lib/python2.7/site-packages/django/test/testcases.py", line 182, in __call__
    self._pre_setup()
  File "/Users/hoangnn/PycharmProjects/jamja/.pydev/lib/python2.7/site-packages/django/test/testcases.py", line 754, in _pre_setup
    self._fixture_setup()
  File "/Users/hoangnn/PycharmProjects/jamja/.pydev/lib/python2.7/site-packages/django/test/testcases.py", line 887, in _fixture_setup
    if not connections_support_transactions():
  File "/Users/hoangnn/PycharmProjects/jamja/.pydev/lib/python2.7/site-packages/django/test/testcases.py", line 874, in connections_support_transactions
    for conn in connections.all())
  File "/Users/hoangnn/PycharmProjects/jamja/.pydev/lib/python2.7/site-packages/django/test/testcases.py", line 874, in <genexpr>
    for conn in connections.all())
  File "/Users/hoangnn/PycharmProjects/jamja/.pydev/lib/python2.7/site-packages/django/utils/functional.py", line 55, in __get__
    res = instance.__dict__[self.func.__name__] = self.func(instance)
  File "/Users/hoangnn/PycharmProjects/jamja/.pydev/lib/python2.7/site-packages/django/db/backends/__init__.py", line 782, in supports_transactions
    self.connection.leave_transaction_management()
  File "/Users/hoangnn/PycharmProjects/jamja/.pydev/lib/python2.7/site-packages/django/db/backends/__init__.py", line 338, in leave_transaction_management
    if managed == self.get_autocommit():
  File "/Users/hoangnn/PycharmProjects/jamja/.pydev/lib/python2.7/site-packages/django/db/backends/__init__.py", line 345, in get_autocommit
    self.ensure_connection()
  File "/Users/hoangnn/PycharmProjects/jamja/.pydev/lib/python2.7/site-packages/django/db/backends/__init__.py", line 133, in ensure_connection
    self.connect()
  File "/Users/hoangnn/PycharmProjects/jamja/.pydev/lib/python2.7/site-packages/django/db/utils.py", line 86, in __exit__
    db_exc_type = getattr(self.wrapper.Database, dj_exc_type.__name__)
AttributeError: 'DatabaseWrapper' object has no attribute 'Database'

This is my trackback, can you help
Reply all
Reply to author
Forward
0 new messages