我正在使用python 3.4和Django 1.8.2
我正在使用一些断言执行一些关于Artist对象的测试用例:
我希望该页面在我的/artist/<id>
测试中返回(存储在res变量中)并返回200状态代码,即O.K.在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警告我想我应该探索:) 谢谢
答案 0 :(得分:1)
在这里完成同样的事情。尝试self.assertTrue('Ricky' in str(res.content))