首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >python assertionError中的单元测试响应:<AssertionError [ 200 ]> != 200

python assertionError中的单元测试响应:<AssertionError [ 200 ]> != 200
EN

Stack Overflow用户
提问于 2019-06-03 00:43:03
回答 1查看 1.8K关注 0票数 1

我正在用unittest和request模块编写python测试,但是

AssertionError: <Response [200]> != 200

测试在两个函数中设置: test_get和test_post。测试运行者从测试类开始,问题出在test2中。我也尝试过断言:<Response [200]>也是。但是得到的却是这个错误:

代码语言:javascript
复制
<Response [200]> != <Response [200]>

Expected :<Response [200]>
Actual   :<Response [200]>

为此,我使用httpbin和pycharm。

代码语言:javascript
复制
import requests
import unittest

# Test 1: Assert get data body
get_url = 'https://httpbin.org/get'
test_get = \
    {
        'args': {},
        'headers': {'Accept': '*/*',
                    'Accept-Encoding': 'gzip, deflate',
                    'Host': 'httpbin.org',
                    'User-Agent': 'python-requests/2.21.0'},
        'origin': '94.255.130.105, 94.255.130.105',
        'url': 'https://httpbin.org/get'
    }

def get_httpbin(get_url):
    r = requests.get(get_url).json()
    return r

# Test 2: Assert post is OK 200
post_url = 'https://httpbin.org/post'
test_post = \
    {
        'sender': 'Alice',
        'receiver': 'Bob',
        'message': 'We did it!'
    }

def post_httpbin(post_url):
    r = requests.post(post_url, test_post)
    return r

# Test Runner
class Tests(unittest.TestCase):
    def test1(self):
        self.assertEqual(get_httpbin(get_url), test_get)

    def test2(self):
        self.assertEqual(post_httpbin(post_url), 200)

if __name__ == '__main__':
    unittest.main()
EN

回答 1

Stack Overflow用户

发布于 2019-06-03 00:44:54

现在您正在比较r,它给您提供了一个整数<Response [200]>,因此出现了断言错误。相反,您可能希望断言r.status_code,它会给出带有200的整数形式的状态代码。

代码语言:javascript
复制
def test2(self):
    self.assertEqual(post_httpbin(post_url).status_code, 200)
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56416975

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档