【问题标题】:WrapperTestResponse' object has no attribute 'text'WrapperTestResponse' 对象没有属性 'text'
【发布时间】:2021-05-23 02:52:52
【问题描述】:
  1. 目前我正在为我的烧瓶项目编写一个联合测试。我写了一个函数来测试登录功能。当我运行单元测试时,它显示了一些错误消息。

失败的 unit_test.py::TestClass::test_login - AttributeError: 'WrapperTestResponse' 对象没有属性 'text'

2.这是我的单元测试实现代码,我可以成功获取状态码,但不能获取文本。我是不是犯了一些错误?

import unittest
from app import app
import requests
from flask import request
import json



class TestClass(unittest.TestCase):
    def setup_class(self):
        app.config['TESTING'] = True  
        self.app = app.test_client()

    def teardown_class(self):
        """Do the testing """
        pass

    def test_login(self):
        response = self.app.get('/login')
        print(response)
        data = {'username': '123456@qq.com', 'password': '12345678'}
        response = app.test_client().post('/login', data=json.dumps(data))
        self.assertEqual(response.status_code, 200)
        print('--------------')
        self.assertEqual(response.text, "Invalid login credentials")

【问题讨论】:

    标签: unit-testing http flask flask-testing


    【解决方案1】:

    我认为您正在寻找 response.data

    调用 get_data() 和 set_data() 的描述符。

    get_data

    响应正文的字符串表示...

    如果您的视图函数返回 'Invalid login credentials' 时的示例输出:

    >>> response.data
    b'Invalid login credentials'
    

    【讨论】:

    • 此链接指向 Request.data,而不是测试响应类?
    • @jess 感谢您指出这一点。我不知何故没有注意到,当我写这个答案时,我已经更新了它。链接现在指向werkzeug.wrappers.Response 的方法,因为werkzeug.test.TestResponse 类是werkzeug.wrappers.Response 的子类。
    • 非常感谢您的快速回答!
    猜你喜欢
    • 2021-02-27
    • 2018-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-26
    • 2016-07-05
    相关资源
    最近更新 更多