首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Python test - AttributeError:'NoneType‘对象没有'status_code’属性

Python test - AttributeError:'NoneType‘对象没有'status_code’属性
EN

Stack Overflow用户
提问于 2021-06-02 20:52:14
回答 1查看 769关注 0票数 0

用python编写一些单元测试,已经将其简化为一些函数,以关注这个问题。

所以有两个函数是这样的

代码语言:javascript
运行
复制
def function_to_throw_http_error():
    logger.info("function_to_throw_http_error")


def function_to_catch_http_error():
    try:
        function_to_throw_http_error()
    except HTTPError as http_err:
        logger.error(f"HTTP error occurred external service: {http_err}")
        return {"statusCode": http_err.response.status_code, "body": http_err.response.reason}

测试是这样的

代码语言:javascript
运行
复制
    @patch(
        "functions.my_file.function_to_throw_http_error", Mock(side_effect=HTTPError(Mock(status_code=404), 'error')),
    )
    def test_catching_http_error_reposnse(self):
        response = function_to_catch_http_error()
        self.assertTrue('error' in str(response))

但是当我运行测试时,我得到了以下错误

代码语言:javascript
运行
复制
>           return {"statusCode": http_err.response.status_code, "body": http_err.response.reason}
E           AttributeError: 'NoneType' object has no attribute 'status_code'
EN

回答 1

Stack Overflow用户

发布于 2021-06-02 21:30:27

代码语言:javascript
运行
复制
#the builtins module itself has no HTTPError as default class
import builtins
print(dir(builtins))
#just printing out the exception classes from buitins directory

''''['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'BlockingIOError', 'BrokenPipeError', 'BufferError', 'BytesWarning', 'ChildProcessError', 'ConnectionAbortedError', 'ConnectionError', 'ConnectionRefusedError', 'ConnectionResetError', 'DeprecationWarning', 'EOFError', 'Ellipsis', 'EnvironmentError', 'Exception', 'False', 'FileExistsError', 'FileNotFoundError', 'FloatingPointError', 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', 'ImportWarning', 'IndentationError', 'IndexError', 'InterruptedError', 'IsADirectoryError', 'KeyError', 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'ModuleNotFoundError', 'NameError', 'None', 'NotADirectoryError', 'NotImplemented', 'NotImplementedError', 'OSError', 'OverflowError', 'PendingDeprecationWarning', 'PermissionError', 'ProcessLookupError', 'RecursionError', 'ReferenceError', 'ResourceWarning', 'RuntimeError', 'RuntimeWarning', 'StopAsyncIteration', 'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError', 'TimeoutError', 'True', 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning', 'ValueError', 'Warning', 'ZeroDivisionError']'''

# so in order to use HTTPError use the requests module

import requests
try:
    #your required code
    pass
except requests.HTTPError as exception:
    #print your exception using print(exception)
    pass
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67805466

复制
相关文章

相似问题

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