Skip to main content

Html assertions is a simple django testing mixin that makes it possible to assert that a given block of html contains a tag (optionally with a given class) that also contains the specified textual content.

pip install django_html_assertions

Usage

To use the new test assertion methods all you need to do is mix the mixin into your test suite. You’re then ready to go.

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'
            )

Release files for django_html_assertions 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django_html_assertions 0.1.0
File Size Uploaded
django_html_assertions-0.1.0.tar.gz 3.2 kB Details

Release files / django_html_assertions-0.1.0.tar.gz

Download URL django_html_assertions-0.1.0.tar.gz
Size 3.2 kB
Tags Source
SHA-256 checksum
How to use checksums
62d7393a0109df941fd10cfc5ed90e8bfba9f3fa0ff86e273c1bd0b3d985f823
BLAKE2b-256 checksum
How to use checksums
2e483a57f834f7111f6d5d9694c05f3fb708df305eefb186d7c404c4d1f1238f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.1.0 This release

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page