Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Django 1.8 compatibility #3

Open
PabloCastellano opened this issue Jul 23, 2015 · 1 comment
Open

Django 1.8 compatibility #3

PabloCastellano opened this issue Jul 23, 2015 · 1 comment

Comments

@PabloCastellano
Copy link

This code won't work in Django 1.8 since it removes django.test.simple.

From https://docs.djangoproject.com/en/1.8/releases/1.6/#discovery-of-tests-in-any-test-module:

Discovery of tests in any test module¶

Django 1.6 ships with a new test runner that allows more flexibility in the location of tests. The previous runner (django.test.simple.DjangoTestSuiteRunner) found tests only in the models.py and tests.py modules of a Python package in INSTALLED_APPS.

The new runner (django.test.runner.DiscoverRunner) uses the test discovery features built into unittest2 (the version of unittest in the Python 2.7+ standard library, and bundled with Django). With test discovery, tests can be located in any module whose name matches the pattern test*.py.
@Saigesp
Copy link

Saigesp commented Jan 4, 2020

As @PabloCastellano commented is deprecated on version 1.8 https://docs.djangoproject.com/en/dev/internals/deprecation/#deprecation-removed-in-1-8.

Also, pymongo uses now MongoClient instead of Connection, and you need to disconnect from default database

A quick fix may be:

from django.test.runner import DiscoverRunner
from django.test import TransactionTestCase
from django.conf import settings
from mongoengine import connect, disconnect
from pymongo import MongoClient

class TestRunner(DiscoverRunner):
    """
    Custom test runner for mongoengine
    """
    def setup_databases(self, **kwargs):
        db_name = settings.TEST_DBNAME # test db defined on settings
        disconnect() # disconnect from default db
        connect(db_name)
        print 'Connecting to test database: ' + db_name
        return db_name

    def teardown_databases(self, db_name, **kwargs):
        if not self.keepdb:
            conn = MongoClient()
            conn.drop_database(db_name)
            print 'Dropping test database: ' + db_name


class TestCase(TransactionTestCase):
    def _fixture_setup(self):
        pass

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants