DemocracyClub / dc_django_utils

Shared helpers and utility functions for DC websites
MIT License
0 stars 2 forks source link

Improve front end testing set up #6

Closed symroe closed 1 year ago

symroe commented 3 years ago

There are a few things in this, but I thought one issue would be useful as a jumping off point for other issues and thoughts.

The problem

At the moment it's too easy to make a mistake that causes invalid HTML to make it to production. It's sometimes hard to review PRs for valid HTML due to the nature of the commits, and even if you can parse the HTML in a diff, Django logic or loops might result in invalid HTML when rendered.

The solution(s)

pytest fixture for validating HTML5

https://pypi.org/project/pytidylib/ is a library that wraps tidy — a tool for validating (and indenting, but that's a different thing) HTML documents.

We can include this in a pytest helper function that works a bit like:

from tidylib import tidy_document

def validate_html(client):
    response = client.get('/')
    content = response.content
    document, errors = tidy_document(content)
    assert errors == None

This is untested, but should more or less work.

Add as a feature to DC Django Utils

The above test can be packaged up in to a reusable test that we can add to DC Django Utils. This would all each project to do something like:

from dc_utils.tests import validate_html

urls = ("/", "/about/", reverse("some_url"))

@pytest.mark.parametrize("url", urls)
def test_urls(url):
    assert validate_html(url)

Implement

Once we have a nice testing interface, let's set up lists of testing URLs in each project that uses DC Utils and implement this test. We might not need to test 100% of URLs on a project, but a decent sample of the most complex or common pages should help us catch a load of errors.

VirginiaDooley commented 1 year ago

Completed with https://github.com/DemocracyClub/dc_django_utils/pull/12