我正在為一個 url 撰寫測驗,問題是當我嘗試傳遞多個引數時它失敗了,這里有一些代碼:
#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
當我運行測驗時,它回傳:
.E
======================================================================
ERROR: test_new_cardio_testing (cardiotesting.tests.test_urls.TestUrls)
----------------------------------------------------------------------
TypeError: TestUrls.test_new_cardio_testing() missing 1 required positional argument: 'protocol'
但是當只有一個引數時,測驗成功。感謝任何幫助。
uj5u.com熱心網友回復:
測驗方法只需要一個引數,那就是self,所以你的測驗類必須是這樣的:
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))
uj5u.com熱心網友回復:
您的 urlpatterns 似乎不適合接受這種格式。嘗試以下操作:
urlpatterns = [
path('new/<str:id_patient>/<str:protocol>/', views.new_cardio_testing, name='new_cardio_testing'),
# Django loves it's trailing slashes.
]
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/504764.html
上一篇:運行松露測驗時出現斷言錯誤