<commit_before>########################
Testing Your Extenstions
########################

************
Testing Apps
************

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::

    from cms.test_utils.util.context_managers import SettingsOverride
    from cms.test_utils.testcases import CMSTestCase

    class MyappTests(CMSTestCase):

        def test_myapp_page(self):
            with SettingsOverride(ROOT_URLCONF='myapp.tests.test_urls'):
                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
the ``SettingsOverrideTestCase``::

    from cms.test_utils.testcases import SettingsOverrideTestCase

    class MyappTests(SettingsOverrideTestCase):

        settings_overrides = {'ROOT_URLCONF': 'myapp.tests.test_urls'}

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

***************
Testing Plugins
***************

Plugins can just be created as objects and then have methods called on them.
So you could do::

    from django.test import TestCase
    from myapp.cms_plugins import MyPlugin
    from myapp.models import MyappPlugin as MyappPluginModel

    class MypluginTests(TestCase):

        def setUp(self):
            self.plugin = MyPlugin()
            self.plugin.save()

        def test_plugin(self):<commit_msg>use django override_settings decorator in cms app test examples<commit_after>########################
Testing Your Extenstions
########################

************
Testing Apps
************

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

***************
Testing Plugins
***************

Plugins can just be created as objects and then have methods called on them.
So you could do::

    from django.test import TestCase
    from myapp.cms_plugins import MyPlugin
    from myapp.models import MyappPlugin as MyappPluginModel

    class MypluginTests(TestCase):

        def setUp(self):
            self.plugin = MyPlugin()
            self.plugin.save()

        def test_plugin(self):
{'fraction_non_alphanumeric': 0.12742830712303424, 'fraction_numerical': 0.0, 'mean_word_length': 3.4313524590163933, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2243742', 'n_tokens_mistral': 1208, 'n_tokens_neox': 1196, 'n_words': 435}