Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I run django tests (manage.py) from a different directory?

I have a pretty standard Django test case setup (I think)

api-name
    manage.py
    api-name
        __init__.py
        settings.py
        wsgi.py
    v0
        project
            stuff.py
            tests
                test_stuff.py

manage.py

#!/usr/bin/env python
import os
import sys

if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "api-name.settings")

    from django.core.management import execute_from_command_line

    execute_from_command_line(sys.argv)

wsgi.py

import sys
import os
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "api-name.settings")

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

test_stuff.py

from django.test import TestCase
from v0.project.stuff import *


class ProjectTestCase(TestCase):
    def setUp(self):
        # set stuff up

    def test_project_stuff(self):
        # test stuff

    def test_other_stuff(self):
        # test stuff

Here's what happens when I execute tests:

[cwilbur api-name]$ ./manage.py test
Creating test database for alias 'default'...
..
----------------------------------------------------------------------
Ran 2 tests in 0.014s

OK
Destroying test database for alias 'default'...
[cwilbur api-name]$ cd ..
[cwilbur source]$ ./api-name/manage.py test
Creating test database for alias 'default'...

----------------------------------------------------------------------
Ran 0 tests in 0.000s

OK
Destroying test database for alias 'default'...

Is this (tests run from the project's root directory, but not from elsewhere) expected behavior? Is there a way to change things so that it will work (I want to execute my tests from a pre-commit hook in a different directory)?

I tried adding from tests import * from this answer, but it didn't help. I also tried moving the sys.path.append line from wsgi.py to manage.py, but that didn't help either.

Any other ideas out there I can try?

like image 852
Chuck Wilbur Avatar asked Apr 15 '15 21:04

Chuck Wilbur


2 Answers

Late in answering this question. But hope this helps someone:

python <project_path>/manage.py test <your_project_dir>

Running Django tests from another directory

From Django documentation:

You can also provide a path to a directory to discover tests below that directory:

$ ./manage.py test animals/

References:

  1. https://docs.djangoproject.com/en/dev/topics/testing/overview/#running-tests
  2. https://docs.python.org/3/library/unittest.html#test-discovery
like image 111
Kenpachi Avatar answered Sep 30 '22 07:09

Kenpachi


Based on the comments from @dizballanze and feedback from review of the code, I wound up doing this:

(cd /somedir && python manage.py test)

I'm still curious as to whether it's possible to run the tests without cding into the root directory of the project, though.

like image 31
Chuck Wilbur Avatar answered Sep 30 '22 05:09

Chuck Wilbur