Django Testing: URL mapping to the Class Based View - django

I'm new to Django testing so trying basic testing codes. But it is showing one error in second test class
Tests.py
from django.test import TestCase,Client
from .views import PostList
from django.urls import resolve
class SmokeTest2(TestCase):
def test_math(self):
self.assertEqual(1+1,2)
class HomePageTest(TestCase):
def test_root_url_resolves_to_home_page_view(self):
found = resolve('/')
print({'found':found})
self.assertEqual(found.func(), PostList)
views.py
class PostList(ListView):
model = Post
template_name = 'home.html'
urls.py
urlpatterns = [
path('',views.PostList.as_view(),name ='list'),
]
When i am printing found its showing the o/p
{'found': ResolverMatch(func=blog.views.PostList, args=(), kwargs={}, url_name=list, app_names=[], namespaces=[])}
But still I am getting this error
(blog_env) PS D:\django\blog_env\mysite> python manage.py test
D:\django\blog_env\mysite
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
{'found': ResolverMatch(func=blog.views.PostList, args=(), kwargs={}, url_name=list, app_names=[], namespaces=[])}
E.
======================================================================
ERROR: test_root_url_resolves_to_home_page_view (blog.tests.HomePageTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "D:\django\blog_env\mysite\blog\tests.py", line 19, in test_root_url_resolves_to_home_page_view
self.assertEqual(found.func(), PostList)
TypeError: view() missing 1 required positional argument: 'request'
----------------------------------------------------------------------
Ran 2 tests in 0.069s
FAILED (errors=1)
Destroying test database for alias 'default'...

I was stung by this issue just now, ended up finding the solution in the documentation
class-based views need to be compared by name, as the functions generated by as_view() won't be equal due to different object ids, so the assertion should look like the below:
from django.test import TestCase
from django.urls import resolve
from .views import HomePageView
class HomePageViewViewTest(TestCase):
def test_resolve_to_home_page_view(self):
resolver = resolve('/')
self.assertEqual(resolver.func.__name__, HomePageView.as_view().__name__)

from django.urls import resolve, reverse
class HomePageViewViewTest(TestCase):
def test_resolve_to_home_page_view(self):
resolver = resolve('/')
self.assertEqual(resolver.func.view_class, HomePageView)
You can try this, it worked for me!

Since you are testing a Class based View, from the Traceback it can be seen that it's missing the request object. You can use the RequestFactory provided by the django.test package. Better read the following RequestFactory Documentation to get a good view of it. It will solve your problem.

from django.urls import resolve, reverse
class HomePageTest(TestCase):
def test_root_url_resolves_to_home_page_view(self):
response = self.client.get(resolve('/'))
response = self.client.get(reverse('your_app_name:list'))
self.assertEqual(response.status_code, 200)

Related

host-specific request breaks unrelated test cases

I have the following tests:
from django.test import TestCase
from django.core.urlresolvers import reverse
class TestA(TestCase):
def test_a(self):
reverse('view1')
class TestB(TestCase):
def test_b(self):
self.client.get('/view2/', HTTP_HOST='second.test.net')
class TestC(TestCase):
def test_c(self):
reverse('view1')
TestA and TestB run successfully, but TestC breaks with
..E
======================================================================
ERROR: test_c (dhtest.tests.test_view2.TestC)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/phihag/dhtest/dhtest/tests/test_view2.py", line 14, in test_c
reverse('view1')
File "/home/phihag/.local/share/virtualenvs/dhtest---IwXRQ3/lib/python3.6/site-packages/django/urls/base.py", line 91, in reverse
return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)))
File "/home/phihag/.local/share/virtualenvs/dhtest---IwXRQ3/lib/python3.6/site-packages/django/urls/resolvers.py", line 497, in _reverse_with_prefix
raise NoReverseMatch(msg)
django.urls.exceptions.NoReverseMatch: Reverse for 'view1' not found. 'view1' is not a valid view function or pattern name.
----------------------------------------------------------------------
Ran 3 tests in 0.006s
FAILED (errors=1)
But when I comment out TestB, TestC works! How do I fix this problem?
I'm using django-hosts with the following configuration:
from django.conf import settings
from django_hosts import patterns, host
host_patterns = patterns(
'',
host(
r'second\.test\.net',
'dhtest.secondurls',
name='second'
),
host(
r'(\w+)',
'dhtest.urls',
name='default'
),
)
and fairly simple URL files:
# dhtest/urls.py
from django.conf.urls import url
from django.http import HttpResponse
urlpatterns = [
url(r'^view1/', lambda _: HttpResponse('This is view1'), name='view1'),
]
# dhtest/secondurls.py
from django.conf.urls import url
from django.http import HttpResponse
urlpatterns = [
url(r'^view2/', lambda _: HttpResponse('view2 on another host')),
]
For reference, here is the full project.
This is a bug in Django-hosts. In its response middleware, django-hosts clobbers the thread-wide urlconf to that of the request.
In production, that is not a problem, because Django resets the urlconf to the default for every request, or the request-specific one.
But during the tests, after TestB there are no more requests coming, and reverse (and a bunch of other URL-related functions) are using the urlconf for a different host.
To work around this, restore the urlconf after any requests to specific hosts, like this:
class TestB(TestCase):
def test_b(self):
self.client.get('/view2/', HTTP_HOST='second.test.net')
def tearDown(self):
from django.urls.base import set_urlconf
set_urlconf(None)

