我目前正在尝试为我在views.py中编写的功能创建单元测试。但是,我无法找到合适的方法。这是我到目前为止的例子:
views.py:
class SalesListView(LoginRequiredMixin, TemplateView):
def get_month(self):
if self.request.GET.get('month'):
d = datetime.strptime(self.request.GET.get('month'), '%Y-%m')
else:
d = datetime.today()
return d
我试图像这样测试它:
tests.py:
class ViewFunctionsTests(TestCase):
def test_saleslistview(self):
self.request = Mock(session={'month': '2019-11'}, method='GET')
view = SalesListView()
self.assertEqual(view.get_month(), '2019-11')
但是,这将返回AttributeError说明: AttributeError: 'SalesListView' object has no attribute 'request'
有人对上述视图功能的正确测试方法有何建议?提前谢谢你。
从Django文档中:
from django.test import Client
c = Client()
response = c.post('/login/', {'username': 'john', 'password': 'smith'})
response.status_code
200
response = c.get('/customer/details/')
response.content
有关更多详细信息,请访问:https : //docs.djangoproject.com/en/3.0/topics/testing/tools/
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句