亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

ModelForm 和單元測試的奇怪問題:TypeError: getattr():

ModelForm 和單元測試的奇怪問題:TypeError: getattr():

慕娘9325324 2021-10-26 15:53:10
我不知道這里會發生什么:模型.pyclass Sound(models.Model):    SEX_CHOICES = (            ('M', 'Male'),            ('F', 'Female')    )    phrase1 = models.CharField(max_length=300)    phrase2 = models.CharField(max_length=300)    language = models.CharField(max_length=50)    num_words = models.PositiveSmallIntegerField(verbose_name="number of words")    num_syllables = models.PositiveSmallIntegerField(verbose_name="number of syllables")    sex = models.CharField(max_length=1, choices=SEX_CHOICES)    voice = models.BooleanField(verbose_name="synthetic voice")    speaker_name = models.CharField(max_length=50)    sound_hash = models.CharField(max_length=40, primary_key=True, editable=False)    key = models.CharField(max_length=200, editable=False, default="", null=True, help_text="Amazon S3 key")表格.pyfrom django import formsclass LessonSoundForm(forms.ModelForm):    class Meta:        model = Sound        fields = ["phrase1", "phrase2", "num_words", "num_syllables"]測試.pyfrom django.test import TestCasefrom forms import LessonSoundFormclass LessonSoundForm(TestCase):    def setUp(self):        self.sound_data = {                "phrase1": "Poop",                "phrase2": "Knuckle",                "num_words": 1,                "num_syllables": 1        }    def test_lesson_sound_form_validates_with_good_data(self):        form = LessonSoundForm(data=self.sound_data)        self.assertTrue(form.is_valid())這看起來非常簡單,所有其他測試都使用ModelForm. 但是,我收到此錯誤:line 20, in test_lesson_sound_form_validates_with_good_data        form = LessonSoundForm(data=self.sound_data)    TypeError: __init__() got an unexpected keyword argument 'data'如果我在沒有data參數的情況下運行它,我會得到:    form = LessonSoundForm(self.sound_data)  File "/usr/lib/python3.6/unittest/case.py", line 397, in __init__    testMethod = getattr(self, methodName)TypeError: getattr(): attribute name must be string這似乎是一個非?;镜陌咐?,我不知道會出現什么問題。我認為這只是一天的結束,我錯過了一些愚蠢的事情。
查看完整描述

1 回答

?
RISEBY

TA貢獻1856條經驗 獲得超5個贊

from forms import LessonSoundForm

                  ^^^^^^^^^^^^^^^

class LessonSoundForm(TestCase):

      ^^^^^^^^^^^^^^^

LessonSoundForm由于相同的 name ,當您為測試用例定義新類時,您正在覆蓋。


查看完整回答
反對 回復 2021-10-26
  • 1 回答
  • 0 關注
  • 228 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號