为什么使用Django的assertInHTML()方法的测试用例失败?

2024-04-24 20:53:35 发布

您现在位置:Python中文网/ 问答频道 /正文

给出下面的Django测试用例,为什么失败?同样重要的是,我可以做些什么来测试HTML元素<form class="bc-form" id="update_template" action="" method="get" novalidate>的存在?在

from django.test import TestCase
class Test_assertInHTML(TestCase):
    def test_something(self):
        needle = '<form class="bc-form" id="update_template" action="" method="get" novalidate>'
        haystack = '''<html>
                <form class="bc-form" id="update_template" action="" method="get" novalidate>
                    <table>
                        <tbody>
                            <tr><th>Template Name </th><td><select name="name" id="id_name"></select></td></tr>
                            <tr><th>Format </th><td><select name="format" id="id_format">
                                <option value="E">Email</option>
                                <option value="S">SMS</option>
                                </select>
                            </td></tr>
                            <tr><th>The message </th><td><textarea name="message" cols="40" rows="10" required id="id_message"></textarea></td></tr>
                        </tbody>
                    </table>
                    <input type="submit" value="Submit">
                </form>
            </html>'''
        self.assertInHTML(needle, haystack, count=1)

Tags: nameformidgetupdatetemplateactionselect
1条回答
网友
1楼 · 发布于 2024-04-24 20:53:35

你的针是一个不完整的HTML片段,这不是有效的HTML。在

documentation for ^{}声明这两个参数(needle和haystack)必须是有效的HTML。解析器首先验证HTML并丢弃任何无效的内容—因此测试失败。在

如果您只想测试是否呈现了开始的表单标记,那么您将需要进行简单的字符串比较,而不是使用assertInHTML。在

相关问题 更多 >