在 Django 配置文件应用程序中测试视图的最佳实践?

发布于 2024-12-12 10:21:53 字数 660 浏览 0 评论 0原文

我想为 Django 配置文件应用程序中的视图编写一些测试。

这些视图有一些智能的错误处理逻辑。例如,如果我们尝试创建个人资料,但该个人资料已经存在,则只需重定向到现有的个人资料页面(或者可能重定向到编辑个人资料页面)。

如何测试此错误处理是否按预期工作? 什么是最佳实践?

一种想法是使用 Zombie.js 执行 BDD,并测试我是否看到标题不是“创建个人资料”的页面(或者检查我是否看到标题为“编辑个人资料”的页面)。但是 Django 测试文档说:

  • 使用 Django 的测试客户端来建立正确的视图 调用并且视图正在收集正确的上下文数据。
  • 使用浏览器内框架(例如 Twill 和 Selenium)来测试渲染 HTML 和网页的行为,即 JavaScript 功能。

但是,如果我想使用 Django 测试客户端,它可以执行以下操作:

  • 模拟 URL 上的 GET 和 POST 请求并观察响应 - 从低级 HTTP(结果标头和状态代码)到页面内容的所有内容。
  • 测试是否针对给定 URL 执行了正确的视图。
  • 测试给定的请求是否由给定的 Django 模板呈现,模板上下文包含某些值。

我应该使用测试客户端然后查看页面内容吗?我应该看看渲染了什么模板吗?检验这个观点的适当方法是什么?

I want to write some tests for views in a Django profile app.

The views have some smart error-handling logic. e.g. if we try to create a profile, but the profile already exists, then just redirect to the existing profile page (or maybe to the edit profile page).

How do I test that this error-handling works as desired?
What are best-practices?

One idea would be to do BDD using Zombie.js, and test that I see a page whose title isn't "Create profile" (or maybe check that I see a page whose title is "Edit profile"). But the Django testing docs say:

  • Use Django's test client to establish that the correct view is being
    called and that the view is collecting the correct context data.
  • Use in-browser frameworks such as Twill and Selenium to test rendered
    HTML and the behavior of Web pages, namely JavaScript functionality.

However, if I want to use the Django test client, it can do the following:

  • Simulate GET and POST requests on a URL and observe the response -- everything from low-level HTTP (result headers and status codes) to page content.
  • Test that the correct view is executed for a given URL.
  • Test that a given request is rendered by a given Django template, with a template context that contains certain values.

Should I use the test client and then look at the page content? Should I see what template got rendered? What is the appropriate way to test this view?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北风几吹夏 2024-12-19 10:21:53

重定向和配置文件创建非常 django,因此虽然您可能使用其他框架来测试 JS 功能,但我将在此实例中使用 django 测试客户端。

您必须根据具体情况决定测试进行到什么程度。时间有限,某些方面比其他方面更容易损坏。例如,我不会担心模板解析 - 200 状态代码和快速检查内容就足够了。

无论如何,我首先要识别视图具有的不同状态(我在这里猜测):

  • 渲染自身(检查内容、模板、状态代码等) 配置
  • 文件表单验证错误
  • 配置文件表单成功:配置文件创建:成功
  • 重定向智能错误处理/重定向。

如何测试此错误处理是否按预期工作?

# create object with specific ID.
# post form data that should trigger your special redirect. 
# ensure the redirect takes you to the model you just created via reverse
response = client.post(reverse('form_url'), {'valid_form_data': ''}, follow=True)
assertRedirects(response, reverse('view_url', args=[id]), target_status_code=200)

这将确认正确的配置文件正在被重定向到您决定应触发此操作的任何数据。

Redirects and profile creation are very django so while you might use other frameworks to test JS functionality, I'd be using the django test client for this instance.

You have to decide on a case by case basis how far to go with your tests. Time is limited, and some aspects are more prone to breakage than others. For example, I wouldn't be worried about template resolution - a 200 status code and a quick check for content is plenty.

Anyhoo, I'd start by identifying the different states the view has (I'm guessing here):

  • Render itself (check content, template, status code, etc.)
  • Profile form validation error
  • Profile form success: profile creation: success redirect
  • The smart error handling / redirect.

How do I test that this error-handling works as desired?

# create object with specific ID.
# post form data that should trigger your special redirect. 
# ensure the redirect takes you to the model you just created via reverse
response = client.post(reverse('form_url'), {'valid_form_data': ''}, follow=True)
assertRedirects(response, reverse('view_url', args=[id]), target_status_code=200)

This would confirm that the correct profile is being redirected to given whatever data you've decided should trigger this action.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文