I'm writing a test for an url, problem is it fails when I try to pass multiple arguments, here is some code:
#test_urls.py
from django.test import SimpleTestCase
from django.urls import reverse, resolve
from cardiotesting.views import *
class TestUrls(SimpleTestCase):
def test_new_cardio(id_patient, protocol):
id_patient = '05'
protocol = 'fox'
url = reverse('new_cardio_testing', args=[id_patient, protocol])
print(resolve(url))
#urls.py
from django.urls import path
from . import views
urlpatterns = [
path('new/<str:id_patient>', views.new_cardio_testing, name='new_cardio_testing'),
]
#views.py
def new_cardio_testing(id_patient, protocol):
pass
When I run the test it returns:
.E
======================================================================
ERROR: test_new_cardio_testing (cardiotesting.tests.test_urls.TestUrls)
----------------------------------------------------------------------
TypeError: TestUrls.test_new_cardio_testing() missing 1 required positional argument: 'protocol'
But when there is only one argument the test succeeds. Appreciate any help.
CodePudding user response:
test methods just take one parameter and that is self, so your test class must be something like this:
class TestUrls(SimpleTestCase):
def test_new_cardio(self):
id_patient = '05'
protocol = 'fox'
url = reverse('new_cardio_testing', args=[id_patient, protocol])
print(resolve(url))
CodePudding user response:
Your urlpatterns seem unfit to accept this format. Try the following:
urlpatterns = [
path('new/<str:id_patient>/<str:protocol>/', views.new_cardio_testing, name='new_cardio_testing'),
# Django loves it's trailing slashes.
]