【问题标题】:Why does django not see my tests?为什么 django 看不到我的测试?
【发布时间】:2010-09-07 14:10:19
【问题描述】:

我创建了 test.py 模块,里面装满了

from django.test import TestCase
from django.test.client import Client
from django.contrib.auth.models import User
from django.contrib.sites.models import Site

from forum.models import *

class SimpleTest(TestCase):


    def setUp(self):
        u = User.objects.create_user("ak", "ak@abc.org", "pwd")
        Forum.objects.create(title="forum")
        Site.objects.create(domain="test.org", name="test.org")

    def content_test(self, url, values):
        """Get content of url and test that each of items in `values` list is present."""
        r = self.c.get(url)
        self.assertEquals(r.status_code, 200)
        for v in values:
            self.assertTrue(v in r.content)

    def test(self):
        self.c = Client()
        self.c.login(username="ak", password="pwd")

        self.content_test("/forum/", ['<a href="/forum/forum/1/">forum</a>'])
        ....

并将其与我的应用程序一起放在文件夹中。 当我运行测试时

python manage.py test forum

创建测试数据库后,我得到一个答案“在 0.000 秒内运行 0 个测试”

我做错了什么?

附: 这是我的项目层次结构:

MyProj:
    forum (it's my app):
        manage.py
        models.py
        views.py
        tests.py
        ...

我将 test.py 重命名为 tests.py。 Eclipse 知道这个模块有测试,但答案仍然是“在 0.000 秒内运行 0 次测试”

【问题讨论】:

    标签: python django testing client


    【解决方案1】:

    您需要为每种测试方法使用前缀 test_

    【讨论】:

    • 我也是,在 Django 1.4 上。谢谢!
    【解决方案2】:

    总结:

    0) 尝试仅为您的应用运行:

    python manage.py test YOUR_APP
    

    1) 如果 YOUR_APP 在 INSTALLED_APP 配置中,请检查您的 settings.py 文件

    2) 测试方法应以“test”开头,例如:

    def test_something(self):
        self.assertEquals(1, 2)
    

    3) 如果您使用的是名为 tests 的目录而不是 tests.py 文件,请检查它是否有 init.py里面的文件。

    4) 如果您使用的是 tests 目录,请删除 tests.pyctests.pyo 文件。 (pycache 用于 Python3 的目录)

    【讨论】:

    • 除了4,别忘了删除__pycache__文件夹
    • 我的问题是 2。谢谢。
    【解决方案3】:

    尝试将您的方法 test 重命名为 test_content 之类的名称。

    我相信测试运行程序将运行所有名为 test_* 的方法(请参阅 python 文档以了解 organising test code。Django 的 TestCaseunittest.TestCase 的子类,因此应该适用相同的规则。

    【讨论】:

    • 我已将我的测试类重命名为 Test_forum。但这没有帮助
    • 不要重命名类,重命名方法。它可能区分大小写,因此请尝试 test_forum。
    【解决方案4】:

    你必须把它命名为tests.py

    【讨论】:

    • 我将test.py重命名为tests.py。 Eclipse 知道这个模块有测试,但答案仍然是“在 0.000 秒内运行 0 次测试”
    【解决方案5】:

    如果在将文件重命名为 tests.py 后得到相同的结果,则有些地方不太对劲。你是如何运行测试的?您是从命令行执行此操作,还是使用 Eclipse 设置了自定义运行目标?如果您还没有,请从命令行尝试。

    同时启动 Django shell (python manage.py shell) 并导入您的测试模块。

    from MyProj.forum.tests import SimpleTest
    

    导入是否正常工作?

    【讨论】:

    • 我尝试了“python manage.py shell”和“from MyProj.forum.tests import Test_forum”(我重命名了测试类),但没有成功。但是“import MyProj.forum.tests”和“from MyProj.forum.tests import *”没有错误通过。
    • 看起来有些不对劲。试试这个:from MyProj.forum import tests,然后是dir(tests)。查看dir 命令的输出中是否存在您的类名。
    • 正如预期的那样: &gt;&gt;&gt; dir(tests) ['<b>builtins</b>', '<b>doc</b>', '<b> file</b>', '<b>name</b>', '<b>package</b>', '<b>path</b>'] 所以,没有我的tests.py 中的测试类。我错过了什么?
    • 这很有趣:该列表中没有Test_forum。在我看来,您在那里有一个名为tests 的目录。这可以解释为什么from MyProj.forum.tests import Test_forum 失败但import MyProj.forum.tests 成功。你能检查一下是不是这样吗?
    • 是的,你是对的。我首先尝试使用nose 和urllib/urllib2 测试我的应用程序,为此我创建了包测试。我删除了这个包,django 测试工具在 tests.py 中得到了我的测试类。谢谢你,马诺伊戈文丹。你救了我!
    【解决方案6】:

    我已经尝试了所有这些方法,但我忽略了在我创建的用于保存所有测试的测试目录中添加 __init__.py 文件,而 Django 找不到它。

    【讨论】:

      【解决方案7】:

      经过一段时间的搜索,我没有找到任何人建议这个,所以我会分享它作为一个迟到的答案。 在我的情况下,我在根目录中有我的manage.py,例如

      .
      ...
      ├── dir
      │   ├── project_name
      │   ├── manage.py
      │   ├── media
      │   ├── app1
      │   ├── static
      │   ├── staticfiles
      │   ├── templates
      │   └── app2
      ...
      

      所以我发现the test command 可以选择提供要运行哪些测试的项目。就我而言,我必须这样做

      python project_name/manage.py test ./project_name/
      

      这成功地运行了我的测试。

      【讨论】:

        猜你喜欢
        • 2017-06-19
        • 1970-01-01
        • 2011-08-18
        • 2016-09-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-29
        • 1970-01-01
        相关资源
        最近更新 更多