use*_*495 8 python testing django 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 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>'])
....
Run Code Online (Sandbox Code Playgroud)
并将其放在我的应用程序的文件夹中.当我运行测试时
python manage.py test forum
Run Code Online (Sandbox Code Playgroud)
创建测试数据库后,我得到一个答案"在0.000s内进行0测试"
我究竟做错了什么 ?
PS这是我的项目层次结构:
MyProj:
forum (it's my app):
manage.py
models.py
views.py
tests.py
...
Run Code Online (Sandbox Code Playgroud)
我将test.py重命名为tests.py.Eclipse得到了这个模块的测试,但答案仍然是"在0.000s内进行0测试"
Pau*_*que 11
摘要:
0)尝试仅针对您的应用运行:
python manage.py test YOUR_APP
Run Code Online (Sandbox Code Playgroud)
1)如果YOUR_APP在INSTALLED_APP配置中,请检入settings.py文件
2)测试方法应以"test"开头,例如:
def test_something(self):
self.assertEquals(1, 2)
Run Code Online (Sandbox Code Playgroud)
3)如果您使用名为tests而不是tests.py文件的目录,请检查其中是否包含init .py文件.
4)如果您使用的是测试目录,请删除tests.pyc和tests.pyo文件.(Python3的pycache目录)
如果将文件重命名为tests.py. 你如何进行测试?您是从命令行执行此操作还是使用 Eclipse 设置自定义运行目标?如果您还没有尝试过,请从命令行尝试一下。
同时启动 Django shell ( python manage.py shell) 并导入您的测试模块。
from MyProj.forum.tests import SimpleTest
Run Code Online (Sandbox Code Playgroud)
导入工作正常吗?
| 归档时间: |
|
| 查看次数: |
8480 次 |
| 最近记录: |