Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test Views Render Templates #1182

Open
ghickman opened this issue Oct 20, 2021 · 8 comments
Open

Test Views Render Templates #1182

ghickman opened this issue Oct 20, 2021 · 8 comments

Comments

@ghickman
Copy link
Contributor

#1181 showed that we weren't testing the configured template existed. Ideally we wouldn't require each view had a unit test for this as it's easily prone to human error.

Can we add a test with subtests which tries to exercise response.rendered_content for every view that responds to GET and with a 200?

@inglesp
Copy link
Contributor

inglesp commented Oct 20, 2021

Ideally we wouldn't require each view had a unit test for this as it's easily prone to human error.

Are you saying that request factory tests don't actually render the template?

@ghickman
Copy link
Contributor Author

Correct, you have to access rendered_content to get the rendered output.

@inglesp
Copy link
Contributor

inglesp commented Oct 20, 2021

Yikes. Feels like a bit of a shortcoming in request factories. Could we make our own request factory that automatically calls rendered_content?

@ghickman
Copy link
Contributor Author

If you're used to using Client, maybe? RequestFactory generates a request, nothing more. You then use that to test a view with. Views, GCBVs in particular, do not render content by default, they instantiate the configured response class (typically TemplateResponse) and return that instance.

I'd rather we didn't have every unit test render templates, that's going to eat up quite a bit of test time.

@inglesp
Copy link
Contributor

inglesp commented Oct 20, 2021

Could we make our own request factory that automatically calls rendered_content?

I see now that this couldn't work!

Have you seen https://github.com/nedbat/django_coverage_plugin?

@ghickman
Copy link
Contributor Author

I haven't, happy to give it a go though!

@inglesp
Copy link
Contributor

inglesp commented Oct 20, 2021

FWIW I just ran:

sed -i 's/status_code == 200/rendered_content is not None/' tests/unit/*/views/*

This changed 95 tests, and the time to run the full suite did not change.

@ghickman
Copy link
Contributor Author

That is very unexpected, but good to know, can you do that same sed, but add the rendered content check after the 200 so we get both?

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

No branches or pull requests

2 participants