Django MongoEngine tests directory/folder

Hello everyone,

This took me hours to work, I hope you find it useful and don't waste time looking for it as I did.

What I was trying to accomplish?

I was trying to have a tests folder with tests files instead of a single file (tests.py).

Why?
Because I was using Selenium to generate python tests and I don't wanna append them into the tests.py since Selenium generate a single python test script for each test.

You may wanna check my other post before this one

http://techamad.blogspot.com.es/2015/05/django-tests-with-mongoengine.html


Versions:
Django 1.7
MongoEngine 0.8.7

How?
1-in setting.py of myapp


TEST_RUNNER = 'myapp.tests.__init__.NoSQLTestRunner'

2-create a folder inside your myapp folder and create a file named __init__.py

3-write the below in the created __init__.py

import unittest
from django.test import TestCase
from django.test.simple import DjangoTestSuiteRunner
import os

def suite():   
    return unittest.TestLoader().discover("OnToology.tests", pattern="*.py")

class NoSQLTestRunner(DjangoTestSuiteRunner):
    def setup_databases(self):
        pass
    def teardown_databases(self, *args):
        pass

class NoSQLTestCase(TestCase):
    def _fixture_setup(self):
        pass
    def _fixture_teardown(self):
        pass

4- in terminal go the parent directory of your myapp

python manage.py test myapp


I also recommend using  coverage

Resources:
http://stackoverflow.com/questions/6248510/how-to-spread-django-unit-tests-over-multiple-files
http://toastdriven.com/blog/2011/apr/10/guide-to-testing-in-django/
https://realpython.com/blog/python/testing-in-django-part-1-best-practices-and-examples/


Comments

Popular Posts