Как тестируется шаблон вне цикла "запрос-ответ"?

Как проверяется шаблон и его контекст при изолированном тестировании? Если быть точным, этот шаблон включается в другой с помощью тега {% include %}. Я использовал два разных тестовых утверждения, но они выдают такие ошибки утверждения:

  • self.assertInHTML => AssertionError: False is not true : Couldn't find 'id=question_id_1' in response

  • self.assertIn => AssertionError: 'id=question_id_1' not found in '\n<div id="question_id_1">\n\n</div>\n'

class TestUserQuestionProfileTemplate(TestCase):

    @classmethod
    def setUpTestData(cls):
        tag1 = Tag.objects.create(name="Tag1")
        tag2 = Tag.objects.create(name="Tag2")
        user = get_user_model().objects.create_user("ItsNotYou")
        profile = Profile.objects.create(user=user)
        question = Question.objects.create(
            title="Test Question ZZZ",
            body="Post content detailing the problem about Test Question ZZZ",
            profile=profile
        )
        question.tags.add(*[tag1, tag2])
        cls.template = render_to_string(
            "authors/questions.html", {"question": question}
        )


    def test_template_user_questions_listing(self):
        self.assertInHTML("id=question_id_1", self.template)
        self.assertIn("id=question_id_1", self.template)

authors/questions.html

<div id="question_id_{{ question.id }}">

</div>

Вернуться на верх