gpt4 book ai didi

python - 我可以从不同的目录运行 django 测试 (manage.py) 吗?

转载 作者:太空狗 更新时间:2023-10-29 21:39:36 26 4
gpt4 key购买 nike

我有一个非常标准的 Django 测试用例设置(我认为)

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

管理.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

这是我执行测试时发生的情况:

[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'...

这是(从项目的根目录而不是从其他地方运行的测试)预期的行为吗?有没有一种方法可以改变它以使其正常工作(我想从不同目录中的预提交 Hook 执行我的测试)?

我尝试添加 from tests import * from this answer ,但这没有帮助。我还尝试将 sys.path.append 行从 wsgi.py 移动到 manage.py,但这也没有帮助。

还有其他我可以尝试的想法吗?

最佳答案

晚了回答这个问题。但希望这对某人有帮助:

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

Running Django tests from another directory

来自 Django 文档:

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

$ ./manage.py test animals/

引用资料:

  1. https://docs.djangoproject.com/en/dev/topics/testing/overview/#running-tests
  2. https://docs.python.org/3/library/unittest.html#test-discovery

关于python - 我可以从不同的目录运行 django 测试 (manage.py) 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29661112/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com