Skip to content

Add beautiful soup 4 dependency with example test#623

Draft
sbland wants to merge 2 commits intomainfrom
593-add-more-specific-html-testing-for-views
Draft

Add beautiful soup 4 dependency with example test#623
sbland wants to merge 2 commits intomainfrom
593-add-more-specific-html-testing-for-views

Conversation

@sbland
Copy link
Contributor

@sbland sbland commented Mar 9, 2026

Description

Implementing beautiful soup to allow HTML testing of views.

Fixes #593

Type of change

  • Documentation (non-breaking change that adds or improves the documentation)
  • New feature (non-breaking change which adds functionality)
  • Optimization (non-breaking, back-end change that speeds up the code)
  • Technical work (non-breaking, change which is work as part of a new feature)
  • Bug fix (non-breaking change which fixes an issue)
  • Breaking change (whatever its nature)

Key checklist

  • All tests pass (eg. python -m pytest)
  • The documentation builds and looks OK (eg. mkdocs serve)
  • Pre-commit hooks run successfully (eg. pre-commit run --all-files)

Further checks

  • Code is commented, particularly in hard-to-understand areas
  • Tests added or an issue has been opened to tackle that in the future. (Indicate issue here: # (issue))

@sbland
Copy link
Contributor Author

sbland commented Mar 9, 2026

One issue I'm having is beautiful soup not being able to find elements unless we strip the text. See util function implemented below to get around this:


def find_stripped_text(doc, tag, text):
    """Helper function to find the stripped text of a tag."""
    return next(
        a
        for a in doc.find_all(tag)
        if re.search(rf"{text}", a.get_text(" ", strip=True))
    )

- This is added as a proof of concept and needs some refinement
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AdrianDAlessandro I've added some tests here trying out user driven testing but I need to dig into how the other tests are working for mocking the db etc.

Copy link
Collaborator

@AdrianDAlessandro AdrianDAlessandro Mar 11, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The DB is currently mocked using the pytest fixtures in main/conftest.py. This might change if we move to using data migrations to populate the DB (see #598). You also need to mark the tests that are going to use the DB with @pytest.mark.django_db.

What might be the best approach here is to use a class-based test that has a setup method that spins up and populates the database and then mark that class as using the DB, then all subsequent tests inherit from that class.

@Sahil590 Sahil590 requested review from AdrianDAlessandro and dalonsoa and removed request for AdrianDAlessandro and dalonsoa March 10, 2026 08:12
Copy link
Collaborator

@AdrianDAlessandro AdrianDAlessandro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the idea of implementing some user-driven testing, although I think maybe it should be in a separate PR to the unit tests on individual views. It is also lower priority, particularly because I think we might be changing some of the user workflows (like the self-assessment form, etc).

(FYI, I'm going to mark this PR as a draft for now)

Copy link
Collaborator

@AdrianDAlessandro AdrianDAlessandro Mar 11, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The DB is currently mocked using the pytest fixtures in main/conftest.py. This might change if we move to using data migrations to populate the DB (see #598). You also need to mark the tests that are going to use the DB with @pytest.mark.django_db.

What might be the best approach here is to use a class-based test that has a setup method that spins up and populates the database and then mark that class as using the DB, then all subsequent tests inherit from that class.

"""Helper function to navigate to the registration page."""
response = go_to_homepage(client)
soup = BeautifulSoup(response.content, "lxml")
print(soup.prettify())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I presume this print is temporary for while debugging?

return next(
a
for a in doc.find_all(tag)
if re.search(rf"{text}", a.get_text(" ", strip=True))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the point of using BS4 is to not need to do any regex. Looking at the example PR I linked in the issue, they needed to put IDs on many of the html objects in order to test them. So maybe that gets around this?

@AdrianDAlessandro AdrianDAlessandro marked this pull request as draft March 11, 2026 14:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add more specific HTML testing for views

2 participants