Repository: wagtail/wagtail
File: wagtail/admin/tests/test_templatetags.py (Python)

Code before:
        rendered = Template(template).render(Context({"request": self.request}))

        soup = self.get_soup(rendered)
        meta_tag = soup.find("meta", {"name": "color-scheme"})
        self.assertIsNotNone(meta_tag)
        self.assertEqual(meta_tag["content"], "light")


class FormattedfieldTagTestCase(SimpleTestCase):
    def render_template(self, template, field_name="title", **context):
        class BasicForm(forms.Form):
            title = forms.CharField()

        form = BasicForm(data={})

        html = Template("{% load wagtailadmin_tags %}" + template).render(
            Context(
                {
                    "field": form[field_name],
                    **context,
                }
            )
        )

        # Parse the rendered HTML for the first DOM element in the output
        return BeautifulSoup(html, "html.parser").find()

    def test_basic_usage_with_full_html(self):
        soup = self.render_template("{% formattedfield field=field %}")

        # check the outer wrapper attributes
        self.assertEqual(
            {"class": ["w-field__wrapper"], "data-field-wrapper": ""},
            soup.attrs,
        )

        # check the label is correct
        label = soup.find("label")
        self.assertIsNotNone(label)
        self.assertEqual(label["for"], "id_title")
        self.assertEqual(label["id"], "id_title-label")
        self.assertEqual(label.get_text().strip(), "Title*")

        # check there is an error container
        errors = soup.find("div", {"data-field-errors": ""})
        self.assertIsNotNone(errors)
        self.assertEqual(errors["class"], ["w-field__errors"])
        self.assertEqual(errors.get_text().strip(), "This field is required.")

        # check there is a help container that is empty
        help_text = soup.find("div", {"data-field-help": ""})

Review comment (suggestion):
Make the test class inherit from `WagtailTestUtils` (in addition to `SimpleTestCase`) and use `self.get_soup` instead

```suggestion
        return self.get_soup(html).find()
```

Code after:

        soup = self.get_soup(rendered)
        meta_tag = soup.find("meta", {"name": "color-scheme"})
        self.assertIsNotNone(meta_tag)
        self.assertEqual(meta_tag["content"], "light")


class FormattedfieldTagTestCase(WagtailTestUtils, SimpleTestCase):
    def render_template(self, template, field_name="title", **context):
        class BasicForm(forms.Form):
            title = forms.CharField()

        form = BasicForm(data={})

        html = Template("{% load wagtailadmin_tags %}" + template).render(
            Context(
                {
                    "field": form[field_name],
                    **context,
                }
            )
        )

        # Parse the rendered HTML for the first DOM element in the output
        return self.get_soup(html).find()

    def test_basic_usage_with_full_html(self):
        soup = self.render_template("{% formattedfield field=field %}")

        # check the outer wrapper attributes
        self.assertEqual(
            {"class": ["w-field__wrapper"], "data-field-wrapper": ""},
            soup.attrs,
        )

        # check the label is correct
        label = soup.find("label")
        self.assertIsNotNone(label)
        self.assertEqual(label["for"], "id_title")
        self.assertEqual(label["id"], "id_title-label")
        self.assertEqual(label.get_text().strip(), "Title*")

        # check there is an error container
        errors = soup.find("div", {"data-field-errors": ""})
        self.assertIsNotNone(errors)
        self.assertEqual(errors["class"], ["w-field__errors"])
        self.assertEqual(errors.get_text().strip(), "This field is required.")

        # check there is a help container that is empty
        help_text = soup.find("div", {"data-field-help": ""})
        self.assertIsNotNone(help_text)