【发布时间】:2015-09-25 20:47:12
【问题描述】:
我正在使用 python 3.4 和 Django 1.8.2
我正在使用一些断言对 Artist 对象执行一些测试用例:
我希望该页面在我对 /artist/<id> 的测试中返回我(存储在 res 变量中)并返回 200 状态代码,这是正常的。在http协议中
另外,我希望在内容中检查带有艺术家姓名的单词
from django.test import TestCase
from .models import Artists
class TestArtist(TestCase):
def setUp(self):
self.artist = Artists.objects.create(first_name = 'Ricky',
last_name ='Martin')
def test_existe_vista(self):
#print (self.client.get('/artists/%d' % self.artist.id))
res = self.client.get('/artists/%d' % self.artist.id)
self.assertEqual(res.status_code, 200)
self.assertTrue('Ricky' in res.content)
输出是:
(venv)➜ myproject ./manage.py test artists
Creating test database for alias 'default'...
/home/bgarcial/.virtualenvs/venv/lib/python3.4/site-packages/django/db/backends/sqlite3/base.py:57: RuntimeWarning: SQLite received a naive datetime (2015-07-08 05:09:23.051431) while time zone support is active.
RuntimeWarning)
E
======================================================================
ERROR: test_existe_vista (artists.tests.TestArtist)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/bgarcial/workspace/Project/myprojec/artists/tests.py", line 43, in test_existe_vista
self.assertTrue('Ricky' in res.content)
TypeError: Type str doesn't support the buffer API
----------------------------------------------------------------------
Ran 1 test in 0.286s
FAILED (errors=1)
Destroying test database for alias 'default'...
/home/bgarcial/.virtualenvs/venv/lib/python3.4/site-packages/django/db/backends/sqlite3/base.py:57: RuntimeWarning: SQLite received a naive datetime (2015-07-08 05:09:23.236391) while time zone support is active.
RuntimeWarning)
(venv)➜ project
我的 assertTrue 断言中的 TypeError: Type str doesn't support the buffer API 是什么意思?
关于主题,我还想评论一下,当我执行第一个测试或打印艺术家对象的第一行(在断言之前)时,这个对象是这样打印的:
如何打印人类可读的对象?
str 方法在测试用例中对我不起作用,就像在我的 admin.py 文件中一样
关于 SQLite 警告,我想我应该探索一下 :) 谢谢
【问题讨论】:
标签: python-3.x testcase django-1.8