Add beautiful soup 4 dependency with example test#623
Conversation
|
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: |
- This is added as a proof of concept and needs some refinement
There was a problem hiding this comment.
@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.
There was a problem hiding this comment.
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.
AdrianDAlessandro
left a comment
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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()) |
There was a problem hiding this comment.
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)) |
There was a problem hiding this comment.
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?
Description
Implementing beautiful soup to allow HTML testing of views.
Fixes #593
Type of change
Key checklist
python -m pytest)mkdocs serve)pre-commit run --all-files)Further checks