Django Integration tests for urls
<p>I have a django app I wanted to write tests for. For now Im writing integration tests for the urls. </p>

<p>For my <code>signin</code> test , my url looks like:
<code>url(r'^signin/$', login_forbidden(signin), name='signin')</code></p>

<p>and my test looks like:</p>

<pre><code>from django.test import TestCase


class SigninTest(TestCase):
    def test_signin(self):
        resp = self.client.get('/signin/')
        self.assertEqual(resp.status_code, 200)
</code></pre>

<p>However I have no idea to test a a longer url, for instance I have one entry in urls like:</p>

<pre><code>url(
        r'^ad_accounts/(?P&lt;ad_account_id&gt;[^/]+)/$',
        AdAccountDetailView.as_view(),
        name='campaigns'
    ),
</code></pre>

<p>If I repeat the above test I have for the signin page (replacing <code>resp = self.client.get('/ad_accounts/')</code>) returns a failure</p>

<pre><code>======================================================================
FAIL: test_signin (engineoftravel.tests.SigninTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/project/tests.py", line 7, in test_signin
    self.assertEqual(resp.status_code, 200)
AssertionError: 302 != 200

----------------------------------------------------------------------
Ran 1 test in 0.103s

FAILED (failures=1)
</code></pre>

<p>why not use reverse: <a href="https://docs.djangoproject.com/en/1.10/ref/urlresolvers/#reverse" rel="nofollow">https://docs.djangoproject.com/en/1.10/ref/urlresolvers/#reverse</a></p>

<pre><code>from django.core.urlresolvers import reverse

....
resp = self.client.get(reverse('campaigns', args=[1]))
</code></pre>

<p>where args is the id you need to pass in.</p>

<p>EDIT: since django 1.10 reverse imports from django.urls</p>

python django integration-testing