我正在為我的“答案檢查器”撰寫一個測驗,所以我做了一個測驗問題,我想通過給出正確的答案來測驗結果檢查器是否有效(所以測驗應該通過)。
我遇到了回傳的錯誤的麻煩。我知道代碼不是很好,但我是這個領域的新手。我想我在邏輯上誤解了一些東西。有人可以幫忙嗎?:)
錯誤是:
correct = QuestionMultipleChoice.correct_answer()
TypeError: 'property' object is not callable
測驗(我知道最后幾行是錯誤的):
@when(u'I give an answer to a Multiple choice question')
def save_object(context):
lab1 = Lab.objects.create(lab_name="testlab", pub_date=datetime.now(), lab_theory="test theory")
question1 = QuestionMultipleChoice.objects.create(lab=lab1, question='This is a test question', option1='1', option2='2', option3='3', option4='4', answer=1)
@then(u'I should get true if answer is correct')
def should_have_only_one_object(self):
given = 1
correct = QuestionMultipleChoice.correct_answer()
QuestionMultipleChoice.check_answer(correct, given)
assert 1 == Lab.objects.count()
模型.py
class QuestionMultipleChoice(models.Model):
lab = models.ForeignKey(Lab, on_delete=models.CASCADE)
type = QuestionType.multiplechoice
question = models.CharField(max_length=200,null=True)
option1 = models.CharField(max_length=200,null=True)
option2 = models.CharField(max_length=200,null=True)
option3 = models.CharField(max_length=200,null=True)
option4 = models.CharField(max_length=200,null=True)
answer = models.IntegerField(max_length=200,null=True)
def __str__(self):
return self.question
@property
def html_name(self):
return "q_mc_{}".format(self.pk)
@property
def correct_answer(self):
correct_answer_number = int(self.answer)
correct_answer = getattr(self, "option{}".format(correct_answer_number))
return correct_answer
def check_answer(self, given):
return self.correct_answer == given
更新:
我遵循了你的提示(謝謝),但仍然有一些問題(同樣的問題),另外,如果它是在另一個函式中定義的,我如何訪問該物件?
@when(u'I save the question in the lab 1')
def save_object(context):
lab1 = Lab.objects.create(lab_name="testlab", pub_date=datetime.now(), lab_theory="test theory")
question1 = QuestionMultipleChoice.objects.create(lab=lab1, question='This is a test question', option1='1', option2='2', option3='3', option4='4', answer=1)
@then(u'I should get true if answer is correct')
def should_have_only_one_object(self):
given = 1
correct = QuestionMultipleChoice.correct_answer
question1.check_answer(correct, given)
assert 1 == Lab.objects.count()
uj5u.com熱心網友回復:
您只需要洗掉后面的括號,correct_answer因為它是一個屬性,而不是一個方法。
嘗試:
@then(u'I should get true if answer is correct')
def should_have_only_one_object(self):
given = 1
correct = QuestionMultipleChoice().correct_answer
QuestionMultipleChoice.check_answer(correct, given)
assert 1 == Lab.objects.count()
更新
這是使用 django 的 TestCase 的簡單實作。 在檔案中查看更多
from django.tests import TestCase
class QuestionTestCase(TestCase):
@classmethod
def setUpTestData(cls):
cls.lab = Lab.objects.create(lab_name="testlab", pub_date=datetime.now(), lab_theory="test theory")
cls.question = QuestionMultipleChoice.objects.create(lab=lab1, question='This is a test question', option1='1', option2='2', option3='3', option4='4', answer=1)
cls.answer = cls.question.correct_answer
def test_check_answer(given=1):
self.assertTrue(self.question.check_answer(self.answer, given))
def test_lab_count():
self.assertEqual(Lab.objects.count(), 1)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/391802.html
標籤:Python 姜戈 单元测试 django 模型 蟒蛇行为
上一篇:方法內部的角度測驗分支
