Replace _get_url abstract test method with a pytest fixture#751
Conversation
|
@AdrianDAlessandro I needed to implement this to do tests where we needed to pass url params to the Also there was a failing test on the main branch. I'm not sure if this is just local to me. |
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
AdrianDAlessandro
left a comment
There was a problem hiding this comment.
I'm not sure exactly what this is trying to fix. The issue says it's to be able to pass arguments to the _get_url method, but that is already done in some places. I've commented on the parts that do it.
| def _get_url(self, **kwargs): | ||
| return reverse("skill_detail", kwargs=kwargs) |
There was a problem hiding this comment.
Is what you are trying to do achieved by this existing pattern?
| def test_template_used(self, admin_client, url): | ||
| """Test the correct template is used by the GET request.""" | ||
| with assertTemplateUsed(template_name=self._template_name): | ||
| response = admin_client.get(self._get_url(slug=skill.slug)) |
There was a problem hiding this comment.
Results in being able to pass args to reverse like this
The existing method works if you are calling The use case I couldn't implement is when the url should use params that are defined from mocked database instance objects. Edit: class BS4Mixin(ABC):
"""Mixin for tests that use BeautifulSoup4. It makes the `soup` fixture available.
Note: Using this requires the test class to define:
- A `_get_url` method
"""
@abstractmethod
def _get_url(self) -> str:
return NotImplemented
@pytest.fixture
def soup(self, client) -> BeautifulSoup:
"""A fixture of the BeautifulSoup4 object of the requested page."""
response = client.get(self._get_url()) # << This does not allow passing args
return BeautifulSoup(response.content, "html.parser")
... |
Description
Replaces the use of the
_get_urlabstract method with adef url(self)...fixture. This allows accessing other fixture data such as database mocked instances.Fixes #750
Type of change
Key checklist
python -m pytest)mkdocs serve)pre-commit run --all-files)Further checks