Expected view to be called with a URL keyword argument named "pk" : <p>I'm writing a test for a Django Rest Framework view following closely the <a href="http://www.django-rest-framework.org/api-guide/testing/" rel="noreferrer">testing documentation</a></p>

<p>Here's my simple test: </p>

<pre><code>def test_patient_detail_api_opens(self):
    factory = APIRequestFactory()
    view =PatientDetailApi.as_view()
    request = factory.get(reverse('api_pacjent', kwargs={'pk' :1}))
    force_authenticate(request, user=self.user)
    response = view(request)
    self.assertEqual(response.status_code, 200)
</code></pre>

<p>This test fails with the following message: </p>

<pre><code>AssertionError: Expected view PatientDetailApi to be called with a URL keyword argument named "pk". Fix your URL conf, or set the `.lookup_field` attribute on the view correctly.
</code></pre>

<p>I fail to understand why this is happening and how to fix this. </p>

<ul>
<li>The <code>pk</code> kwargs is there in the URL, </li>
<li>according to the docs there's no need to add the <code>lookup-field</code> value explicitly if it defaults to <code>pk</code>, </li>
<li>the view opens correctly and yet this test fails... </li>
</ul>

<p><strong>Can somebody please explain why this error occurs?</strong> </p>

<p>Here's the relevant code:</p>

<p>the 'main' <code>url.py</code>:</p>

<pre><code>urlpatterns = [
    url(r'^pacjent/', include('pacjent.urls')),
] 
</code></pre>

<p><code>pacjent.urls</code> looks like this:</p>

<pre><code>url(r'^api/szczegoly/(?P&lt;pk&gt;\d+)/$', PatientDetailApi.as_view(), name="api_pacjent"),
</code></pre>

<p>And <code>PatientDetailApi</code> is this:</p>

<pre><code>class PatientDetailApi(generics.RetrieveUpdateAPIView):
    model = Patient
    serializer_class = PatientDetailsSerializer
    queryset = Patient.objects.all()

    authentication_classes = (SessionAuthentication, BasicAuthentication)
    permission_classes = (IsAuthenticated,) 
</code></pre>
