Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python测试资产内容–;属性错误:';答复';对象没有属性';流媒体';_Python_Testing - Fatal编程技术网

Python测试资产内容–;属性错误:';答复';对象没有属性';流媒体';

Python测试资产内容–;属性错误:';答复';对象没有属性';流媒体';,python,testing,Python,Testing,我正在使用以下工具测试html响应对象: 在以下信号中测试与Mailchimp API的连接: from django.db.models.signals import post_save from django.dispatch import receiver import requests from .models import RSSFeed, WagchimpCampaign, MailchimpSetting @receiver(post_save, sender=Wagchimp

我正在使用以下工具测试html响应对象:

在以下信号中测试与Mailchimp API的连接:

from django.db.models.signals import post_save
from django.dispatch import receiver
import requests

from .models import RSSFeed, WagchimpCampaign, MailchimpSetting

@receiver(post_save, sender=WagchimpCampaign)
def check_mailchimp_campaign(sender, **kwargs):
    apikey = MailchimpSetting.objects.get().api_key
    r = requests.get('https://us1.api.mailchimp.com/3.0/',
        auth=('user', apikey))
    return r
我得到了一个错误:

Traceback (most recent call last):
  File "/Users/technical/code/pb/tests.py", line 46, in test_mailchimp_api_response_on_signal_handler
    self.assertContains(r, "", status_code=200)
  File "/Users/technical/.virtualenvs/wagtest6-PGdhJpMT/lib/python2.7/site-packages/django/test/testcases.py", line 385, in assertContains
    response, text, status_code, msg_prefix, html)
  File "/Users/technical/.virtualenvs/wagtest6-PGdhJpMT/lib/python2.7/site-packages/django/test/testcases.py", line 360, in _assert_contains
    if response.streaming:
AttributeError: 'Response' object has no attribute 'streaming'

响应对象带有一个200状态代码,并且似乎格式良好。如果没有流属性,为什么会失败?

文档中提到的关于使用响应实例的assertContains特别指的是从Django测试客户端返回的内部响应类,如下所述


在任何情况下,正如我提到的,您绝对不应该在测试中查询外部API。根据定义,单元测试应该只使用代码单元——其他一切都是外部的。测试API是否工作有什么用?您应该测试的是,给定一个来自API的特定响应,您自己的代码会执行预期的操作。您可以使用模拟来实现这一点。

发布完整堆栈跟踪比只发布错误更有帮助。请编辑您的问题以包含它。您可能需要发布测试的其余部分。您似乎在暗示,
response
是您从查询外部API中获得的东西-这首先不是
assertContains
所期望的,其次是在测试中执行的一个非常糟糕的主意。您使用Python还是Django单元测试?请你写一封信好吗!德扬戈TestCase@DanielRoseman文档只是说这个断言:“断言一个响应实例生成了给定的状态代码,并且文本出现在响应的内容中。”它只是说一个响应,似乎不排除外部API响应。您是否建议不要在单元测试中测试API连接性?
Traceback (most recent call last):
  File "/Users/technical/code/pb/tests.py", line 46, in test_mailchimp_api_response_on_signal_handler
    self.assertContains(r, "", status_code=200)
  File "/Users/technical/.virtualenvs/wagtest6-PGdhJpMT/lib/python2.7/site-packages/django/test/testcases.py", line 385, in assertContains
    response, text, status_code, msg_prefix, html)
  File "/Users/technical/.virtualenvs/wagtest6-PGdhJpMT/lib/python2.7/site-packages/django/test/testcases.py", line 360, in _assert_contains
    if response.streaming:
AttributeError: 'Response' object has no attribute 'streaming'