Update to Django 1.8 - AttributeError: django.test.TestCase has no attribute 'cls_atomics'

I updated a Django 1.7 project to Django 1.8 and now get errors when I run the tests (that are subclasses of django.test.TestCase).
Traceback (most recent call last):
File "env\lib\site-packages\django\test\testcases.py", line 962, in tearDownClass
cls._rollback_atomics(cls.cls_atomics)
AttributeError: type object 'SomeTests' has no attribute 'cls_atomics'
If I debug through the test I can step through all lines without problems, but after the last line the exception is thrown.
This is an example test:
import django
import unittest
from django.test import TestCase
import logging
import sys
from builtins import classmethod, isinstance
class ATestTests(TestCase):
#classmethod
def setUpClass(cls):
django.setup()
logging.basicConfig(stream=sys.stderr, level=logging.DEBUG)
def setUp(self):
self._app = Application(name="a")
def testtest(self):
self.assertIsNotNone(self._app)
My environment:
astroid==1.3.4
colorama==0.3.3
defusedxml==0.4.1
Django==1.8
django-extensions==1.5.2
django-filter==0.9.2
djangorestframework==3.0.5
djangorestframework-xml==1.0.1
eight==0.3.0
future==0.11.4
logilab-common==0.63.2
Markdown==2.5.2
pylint==1.4.1
python-dateutil==2.4.1
python-mimeparse==0.1.4
six==1.9.0
xmltodict==0.9.2
How can I fix this?
I believe the reason is that your setUpClass(cls) class method is not calling super. Because of that, django.tests.TestCase.setUpClass is not called and
cls.cls_atomics = cls._enter_atomics()
is not called, naturally causing cls_atomics to be undefined.
You should add super(ATestTests, cls).setUpClass() to your setUpClass.
For Django 1.8+, you should use TestCase.setUpTestData instead of TestCase.setUpClass.
class MyTests(TestCase):
#classmethod
def setUpTestData(cls):
# Set up data for the whole TestCase
cls.foo = Foo.objects.create(bar="Test")
def test1(self):
self.assertEqual(self.foo.bar, 'Test')
The documentation is here.
I had a similar problem where a TestCase used setUpClass but did not have a tearDownClass method. My tests pass when I add an empty one:
#classmethod
def tearDownClass(cls):
pass
I also do not call django.setup.
Here is the complete code with the call to the base class (as suggested by #J. C. Leitão):
import django
import unittest
from django.test import TestCase
import logging
import sys
from builtins import classmethod
class ATestTests(TestCase):
#classmethod
def setUpClass(cls):
super(ATestTests, cls).setUpClass()
django.setup()
logging.basicConfig(stream=sys.stderr, level=logging.DEBUG)
def setUp(self):
self._app = Application(name="a")
def testtest(self):
self.assertIsNotNone(self._app)

No forms exist in unit test with django-webtest

I want to write a test that will test change the password in the application. I use the django-allauth. For testing, I use django-WebTest.
When I run my code, I get the message:
FAILED (errors=1)
Destroying test database for alias 'default'...
mark#mariusz-K73E:~/myapp$ python manage.py test users
Creating test database for alias 'default'...
.E
======================================================================
ERROR: test_password_change_use_template (myapp.users.tests.ChangePasswordTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/mark/myapp/users/tests.py", line 16, in test_password_change_use_template
password_change.form['oldpassword'] = "test123"
File "/home/mark/.virtualenvs/urlop/local/lib/python2.7/site-packages/webtest/response.py", line 56, in form
"You used response.form, but no forms exist")
TypeError: You used response.form, but no forms exist
My code:
from django_webtest import WebTest
from django_dynamic_fixture import G
from users.models import User
from django.core.urlresolvers import reverse
class ChangePasswordTest(WebTest):
def setUp(self):
self.user = G(User)
def test_password_change_code(self):
password_change = self.app.get(reverse('account_change_password'), user=self.user)
def test_password_change_use_template(self):
password_change = self.app.get(reverse('account_change_password'), user=self.user)
password_change.form['oldpassword'] = "test123"
password_change.form['password1'] = "test456"
password_change.form['password2'] = "test456"
password_change.form.submit()
self.assertRedirects(password_change, reverse('change_password'))
WebTest tests the rendered content. There is no form found in the HTML (maybe the form tag is incorrect or missing).
If it's working in your manual test. You can try to print the response to see what's different:
def test_password_change_use_template(self):
response = self.app.get(reverse('account_change_password'), user=self.user)
print response

Django class based views - no module named base

I'm having a problem trying to use class based views in django.
When I try to import the base View I get an import error.
I've simplified my view down to the exact same code as used in the documentation:
from django.http import HttpResponse
from django.views.base import View
class MyView(View):
def get(self, request):
# <view logic>
return HttpResponse('result')
The error I'm getting is
ImportError at /myurl/
No module named base
urls.py are fine and Django is definitely version 1.5 - I've completely reinstalled it with pip, any ideas?
Because of #dm03514 comment I test it. I try your code in your question and I got the same error with you "No module named base". So when I change it to, like the codes below, it works and no error.
from django.views.generic.base import View
Try before judging, I will accept it if it is wrong and I will try to fix it.

django piston Circular reference detected while emitting response when using model

Am facing a problem with configuring piston for django, every time I specify the model name it runs I get the below error
RuntimeError at /en/vehicle/api/car.json
Circular reference detected while emitting response
Request Method: GET
Request URL: http://127.0.0.1:8000/en/vehicle/api/car.json
Django Version: 1.4.1
Exception Type: RuntimeError
Exception Value:
Circular reference detected while emitting response
Exception Location: /Users/mo/Projects/pythonic/gar-env/lib/python2.7/site-packages/piston/emitters.py in _any, line 109
Python Executable: /Users/mo/Projects/pythonic/gar-env/bin/python
Below is my handelrs.py
from piston.handler import BaseHandler
from piston.utils import rc, throttle, translate_mime
from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage
from django.core.urlresolvers import reverse
from django.db.models.loading import get_model
from models import (ModelLookUpI18n as ModelLookup, Image)
from forms import CarForm, ModelLookUpForm, ModelLookUpI18nForm
from django.http import HttpResponse
import logging, json, os
from piston.utils import validate
from django.conf import settings
from django.utils.translation import ugettext as _
class CarHandler(BaseHandler):
"""
CarHandler
"""
allowed_methods = ('GET', 'POST', 'PUT', 'DELETE')
model = Car
fields = ('id', 'model', 'description', 'primary_image', 'color', 'mileage', 'view_count', 'asking_price')
def read(self, request):
params = dict()
params.update({'status' : self.model.STATUS_ACTIVE})
return self.model.objects.filter(**params).order_by('created_at')
in the url.py here is my code
from django.conf.urls import *
from handlers import CarHandler
from piston.resource import Resource
car_resource = Resource(CarHandler)
# API URL schema
urlpatterns += patterns('',
# car API
url(r'^api/car\.(?P<emitter_format>.+)', car_resource, name='vehicle-api-car'),
)
The error is coming at run time, I cannot find a solution to the problem. I tried to remove model and fields attribute from CarHandler class that would make it work. I tired to use get_model and load at run time but again, I would get the same runtime error.
Please advise?
Is 'model' a foreign key reference? Is it possible Piston is including 'model' in the output, which itself may have a reference back to the Car model it was included in?