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?
Late in answering this question. But hope this helps someone:
python <project_path>/manage.py test <your_project_dir>
From Django documentation:
You can also provide a path to a directory to discover tests below that directory:
$ ./manage.py test animals/
References:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With