[Django]-Django unit testing Sekizai and django cms

5👍

http://racingtadpole.com/blog/testing-django-cms-sites/

This was confusing because I was using sekizai correctly in my
template.

This post pointed me in the right direction here. The problem was
Django was raising an exception but I was never getting to see it –
just this much less helpful message.

This Stackoverflow post explained how to enable logging of errors. I
copied in the changes to settings.py, wrapping them inside the if
‘test’ statement.

Then when I ran

./manage.py test

I got a much more useful error message: I had forgotten to set up a
table that my template was assuming would exist. Easily fixed!

Hope that helps someone else.

I needed to add some fixtures for it to work.

EDIT:
Ok after a lot of research I found out that despite the above blog I had a different problem.

Thanks to this answer and this doc I knew I had to do it like this :

from django.test import TestCase
from django.test.client import Client
from cms.test_utils.testcases import CMSTestCase
from django.test.utils import override_settings
from cms.models import Page
from django.contrib import admin
from django.conf.urls import url, patterns, include
from django.conf import settings

urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls)),
    url(r'^search_engine/', include('search_engine.urls')),
    url(r'', include('cms.urls')),
)


class AccessTest(CMSTestCase):

    def setUp(self):
        # Every test needs a client.
        p = Page.objects.create(site_id=settings.SITE_ID, template='home_page.html')
        p.publish()
        self.client = Client()

    @override_settings(ROOT_URLCONF='search_engine.tests')
    def test_details(self):
        # Issue a GET request.
        response = self.client.get('/')

        # Check that the response is 200 OK.
        self.assertEqual(response.status_code, 200)

How to unit test Django-CMS extensions?
and http://docs.django-cms.org/en/latest/extending_cms/testing.html <= somehow this is only readable in ‘lastest’

8.1.1. Resolving View Names

Your apps need testing, but in your live site they aren’t in urls.py
as they are attached to a CMS page. So if you want to be able to use
reverse() in your tests, or test templates that use the url template
tag, you need to hook up your app to a special test version of urls.py
and tell your tests to use that.

So you could create myapp/tests/test_urls.py with the following code:

from django.contrib import admin from django.conf.urls import url,
patterns, include

urlpatterns = patterns(”,
url(r’^admin/’, include(admin.site.urls)),
url(r’^myapp/’, include(‘myapp.urls’)),
url(r”, include(‘cms.urls’)), )

And then in your tests you can plug this in with the
override_settings() decorator:

from django.test.utils import override_settings from
cms.test_utils.testcases import CMSTestCase

class MyappTests(CMSTestCase):

@override_settings(ROOT_URLCONF='myapp.tests.test_urls')
def test_myapp_page(self):
    test_url = reverse('myapp_view_name')
    # rest of test as normal

If you want to the test url conf throughout your test class, then you
can use apply the decorator to the whole class:

from django.test.utils import override_settings from
cms.test_utils.testcases import CMSTestCase

@override_settings(ROOT_URLCONF=’myapp.tests.test_urls’) class
MyappTests(CMSTestCase):

def test_myapp_page(self):
    test_url = reverse('myapp_view_name')
    # rest of test as normal
👤maazza

3👍

To solve the sekizai error, this is what I did:

from django.test import TestCase                                                
from sekizai.context import SekizaiContext                                      

from cms.api import add_plugin                                                  
from cms.models import Placeholder  
# Put class def here

def test_plugin(self):                                               
    placeholder = Placeholder.objects.create(slot='test')                   
    model_inst = add_plugin(placeholder,                                    
        cms_plugins.MyPlugin, 'en')                     
    context = SekizaiContext()                                              

    html = model_inst.render_plugin(context=context)                                                                                    
    self.assertTrue(len(html) > 0) 

It was a case of making sure the seikizai context processor was being used during rendering of the plugin.

👤Ash

Leave a comment