4

I'm trying to automate the test rerun after a change while developing. After searching around a little sniffer seemed fine. But if I run it my tests fail with this error:

ERROR: Failure: ImportError (Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined.)

if I run them manually they pass. Do you have a clue why sniffer won't work?

optikfluffel
  • 2,538
  • 4
  • 20
  • 27
  • the reason probably is related to the fact that sniffer runner does not preserve the environment variables of your user. If you can add some line of codes showing how do you call your test function we can provide more hints – furins Dec 20 '12 at 16:08

3 Answers3

4

Something like the following as your scent.py should work:

from subprocess import call
from sniffer.api import runnable

@runnable
def execute_tests(*args):
    fn = [ 'python', 'manage.py', 'test' ]
    fn += args[1:]
    return call(fn) == 0

Which you can then call as sniffer -x appName.

M Somerville
  • 4,499
  • 30
  • 38
1

You can get sniffer to read your settings by creating a scent.py file in the same directory as manage.py.

Here's what mine looks like:

import os
os.environ["DJANGO_SETTINGS_MODULE"] = 'myapp.settings'

Which will get you as far as sniffer reading your settings, but then you'll run into other problems — basically, sniffer just runs your tests using nose, which isn't the same thing that the manage.py test does when django-nose is installed.

Anybody know what else needs to be in scent.py for snigger to with with Django?

mgerring
  • 53
  • 1
  • 6
0

Trying to guess where the problem may reside: it seems you need to explicitly set the position of your settings.py file.

if you're running your test from a subprocess' call you can use the following command:

call(["django-admin.py", "test --settings=your_project.settings"])

otherwise you can set environment variables with the following command:

import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'your_project.settings'

(change your_project with the name of your django project)

if you're running a command like "./manage.py tests" you can add the former lines at the beginning of manage.py (there are other ways but I need to see the code to provide a more precise solution)

furins
  • 4,979
  • 1
  • 39
  • 57