ttoolly:Django测试工具

  • u7_144078
    了解作者
  • 111.7KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-04-13 07:33
    上传日期
整齐地 Django测试工具。 的Django> = 1.8 Django表单验证集 样本测试 测试类的描述及其用于描述表单的参数 GlobalTestMixIn 栏位名称 默认 描述 使用实例 all_unique 没有任何 不使用 choice_fields_values {} 选择,多选字段的值选项 choice_fields_values = {'field1':(value1,value2)} custom_error_messages {} 特定字段的自定义消息 custom_error_messages = {'field1':{message_type:u“错误消息文本。”}} 错误 [] 不覆盖(存储当前测试的错误值) 档案 [] 当前测试中的文件列表(用于在每个测试结束时关闭文件) f =打开(文件名); self.fields.append
ttoolly-master.zip
内容介绍
# -*- coding: utf-8 -*- from __future__ import (absolute_import, division, print_function, unicode_literals) from copy import copy from datetime import datetime, timedelta from random import choice import re from django.conf import settings from django.core import mail from django.db import transaction from django.db.models import Q from django.utils.encoding import force_text from builtins import str from freezegun import freeze_time from future.utils import viewitems, viewkeys from past.builtins import basestring from .utils import (format_errors, get_randname, get_random_email_value, get_field_from_response, convert_size_to_bytes) from .utils.decorators import (only_with, only_with_obj, only_with_files_params, only_with_any_files_params) class ListPositiveCases(object): @only_with_obj @only_with(('url_list', 'filter_params')) def test_view_list_with_filter_positive(self): """ View list with filter positive """ for field, value in viewitems(self.filter_params): value = value if value else '' try: response = self.client.get(self.get_url(self.url_list), {field: value}, follow=True, **self.additional_params) self.assert_status_code(response.status_code, 200) except Exception: self.errors_append(text='For filter %s=%s' % (field, value)) class ListNegativeCases(object): @only_with_obj @only_with(('url_list', 'filter_params')) def test_view_list_with_filter_negative(self): """ View list with filter negative """ for field in viewkeys(self.filter_params): self.check_and_create_objects_for_filter(field) for value in ('qwe', '1', '0', 'йцу', '²'): try: response = self.client.get(self.get_url(self.url_list), {field: value}, follow=True, **self.additional_params) self.assert_status_code(response.status_code, 200) except Exception: self.errors_append(text='For filter %s=%s' % (field, value)) class AddPositiveCases(object): @only_with_obj def test_add_page_fields_list_positive(self): """ check that all and only need fields is visible at add page """ self.prepare_for_add() response = self.client.get(self.get_url(self.url_add), follow=True, **self.additional_params) form_fields = self.get_fields_list_from_response(response) try: """not set because of one field can be on form many times""" self.assert_form_equal(form_fields['visible_fields'], [el for el in self.all_fields_add if el not in (self.hidden_fields_add or ())]) except Exception: self.errors_append(text='For visible fields') if self.disabled_fields_add is not None: try: self.assert_form_equal(form_fields['disabled_fields'], self.disabled_fields_add) except Exception: self.errors_append(text='For disabled fields') if self.hidden_fields_add is not None: try: self.assert_form_equal(form_fields['hidden_fields'], self.hidden_fields_add) except Exception: self.errors_append(text='For hidden fields') fields_helptext = getattr(self, 'fields_helptext_add', {}) for field_name, text in viewitems(fields_helptext): if field_name not in self.all_fields_add: continue try: field = get_field_from_response(response, field_name) self.assertEqual(field.help_text, text) except Exception: self.errors_append(text='Helptext for field %s' % field_name) @only_with_obj def test_add_object_all_fields_filled_positive(self): """ Create object: fill all fields """ self.prepare_for_add() params = self.deepcopy(self.default_params_add) self.update_params(params) self.update_captcha_params(self.get_url(self.url_add), params) prepared_depends_fields = self.prepare_depend_from_one_of( self.one_of_fields_add) if self.one_of_fields_add else {} only_independent_fields = set(self.all_fields_add).difference( viewkeys(prepared_depends_fields)).difference(viewkeys(self.only_if_value or {})) for field in viewkeys(prepared_depends_fields): self.set_empty_value_for_field(params, field) self.fill_all_fields(list(only_independent_fields) + self.not_empty_fields_add + self._get_required_from_related(self.not_empty_related_fields_add), params) only_if_value_fields = [field for field, values in viewitems(self.only_if_value or {}) if all((params.get(k, None) == v for k, v in viewitems(values)))] self.fill_all_fields(only_if_value_fields, params) initial_obj_count = self.get_obj_manager.count() old_pks = list(self.get_obj_manager.values_list('pk', flat=True)) try: response = self.send_add_request(params) self.check_on_add_success(response, initial_obj_count, locals()) new_object = self.get_obj_manager.exclude(pk__in=old_pks)[0] exclude = getattr(self, 'exclude_from_check_add', []) self.assert_object_fields(new_object, params, exclude=exclude) except Exception: self.errors_append() @only_with_obj @only_with(('one_of_fields_add',)) def test_add_object_with_group_all_fields_filled_positive(self): """ Create object: fill all fields. Check with any filled field from one_of_fields groups """ for group in self.one_of_fields_add: for field in group: self.prepare_for_add() params = self.deepcopy(self.default_params_add) self.update_params(params) self.update_captcha_params(self.get_url(self.url_add), params) only_independent_fields = set(self.all_fields_add).difference(viewkeys(self._depend_one_of_fields_add)) fields_from_groups = set(viewkeys(self._depend_one_of_fields_add) ).difference(self._depend_one_of_fields_add[field]) for group in self.one_of_fields_add: _field = choice(group) fields_from_groups = fields_from_groups.difference(self._depend_one_of_fields_add[_field]) for f in set(viewkeys(self._depend_one_of_fields_add)).difference(fields_from_groups): self.set_empty_value_for_field(params, f) self.fill_all_fields(tuple(only_independent_fields) + tuple(fields_from_groups), params) self.clean_depend_fields_add(params, field) self.fill_all_fields((field,), params) mail.outbox = [] initial_obj_count = self.get_obj_manager.count() old_pks = list(self.get_obj_manager.values_list('pk', flat=True)) try: response = self.send_add_request(params) self.check_on_add_success(response, initial_obj_count, locals()) new_object = self.get_obj_manager.exclude(pk__in=old_pks)[0] exclude = getattr(self, 'exclude_from_check_add', []) self.assert_object_fields(new_object, params, exclude=exclude) except Exception: self.errors_append(text='For filled %s from group %s' % (field, repr(group))) @only_with_obj def test_add_object_only_required_fields_positive(self):
评论
    相关推荐