【问题标题】:what is difference between `django.test.APITestCase` and `rest_framework.test.TestCase` in `python-django``python-django`中的`django.test.APITestCase`和`rest_framework.test.TestCase`有什么区别
【发布时间】:2017-10-13 20:57:43
【问题描述】:

django.test.TestCaserest_framework.test.APITestCase 这些测试类的主要区别是什么。哪个更好测试我的 views.py ? 你能建议我了解这些文件吗?

提前谢谢你。 :-)

【问题讨论】:

  • 据我所知,django 中没有 APITestCase。使用 rest_framework.test.APITestCase
  • 对不起,我修改了我的问题。 rest_framework 中的 APITestCase 和 Django 中的 TestCase

标签: python-3.x unit-testing django-rest-framework django-testing web-api-testing


【解决方案1】:

rest_framework.test 中的APITestCase 用于测试其余的api。它特定于 api 操作和 api 调用。 Django.test.TestCase 用于测试 Django 类。

【讨论】:

    【解决方案2】:

    rest_framework.test.APITestCase 和 django.test.testcases.TestCase 在他们使用的 HTTP 客户端中的区别(来源:github)。

    使用 rest_framework APIClient 比使用 Django 客户端更容易测试 API。例如,较少使用 json 响应。查看更多:http://www.django-rest-framework.org/api-guide/testing/

    【讨论】:

    • 我正在测试响应。我有 3python 对象,我将得到 3 个对象作为响应,我需要检查是否相同。你能说如何比较reponse.data和python对象吗?
    • assertEqual 有第一个和第二个响应,然后assertEqual 有第二个和第三个响应,请参阅docs.python.org/3/library/…
    猜你喜欢
    • 2015-02-15
    • 2014-07-22
    • 1970-01-01
    • 1970-01-01
    • 2014-03-02
    • 1970-01-01
    • 2014-04-16
    • 2016-10-09
    相关资源
    最近更新 更多