Django 测试:确定执行了哪个视图
在 Django 测试文档中,他们承诺您可以“测试是否针对给定 URL 执行了正确的视图”。
但是我没有找到任何如何测试执行哪个视图的可能性。我希望在 Response< /code> class
但执行视图没有任何内容。
提前致谢。
In the Django testing documentation they promise that you can "Test that the correct view is executed for a given URL."
However I didn't find any possibility how to test which view was executed. I would expect that in the Response
class but there's nothing about the executed view.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以这样提取视图函数名称
You can extract the view function name thusly
Dave 的答案涉及每次测试 URL 时的 HTTP 请求,这可能会很慢。如果您只想知道网址解析为哪个视图,则无需使用
Client
即可:Ryan Wilcox 关于路由测试的帖子 更详细地介绍了这些内容,并提供了使测试变得更加容易的技术。
Dave's answer involves a HTTP request each time you're testing a url, which can be slow. If you just want to know what view a url resolves to, you can do that without using
Client
:Ryan Wilcox's post on route testing goes into more detail and provides techniques for making it even easier to test them.