Python 为什么django没有看到我的测试?
我创建了test.py模块,其中包含Python 为什么django没有看到我的测试?,python,django,testing,client,Python,Django,Testing,Client,我创建了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 setU
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>'])
....
创建测试数据库后,我得到一个答案“在0.000秒内运行了0个测试”
我做错了什么
附言。
以下是我的项目层次结构:
MyProj:
forum (it's my app):
manage.py
models.py
views.py
tests.py
...
我将test.py重命名为tests.py。Eclipse知道该模块带有测试,但答案仍然是“在0.000s内运行0个测试”您必须将其命名为
tests.py
如果您将文件重命名为tests.py
后得到相同的结果,则有些地方不太正确。你是如何运行测试的?您是从命令行执行此操作,还是使用Eclipse设置了自定义运行目标?如果还没有,请从命令行尝试
同时启动Django shell(python manage.py shell
)并导入测试模块
from MyProj.forum.tests import SimpleTest
导入是否正常工作 尝试将您的方法
test
重命名为类似test\u content
我相信测试运行程序将运行所有名为
test.*
的方法(请参阅python文档了解.Django的TestCase
是unittest.TestCase
的一个子类,因此同样的规则应该适用。对于每个测试方法,您都需要使用前缀test.摘要:
0)仅针对您的应用尝试跑步:
python manage.py test YOUR_APP
1) 如果您的应用程序处于已安装的应用程序配置中,请签入您的设置.py文件
2) 测试方法应以“测试”一词开头,例如:
3) 如果使用名为tests的目录而不是tests.py文件,请检查其中是否有init.py文件
4) 如果使用的是tests目录,请删除tests.pyc和tests.pyo文件。(pycachedir for Python3)我已经尝试了所有这些方法,但是我忽略了在我创建的用于保存所有测试的tests目录中添加uu init_uu.py文件,Django找不到它 经过一段时间的搜索,我没有找到任何人提出这一建议,因此我将把它作为迟来的答案与大家分享。
在我的例子中,我在根目录中有我的manage.py
.
...
├── dir
│ ├── project_name
│ ├── manage.py
│ ├── media
│ ├── app1
│ ├── static
│ ├── staticfiles
│ ├── templates
│ └── app2
...
所以我发现它可以选择提供要运行哪些测试的项目。就我而言,我必须这样做
python project_name/manage.py test ./project_name/
这成功地运行了我的测试。我将test.py重命名为tests.py。Eclipse知道这个模块带有测试,但答案仍然是“在0.000秒内运行了0个测试”。我已经将我的测试类重命名为test_forum。但是它没有帮助不要重命名类,重命名方法。它可能区分大小写,所以请尝试test_论坛。我尝试了“python manage.py shell”和“from MyProj.forum.tests导入test_论坛”(我重命名了test类),但它不起作用。但是“import MyProj.forum.tests”和“from MyProj.forum.tests import*”没有错误地通过。看起来好像有什么问题。尝试以下操作:从MyProj.forum导入测试
,然后是目录(测试)
。查看您的类名是否出现在dir
命令的输出中。正如预期的那样:>>dir(tests)['builtins','doc','file','name','package','path']
因此,tests.py中没有我的测试类。我错过了什么?这很有趣:列表中没有Test\u论坛
。在我看来,这里有一个名为tests
的目录。这就解释了为什么MyProj.forum.tests导入测试论坛失败,但导入MyProj.forum.tests成功的原因。你能检查一下情况吗?是的,你是对的。我尝试先用nose和urllib/urllib2测试我的应用程序,为此我创建了包测试。我删除了这个包,django测试工具在tests.py中获得了我的测试类。谢谢你,Manoj Govindan。你救了我!我也是,在Django 1.4上。谢谢!除了4之外,不要忘记删除\uuu pycache\uuu
文件夹问题是2。谢谢
.
...
├── dir
│ ├── project_name
│ ├── manage.py
│ ├── media
│ ├── app1
│ ├── static
│ ├── staticfiles
│ ├── templates
│ └── app2
...
python project_name/manage.py test ./project_name/