Django pytest AssertionError: should return body unicode
Django pytest AssertionError: should return body unicode
我正在学习 django-pytest,我正在尝试测试 unicode,但出现错误
我的模特
class Post(models.Model):
body = models.TextField()
value = models.CharField(max_length=5)
def __unicode__(self):
return self.body
测试
class TestPost:
def test_model(self):
obj = mixer.blend('birdie.Post')
assert obj.pk == 1, 'should create a post instance '
def test_uniccode(self):
obj = mixer.blend('birdie.Post',body='hello')
result = obj.__unicode__
assert result == 'hello', 'should return body unicode
我在测试 unicode 时遇到困难,我这样做只是为了获得 100% 的覆盖率
错误
对于通过测试的任何帮助,我们将不胜感激。提前致谢
当然,我不明白你为什么不接受(1 个月前接受)我的回答。它帮助了你。很明显。也许,你接受了你朋友的回答。好吧,祝你好运。
你得打电话给
obj.__unicode__()
它应该可以正常工作。
这是一个方法,所以就像方法一样调用它。不要忘记括号。
class Group(models.Model):
name = models.TextField()
def __unicode__(self):
return self.name
obj = Group(name='test')
obj.save()
obj.__unicode__
<bound method Group.__unicode__ of <Group: test>>
obj.__unicode__()
u'test'
result = unicode(obj)
result == u'hello'
object = obj
result = object.__unicode__()
我正在学习 django-pytest,我正在尝试测试 unicode,但出现错误
我的模特
class Post(models.Model):
body = models.TextField()
value = models.CharField(max_length=5)
def __unicode__(self):
return self.body
测试
class TestPost:
def test_model(self):
obj = mixer.blend('birdie.Post')
assert obj.pk == 1, 'should create a post instance '
def test_uniccode(self):
obj = mixer.blend('birdie.Post',body='hello')
result = obj.__unicode__
assert result == 'hello', 'should return body unicode
我在测试 unicode 时遇到困难,我这样做只是为了获得 100% 的覆盖率
错误
对于通过测试的任何帮助,我们将不胜感激。提前致谢
当然,我不明白你为什么不接受(1 个月前接受)我的回答。它帮助了你。很明显。也许,你接受了你朋友的回答。好吧,祝你好运。
你得打电话给
obj.__unicode__()
它应该可以正常工作。
这是一个方法,所以就像方法一样调用它。不要忘记括号。
class Group(models.Model):
name = models.TextField()
def __unicode__(self):
return self.name
obj = Group(name='test')
obj.save()
obj.__unicode__
<bound method Group.__unicode__ of <Group: test>>
obj.__unicode__()
u'test'
result = unicode(obj)
result == u'hello'
object = obj
result = object.__unicode__()