Commits

Anonymous committed 35178e7

urls support in testcase objects

Comments (0)

Files changed (8)

examples/README.TXT

 --- Sample test run below ---
 
 C:\dev\nosedjango\examples\project>nosetests -v --with-django --with-doctest --doctest-tests --doctest-tests
-doctest: project.zoo.models.zoo ... ok
+
+Doctest: project.zoo.models.Zoo ... ok
 Doctest: project.zoo.models.Zoo.__str__ ... ok
 Doctest: project.zoo.models.func ... ok
 This is just a stub for a regular test method ... ok
 project.zoo.test_fixtures.TestFixture2.test_count ... ok
 project.zoo.test_race.TestDBRace1.test1 ... ok
 project.zoo.test_race.TestDBRace2.test1 ... ok
-project.tests.test_views.test_view_index ... ok
+We're customizing the ROOT_URLCONF with zoo.urls, ... ok
+We're using the standard ROOT_URLCONF, so we need to ... ok
+testcase1 (project.zoo.tests.TestDjango) ... ok
+testcase2 (project.zoo.tests.TestDjango) ... ok
 
 ----------------------------------------------------------------------
-Ran 11 tests in 0.859s
+Ran 14 tests in 1.219s
 
 OK
 Destroying test database...
 
-C:\dev\nosedjango\examples\project>
+

examples/project/settings.py

 # Django settings for 'project' project.
 
+import os
+PROJECT_PATH = os.path.abspath(os.path.split(__file__)[0])
+
 DEBUG = True
 TEMPLATE_DEBUG = DEBUG
 
 MANAGERS = ADMINS
 
 DATABASE_ENGINE = 'sqlite3'           # 'postgresql', 'mysql', 'sqlite3' or 'ado_mssql'.
-DATABASE_NAME = 'zoo.db'             # Or path to database file if using sqlite3.
+DATABASE_NAME = os.path.join(PROJECT_PATH, 'zoo.db')  # Or path to database file if using sqlite3.
 DATABASE_USER = ''             # Not used with sqlite3.
 DATABASE_PASSWORD = ''         # Not used with sqlite3.
 DATABASE_HOST = ''             # Set to empty string for localhost. Not used with sqlite3.
 ROOT_URLCONF = 'project.urls'
 
 TEMPLATE_DIRS = (
+    os.path.join(PROJECT_PATH, 'templates'),
     # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
     # Always use forward slashes, even on Windows.
     # Don't forget to use absolute paths, not relative paths.
     'django.contrib.sites',
     'project.zoo'
 )
+handler500 = 'django.views.defaults.server_error'
+

examples/project/templates/500.html

+blah.  HTTP 500 error

examples/project/tests/__init__.py

-pass

examples/project/tests/test_views.py

-from django.test.client import Client
-
-def test_view_index():
-    c = Client()
-    resp = c.get('/zoo/')
-    assert "Just a title" in resp.content
-    assert "foobar" in resp.content

examples/project/zoo/test_urlconf.py

+from django.test import TestCase
+from django.test.client import Client
+
+class TestStandardUrlConf(TestCase):
+    def test_index(self):
+        '''
+        We're using the standard ROOT_URLCONF, so we need to
+        pass in /zoo/, just the empty string
+        '''
+        c = Client()
+        resp = c.get('')
+        assert resp.status_code == 500
+
+        c = Client()
+        resp = c.get('/zoo/')
+        assert "Just a title" in resp.content
+        assert "foobar" in resp.content
+
+class TestCustomUrlConf(TestCase):
+    urls = 'zoo.urls'
+
+    def test_index(self):
+        '''
+        We're customizing the ROOT_URLCONF with zoo.urls,
+        so we do *not* need to pass in /zoo/, just the empty string
+        '''
+        c = Client()
+        resp = c.get('')
+        assert "Just a title" in resp.content
+        assert "foobar" in resp.content
+
+        c = Client()
+        resp = c.get('/zoo/')
+        assert resp.status_code == 500
+
+
+
+

examples/project/zoo/tests.py

     def testcase1(self):
         zoo = Zoo.objects.create(name='blah')
         assert Zoo.objects.count() == 1
-        import pdb
-        pdb.set_trace()
 
     def testcase2(self):
         zoo = Zoo.objects.create(name='blah')

nosedjango/nosedjango.py

 
         from django.core.management import call_command
         from django.core.urlresolvers import clear_url_caches
+        from django.conf import settings
         call_command('flush', verbosity=0, interactive=False)
 
         if isinstance(test, nose.case.Test) and \
                 # We have to use this slightly awkward syntax due to the fact
                 # that we're using *args and **kwargs together.
                 call_command('loaddata', *test.context.fixtures, **{'verbosity': 0}) 
+
+        if isinstance(test, nose.case.Test) and \
+            isinstance(test.test, nose.case.MethodTestCase) and \
+            hasattr(test.context, 'urls'):
+                # We have to use this slightly awkward syntax due to the fact
+                # that we're using *args and **kwargs together.
+                self.old_urlconf = settings.ROOT_URLCONF
+                settings.ROOT_URLCONF = self.urls
+                clear_url_caches()
+
         self.mail.outbox = []
 
     def finalize(self, result=None):
 
         from django.test.utils import teardown_test_environment
         from django.db import connection
+        from django.conf import settings
         connection.creation.destroy_test_db(self.old_db, verbosity=self.verbosity)   
         teardown_test_environment()
+
+        if hasattr(self, 'old_urlconf'):
+            settings.ROOT_URLCONF = self.old_urlconf
+            clear_url_caches()
+
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.