unit-testing - 与 APIClient() 相比,使用 RequestsClient() 有什么优势?

标签 unit-testing django-rest-framework

我正在为 Django-rest-framework API 端点编写单元测试。在 version 3.5 中,他们添加了 RequestsClient()。文档说——

Rather than sending any HTTP requests to the network, this interface will coerce all outgoing requests into WSGI, and call into your application directly.



根据我的理解,我认为 RequestsClient() 对于来自不同服务器的网络请求很有用。不知道在同一台服务器上有没有什么优势?另外,使用 RequestsClient() 比 APIClient() 有什么优势吗?

最佳答案

如果您不确定,请使用标准 APIClient
RequestsClient 在以下情况下很有用:

  • 您打算与来自另一个 Python 服务的 API 进行交互,并希望您的测试在同一级别上工作。
  • 您希望以一种允许您在实时或临时环境中运行它们的方式编写测试。
  • 关于unit-testing - 与 APIClient() 相比,使用 RequestsClient() 有什么优势?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40488339/

    相关文章:

    java - 是否可以要求 junit 在进行测试之前运行处方?

    unit-testing - 从对象测试事件

    python - Django 项目无法强制 Google Appengine 重定向到 https

    python - Django session 将参数从一个 View 传递到另一个 View ,但为所有用户实例设置相同的值

    python - 如何正确断言在 pytest 中引发了异常?

    python - Django 测试技巧

    unit-testing - 如何构建具有依赖关系的单元测试?

    python - Django 休息框架没有正确读取 JSON

    api - 为每个数据库表拥有一个 RESTful 资源是一种好习惯吗?

    django-rest-framework - 如何解决不起作用的权限类或逻辑