title: URL resolution issues in Django Tests
body: <p>I've written a django model that has a get_absolute_url method, with unit tests to make sure everything is kosher. The get_absolute_url tests pass no problem. The get_absolute_url method is written as so</p>
  
  <pre><code>def get_absolute_url(self):
      return reverse("scheduling:klass", args=[self.pk])
  </code></pre>
  
  <p>This is the url routing to give context about how the urls flow.</p>
  
  <pre><code>#main.urls
  urlpatterns = [
      url(r'^$', homepage, name="home"),
      url(r'^scheduling/', include('scheduling.urls', namespace="scheduling")),
      url(r'^profile/', include('user_profile.urls', namespace="profile")),
      url(r'^accounts/', include('allauth.urls')),
      url(r'^admin/', admin.site.urls),
  ]
  
  #scheduling.urls
  urlpatterns = [
      url(r'add-class/$', views.add_klass, name="add_klass"),
      url(r'class/(?P&lt;pk&gt;\d+)/$', views.klass, name="klass"),
  ]
  </code></pre>
  
  <p>I'm testing the klass view (it's basically just a klass detail view) as follows</p>
  
  <pre><code>def test_klass_detail_template_renders(self):
      print(self.klass.pk)
      response = self.client.get(self.klass.get_absolute_url())
      self.assertTemplateUsed(response, "scheduling/klass.html")
  </code></pre>
  
  <p>The test fails with the following stacktrace.</p>
  
  <pre><code>======================================================================
  ERROR: test_klass_detail_template_renders (scheduling.tests.test_views.KlassViewTests)
  ----------------------------------------------------------------------
  Traceback (most recent call last):
    File "/Users/me/.virtualenvs/myproject/lib/python3.5/site-packages/django/core/urlresolvers.py", line 586, in reverse
      extra, resolver = resolver.namespace_dict[ns]
  KeyError: 'myproject.profile'
  
  During handling of the above exception, another exception occurred:
  
  Traceback (most recent call last):
    File "/Users/me/.virtualenvs/myproject/lib/python3.5/site-packages/django/template/defaulttags.py", line 507, in render
      current_app=current_app)
    File "/Users/me/.virtualenvs/myproject/lib/python3.5/site-packages/django/core/urlresolvers.py", line 596, in reverse
      key)
  django.core.urlresolvers.NoReverseMatch: 'myproject.profile' is not a registered namespace
  
  During handling of the above exception, another exception occurred:
  
  Traceback (most recent call last):
    File "/Users/me/Documents/Development/myproject/src/scheduling/tests/test_views.py", line 60, in test_klass_detail_template_renders
      response = self.client.get(self.klass.get_absolute_url())
    File "/Users/me/.virtualenvs/myproject/lib/python3.5/site-packages/django/test/client.py", line 503, in get
      **extra)
    File "/Users/me/.virtualenvs/myproject/lib/python3.5/site-packages/django/test/client.py", line 304, in get
      return self.generic('GET', path, secure=secure, **r)
  
  ...
      File "/Users/me/.virtualenvs/myproject/lib/python3.5/site-packages/django/core/urlresolvers.py", line 508, in _reverse_with_prefix
          (lookup_view_s, args, kwargs, len(patterns), patterns))
      django.core.urlresolvers.NoReverseMatch: Reverse for 'profile' with arguments '('',)' and keyword arguments '{}' not found. 2 pattern(s) tried: ['profile/$', 'profile/(?
      P&lt;username&gt;[\\w+-.@]+)/$']
  </code></pre>
  
  <p>It implies that that get_absolute_url is passing the pk value of the klass model to the url pattern but it works when I test it manually and it works in literally every other situation.</p>
  
  <p>This...is...a...head scratcher. Any help is appreciated.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I think it might have to do with test isolation. I've done some print debugging and found that each test creates a new instance of the klass model.</p>
  
  <p>Here are my setUp and tearDown methods. </p>
  
  <pre><code>class KlassViewTests(TestCase):
  def setUp(self):
      self.factory = ClassMateFactory()
      self.teacher = self.factory.create_teacher("Maria")
      self.linked_student = self.factory.create_student(
          "Milhouse",
          self.teacher.profile
      )
      self.klass = self.factory.create_klass(
          "Test Klass",
          teachers=self.teacher.profile,
          students=self.linked_student.profile
      )
      self.data = {
          "name": "New Test Class",
          "status": "A",
          "students": [self.linked_student.profile.pk],
          "teachers": [self.teacher.profile.pk]
      }
      super().setUp()
  
  def tearDown(self):
      print(self.klass.get_absolute_url())
      self.teacher.delete()
      self.linked_student.delete()
      self.klass.delete()
      super().tearDown()
  </code></pre>
  
  <p>Hopefully this helps clear things up.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Added the url conf to provide more context</p>