Assertion error while testing Django views

风流意气都作罢 提交于 2020-04-07 07:17:37

问题


This is my testing function for views.py which I have mention below:

def test_operation_page(self):
    url = reverse('operation')
    response = self.client.get(url)
    self.assertEqual(response.status_code, 200)
    self.assertTemplateUsed(response, 'abc.html')
    self.assertContains(response, '<b>BOOK id having certain title:</b>')

This is the error I am having while testing my views

AssertionError: Database queries to 'default' are not allowed in SimpleTestCase subclasses. Either subclass TestCase or TransactionTestCase to ensure proper test isolation or add 'default' to home.tests.TestViews.databases to silence this failure.

This is my views.py

def operation(request):
    queryset=Mytable.objects.filter(title="The Diary of Virginia Woolf  Volume Five: 1936-1941").values('bookid')
    textset=list(Mytable.objects.order_by('-bookid').values('title'))
    context={

    'key1' : queryset, 
    'key2' : textset
    }
    return render(request,'abc.html',context)

This is my urls.py

urlpatterns = [
path('admin/', admin.site.urls),
path('',v.index,name='index'),
path('abc/',v.operation,name='operation')

]


回答1:


As it states in the docs under SimpleTestCase, "If your tests make any database queries, use subclasses TransactionTestCase or TestCase."

The error that you are getting is telling you that your view is trying to execute a database query in a subclass of SimpleTestCase. You should change what TestCase class you are using - that should solve the error.



来源:https://stackoverflow.com/questions/57007022/assertion-error-while-testing-django-views

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!