django应用程序的自定义测试用例和其他测试帮助程序
incuna-test-utils的Python项目详细描述
#包括测试用例
[![构建状态](https://travis-ci.org/incuna/incuna-test-utils.svg?branch=master)](https://travis-ci.org/incuna/incuna-test-utils?branch=master)
incuna test utils是测试django应用程序的测试用例和其他帮助程序的集合。
>urltestcase.urltestcase.urltestcase.urltestcase`
\request.baserequesttestcase`
`baserequesttestcase`提供各种帮助方法来处理django
视图:
*`get_view`返回基于"view"属性的可调用视图在
`testcase`类上设置te。` view`可以是基于函数的视图,也可以是基于类的视图。
*`view_instance`返回一个基于类的"view"属性集的实例。` view_instance`接受一个'request`和`*args`并
`**kwargs`。这些是在"视图"实例上设置的。
*"将会话添加到请求"提供"请求"会话。
*"创建用户"使用"匿名用户"或"测试用例"上设置的"用户工厂"属性返回"用户"。"user_factory"应该有
返回"user"的"create"方法。建议使用
[`factory_boy`](http://factoryboy.readthedocs.org/en/latest/index.html)。
*`create_request`包装django的"requestfactory"以提供有用的默认值。
返回带有"user"和"um"的"request"消息的属性。它还可以对"请求"设置
"数据"和"会话"。
ontestcase`应该是子类,并且应该定义两个属性:
-创建经过身份验证的客户机的"用户工厂";
-要测试的"模型"。
示例:
`` python
来自incuna-test-utils.testcases.integration import baseadminintegrationtestcase
class testuseradmin(baseadministrationtestcase):
用户工厂=factories.user factory
模型=modeltotest
def测试管理添加页(self):
响应=self.get管理添加页()
self.assertequal(响应状态代码,200)
…
`````
###` integration.baseintegrationtestcase`
`baseintegrationtestcase`扩展了`baserequesttestcase`并添加了更多对集成测试有用的助手
方法:
*`access\u view`创建一个`request`,调用`testcase``的`view`并返回
a`response`.
*`render_str使用"request"、"response.template"和"response.context"数据呈现"response"。如果未提供"request",则"render_to_str"使用
"response.request"。
*"access_view_and_render_response"包装"access_view"和"render_to_str"。
它还会按预期检查"response.status_code"。默认值
`预期的显示状态'是'200'(`http_ok`)。
*`assert_presence`检查项目是否出现在容器中。
*`assert_count`检查项目是否出现在容器中的预期次数
。
*`assert_presence_multiple`和`assert_count廑le`在
单个方法调用中运行一个或多个断言。
*`render_view_and_assert_content`和`render_view_and_assert_content_counts`将
对'access_view_and_render_response'的调用与对结果的多个断言调用结合起来。
se`
`baseapirequesttestcase`扩展了'baserequesttestcase',用于
[`django rest framework`](http://www.django-rest-framework.org/)。
*`create_request`被重写以使用rest framework的
[`apirequestfactory`](http://www.django-rest-framework.org/api-guide/testing apirequestfactory).
它还将"request.format"设置为"json"。如果使用"auth=true"(默认值)调用,
"create_request"还调用
[`force_authenticate`](http://www.django-rest-framework.org/api guide/testing forcing authentication)。
factories
它们需要
[`factory戋boy`](http://factoryboy.readthedocs.org/en/latest/index.html)。
\user.baseuserfactory`
这可以与具有以下字段的自定义用户模型一起使用:
``python
class userfactory(baseuserfactory):
class meta:
model=user
````
_ active`,`is_staff`和
`is_superuser` to`true`。这个工厂还定义了一个post hook,它设置了一个默认的密码,可以用"raw_u password"访问。
\feincms_page.page factory`
这个工厂可以用来创建
[`feincms`]的实例(http://feincms django cms.readthedocs.org/en/latest/index.html)
[`第`]页(http://feincms-django-cms.readthedocs.org/en/latest/page.html)模型。
\compat`
代码:
*`django-lt-19`如果"django.version"小于"1.9",则为"true"。
*`python2assertmixin`aliases python 2.7 assert方法以匹配python 3 api。
*`testcase.asserttemsequal`别名为'assertcountequal`
*`testcase.assertregexpmatches`别名为'assertregex`
[![构建状态](https://travis-ci.org/incuna/incuna-test-utils.svg?branch=master)](https://travis-ci.org/incuna/incuna-test-utils?branch=master)
incuna test utils是测试django应用程序的测试用例和其他帮助程序的集合。
>urltestcase.urltestcase.urltestcase.urltestcase`
\request.baserequesttestcase`
`baserequesttestcase`提供各种帮助方法来处理django
视图:
*`get_view`返回基于"view"属性的可调用视图在
`testcase`类上设置te。` view`可以是基于函数的视图,也可以是基于类的视图。
*`view_instance`返回一个基于类的"view"属性集的实例。` view_instance`接受一个'request`和`*args`并
`**kwargs`。这些是在"视图"实例上设置的。
*"将会话添加到请求"提供"请求"会话。
*"创建用户"使用"匿名用户"或"测试用例"上设置的"用户工厂"属性返回"用户"。"user_factory"应该有
返回"user"的"create"方法。建议使用
[`factory_boy`](http://factoryboy.readthedocs.org/en/latest/index.html)。
*`create_request`包装django的"requestfactory"以提供有用的默认值。
返回带有"user"和"um"的"request"消息的属性。它还可以对"请求"设置
"数据"和"会话"。
ontestcase`应该是子类,并且应该定义两个属性:
-创建经过身份验证的客户机的"用户工厂";
-要测试的"模型"。
示例:
`` python
来自incuna-test-utils.testcases.integration import baseadminintegrationtestcase
class testuseradmin(baseadministrationtestcase):
用户工厂=factories.user factory
模型=modeltotest
def测试管理添加页(self):
响应=self.get管理添加页()
self.assertequal(响应状态代码,200)
…
`````
###` integration.baseintegrationtestcase`
`baseintegrationtestcase`扩展了`baserequesttestcase`并添加了更多对集成测试有用的助手
方法:
*`access\u view`创建一个`request`,调用`testcase``的`view`并返回
a`response`.
*`render_str使用"request"、"response.template"和"response.context"数据呈现"response"。如果未提供"request",则"render_to_str"使用
"response.request"。
*"access_view_and_render_response"包装"access_view"和"render_to_str"。
它还会按预期检查"response.status_code"。默认值
`预期的显示状态'是'200'(`http_ok`)。
*`assert_presence`检查项目是否出现在容器中。
*`assert_count`检查项目是否出现在容器中的预期次数
。
*`assert_presence_multiple`和`assert_count廑le`在
单个方法调用中运行一个或多个断言。
*`render_view_and_assert_content`和`render_view_and_assert_content_counts`将
对'access_view_and_render_response'的调用与对结果的多个断言调用结合起来。
se`
`baseapirequesttestcase`扩展了'baserequesttestcase',用于
[`django rest framework`](http://www.django-rest-framework.org/)。
*`create_request`被重写以使用rest framework的
[`apirequestfactory`](http://www.django-rest-framework.org/api-guide/testing apirequestfactory).
它还将"request.format"设置为"json"。如果使用"auth=true"(默认值)调用,
"create_request"还调用
[`force_authenticate`](http://www.django-rest-framework.org/api guide/testing forcing authentication)。
factories
它们需要
[`factory戋boy`](http://factoryboy.readthedocs.org/en/latest/index.html)。
\user.baseuserfactory`
这可以与具有以下字段的自定义用户模型一起使用:
``python
class userfactory(baseuserfactory):
class meta:
model=user
````
_ active`,`is_staff`和
`is_superuser` to`true`。这个工厂还定义了一个post hook,它设置了一个默认的密码,可以用"raw_u password"访问。
\feincms_page.page factory`
这个工厂可以用来创建
[`feincms`]的实例(http://feincms django cms.readthedocs.org/en/latest/index.html)
[`第`]页(http://feincms-django-cms.readthedocs.org/en/latest/page.html)模型。
\compat`
*`django-lt-19`如果"django.version"小于"1.9",则为"true"。
*`python2assertmixin`aliases python 2.7 assert方法以匹配python 3 api。
*`testcase.asserttemsequal`别名为'assertcountequal`
*`testcase.assertregexpmatches`别名为'assertregex`