Open hendrik1120 opened 2 months ago
I created a demo app to replicate this issue with minimal dependencies: https://github.com/hendrik1120/test_partials The tests also fail using only the django built in tests, pytest and pytest-django are optional.
I believe it is because of performance as shown here. I also had the need to analyze the context and I couldn't.
I think that only applies if you are using a partial in a for loop or using the with tag to modify the context. This must be unintentional, as returning the current context shouldn't be expensive at all. Otherwise, this would be a huge dealbreaker and should be at the top of the README.
Hi @hendrik1120 — thanks for the report. Yes, partials aren't currently firing the template_rendered
signal during test runs, that the Django test runner patches into Template
itself.
This means (in particular) that the context
and templates
attributes that the test client adds will be missing.
Until we can merge into Django (refs #29) it's likely a test runner subclass overriding the appropriate hook would be needed here. Would you fancy having a look at that?
Hi, thanks for the explanation. To be honest, that's quite a bit above my django skill level currently. Looking forward to this getting merged into Django.
@hendrik1120 For the moment I'd suggest just asserting the response content, which has what you're expecting. HTH.
Hello, thank you for creating this app. I have been playing around with it for a few days now, but I am unable to test the returned context from a view when rendering a template partial.
The view function looks like this:
The test looks like this:
The test will always fail with this error:
Which means that
reponse.context
is always None when running the test. If I change the template from "pages/alarms.html#alarm_list" to e.g., "base.html" the test passes just fine. The view works just fine in the browser, this just occurs during testing.I am not really sure if this is a bug, please let me know if there is a better way to test this.
Edit: I installed template-partials by just adding it to
INSTALLED_APPS
without the advanced setup.