Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assertion error while testing Django views

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')

]

like image 621
Sachin Avatar asked Jul 12 '19 12:07

Sachin


People also ask

What are the exceptions in Django test cases?

The only exceptions that are not visible to the test client are Http404 , PermissionDenied, SystemExit, and SuspiciousOperation. Django catches these exceptions internally and converts them into the appropriate HTTP response codes. In these cases, you can check response.status_code in your test.

How do I test async functions in Django?

Firstly, your tests must be async def methods on the test class (in order to give them an asynchronous context). Django will automatically detect any async def tests and wrap them so they run in their own event loop. If you are testing from an asynchronous function, you must also use the asynchronous test client.

How to handle AssertionError in Java?

Both of the ways are valid. AssertionError is inherited from Exception class, when this exception occurs and raises AssertionError there are two ways to handle, either the user handles it or the default exception handler. In Example 1 we have seen how the default exception handler does the work.

What is a test client in Django?

The test client is a Python class that acts as a dummy web browser, allowing you to test your views and interact with your Django-powered application programmatically. Simulate GET and POST requests on a URL and observe the response – everything from low-level HTTP (result headers and status codes) to page content.


2 Answers

It would be something like either you inherit the TestCase or TransactionTestCase or by using the same SimpleTestCase in the following way

class CustomClass(django.test.SimpleTestCase):
    databases = '__all__'
    ...

Earlier SimpleTestCase had dependency on allow_database_queries = True which is depreciated since django version 2.2.

This attribute is deprecated in favor of databases. The previous behavior of allow_database_queries = True can be achieved by setting databases = '__all__'.

https://docs.djangoproject.com/en/2.2/topics/testing/tools/#django.test.SimpleTestCase.databases

like image 123
Umar Asghar Avatar answered Oct 17 '22 03:10

Umar Asghar


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.

like image 42
Jacinator Avatar answered Oct 17 '22 02:10

Jacinator