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

时间:2015-04-15 21:19:03

标签: python django unit-testing

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

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

这是我执行测试时会发生什么:

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

这是(测试从项目的根目录运行,但不是从其他地方运行)预期的行为?有没有办法改变它以便它可以工作(我想从不同目录中的预提交钩子执行我的测试)?

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

我可以尝试其他任何想法吗?

2 个答案:

答案 0 :(得分:4)

迟到回答这个问题。但希望这有助于某人:

{ "definition": { "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#", "actions": { "For_each": { "actions": { "Condition": { "actions": { "Append_to_array_variable": { "inputs": { "name": "result", "value": "@items('For_each')" }, "runAfter": {}, "type": "AppendToArrayVariable" } }, "expression": "@contains(tolower(items('For_each')['VM']), 'myvm1')", "runAfter": {}, "type": "If" } }, "foreach": "@body('Parse_JSON')", "runAfter": { "Initialize_variable": [ "Succeeded" ] }, "type": "Foreach" }, "Initialize_variable": { "inputs": { "variables": [ { "name": "result", "type": "Array" } ] }, "runAfter": { "Parse_JSON": [ "Succeeded" ] }, "type": "InitializeVariable" }, "Parse_JSON": { "inputs": { "content": [ { "PSComputerName": "localhost", "PSShowComputerName": true, "PSSourceJobInstanceId": "5e18cd92-5676-4ed6-a7e4-14b0d9fea3b3", "Success": true, "VM": "MyVM1" }, { "PSComputerName": "localhost", "PSShowComputerName": true, "PSSourceJobInstanceId": "5e18cd92-5676-4ed6-a7e4-14b0d9fea3b3", "Success": true, "VM": "MyVM2" } ], "schema": { "items": { "properties": { "PSComputerName": { "type": "string" }, "PSShowComputerName": { "type": "boolean" }, "PSSourceJobInstanceId": { "type": "string" }, "Success": { "type": "boolean" }, "VM": { "type": "string" } }, "required": [ "VM", "Success", "PSComputerName", "PSShowComputerName", "PSSourceJobInstanceId" ], "type": "object" }, "type": "array" } }, "runAfter": {}, "type": "ParseJson" }, "Response": { "inputs": { "body": "@variables('result')", "statusCode": 200 }, "runAfter": { "For_each": [ "Succeeded" ] }, "type": "Response" } }, "contentVersion": "1.0.0.0", "outputs": {}, "parameters": {}, "triggers": { "manual": { "inputs": { "schema": {} }, "kind": "Http", "type": "Request" } } } }

Running Django tests from another directory

来自Django文档:

  

您还可以提供目录路径以发现该目录下的测试:

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

参考文献:

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

答案 1 :(得分:1)

基于@dizballanze的评论以及对代码审核的反馈,我最终做到了这一点:

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

我仍然很好奇是否可以在没有进入项目根目录的情况下运行测试。