一个简单的django应用程序,用于向djangos测试类添加与html相关的断言。
django_html_assertions的Python项目详细描述
HTML断言是一种简单的Django测试混合,它可以断言给定的HTML块包含一个标签(可选地与给定的类),该标签也包含指定的文本内容。
pip install django_html_assertions
用法
< >使用新的测试断言方法,您需要做的就是将MIXIN混合到您的测试套件中。你就可以走了。from django.test import TestCase from django_html_assertions.mixins import HTMLContainsMixin TEST_HTML = """ <html> <body> <p class="odd">Item 1</p> <p class="even">Item 2</p> <p class="odd">Item 3</p> </body> </html> """ class HTMLContainsMixinTestCase(HTMLContainsMixin, TestCase): """ Tests the test class mixin """ def test_assert_html_tag_contains_valid(self): """ The method should not raise an exception """ self.assertHTMLTagContains( TEST_HTML, 'Item 1', 'p' ) def test_assert_html_tag_contains_valid_with_class(self): """ The method should not raise an exception """ self.assertHTMLTagContains( TEST_HTML, 'Item 1', 'p', tag_class='odd' ) def test_assert_html_tag_contains_invalid(self): """ The method should raise an exception """ with self.assertRaises(AssertionError): self.assertHTMLTagContains( TEST_HTML, 'Item 10', 'p' ) def test_assert_html_tag_contains_invalid_with_class(self): """ The method should not raise an exception """ with self.assertRaises(AssertionError): self.assertHTMLTagContains( TEST_HTML, 'Item 1', 'p', tag_class='even' ) def test_assert_html_tag_not_contains_valid(self): """ The method should not raise an exception """ self.assertHTMLTagNotContains( TEST_HTML, 'Item 10', 'p' ) def test_assert_html_tag_not_contains_valid_with_class(self): """ The method should not raise an exception """ self.assertHTMLTagNotContains( TEST_HTML, 'Item 1', 'p', tag_class='even' ) def test_assert_html_tag_not_contains_invalid(self): """ The method should raise an exception """ with self.assertRaises(AssertionError): self.assertHTMLTagNotContains( TEST_HTML, 'Item 1', 'p' ) def test_assert_html_tag_not_contains_invalid_with_class(self): """ The method should not raise an exception """ with self.assertRaises(AssertionError): self.assertHTMLTagNotContains( TEST_HTML, 'Item 1', 'p', tag_class='odd' )