在具有moto的项目上运行coverage.py会导致jinja2错误

时间:2019-03-15 19:32:21

标签: python mocking jinja2 boto3 moto

我正在使用来自moto和boto3的@ mock_s3装饰器来模拟对s3的AWS调用。我正在从根目录使用“ coverage run”运行测试。

每当我的测试涉及涉及列出AWS S3内容的任何命令时,都会出现此错误:

  File "/Users/Name/Documents/project-name/test/lib/python3.7/site-packages/jinja2/compiler.py", line 1044, in visit_For
    if node.test:
AttributeError: 'For' object has no attribute 'test'

一些因该错误而失败的命令示例:

s3_client = s3_session.client("s3")
s3_client.list_buckets() #fails at this line

s3_session = boto3.session.Session()
s3_client = s3_session.client("s3")
paginator = s3_client.get_paginator("list_objects_v2")
iterator = paginator.paginate(Bucket="mybucket", Prefix="mypath")
keys = iterator.search(query)
regex = re.compile('(\S)')
data_files = list(filter(regex.search, keys)) #fails at this line

在运行这些代码行之前,我使用boto3在这些存储桶中创建存储桶和对象。

这是一个测试示例:

@mock_s3
class TestModel(TestCase):

    def test_my_model_save(self):

        s3_client = boto3.client('s3', region_name='us-east-1') 
        s3_client.create_bucket(Bucket='mybucket') # printing the response from this returns {'ResponseMetadata': {'HTTPStatusCode': 200, 'HTTPHeaders': {}, 'RetryAttempts': 0}}
        s3_client.put_object(Bucket='mybucket', Key="mypath", Body="details") # printing the response from this returns {'ResponseMetadata': {'HTTPStatusCode': 200, 'HTTPHeaders': {'etag': '"27792947ed5d5da7c0d1f43327ed9dab"', 'last-modified': 'Fri, 15 Mar 2019 20:15:44 GMT', 'content-length': '7'}, 'RetryAttempts': 0}, 'ETag': '"27792947ed5d5da7c0d1f43327ed9dab"'}
        print(s3_client.list_buckets()) #code breaks here 

我认识到该测试尚未完成(缺少断言,并且未在实际测试我的代码),这只是演示该错误的一般情况。

我还注意到,create_bucket和put_object的响应似乎与这两个函数的boto3文档都不匹配(请参见上面的示例测试代码中的注释)。

这是执行测试时的完整回溯:

    Traceback (most recent call last):
  File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/moto/core/models.py", line 74, in wrapper
    result = func(*args, **kwargs)
  File "/Users/user_name/Documents/project_name/api/tests/test_my_Model.py", line 24, in test_my_model_save
    print(s3_client.list_buckets())
  File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/botocore/client.py", line 320, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/botocore/client.py", line 611, in _make_api_call
    operation_model, request_dict)
  File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/botocore/endpoint.py", line 102, in make_request
    return self._send_request(request_dict, operation_model)
  File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/botocore/endpoint.py", line 136, in _send_request
    success_response, exception):
  File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/botocore/endpoint.py", line 210, in _needs_retry
    caught_exception=caught_exception, request_dict=request_dict)
  File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/botocore/hooks.py", line 356, in emit
    return self._emitter.emit(aliased_event_name, **kwargs)
  File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/botocore/hooks.py", line 228, in emit
    return self._emit(event_name, kwargs)
  File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/botocore/hooks.py", line 211, in _emit
    response = handler(**kwargs)
  File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/botocore/retryhandler.py", line 183, in __call__
    if self._checker(attempts, response, caught_exception):
  File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/botocore/retryhandler.py", line 251, in __call__
    caught_exception)
  File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/botocore/retryhandler.py", line 269, in _should_retry
    return self._checker(attempt_number, response, caught_exception)
  File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/botocore/retryhandler.py", line 317, in __call__
    caught_exception)
  File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/botocore/retryhandler.py", line 223, in __call__
    attempt_number, caught_exception)
  File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/botocore/retryhandler.py", line 359, in _check_caught_exception
    raise caught_exception
  File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/botocore/endpoint.py", line 176, in _get_response
    responses = self._event_emitter.emit(event_name, request=request)
  File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/botocore/hooks.py", line 356, in emit
    return self._emitter.emit(aliased_event_name, **kwargs)
  File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/botocore/hooks.py", line 228, in emit
    return self._emit(event_name, kwargs)
  File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/botocore/hooks.py", line 211, in _emit
    response = handler(**kwargs)
  File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/moto/core/models.py", line 292, in __call__
    status, headers, body = response_callback(request, request.url, request.headers)
  File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/moto/s3/responses.py", line 116, in bucket_response
    response = self._bucket_response(request, full_url, headers)
  File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/moto/s3/responses.py", line 138, in _bucket_response
    return self.all_buckets()
  File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/moto/s3/responses.py", line 53, in all_buckets
    template = self.response_template(S3_ALL_BUCKETS)
  File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/moto/core/responses.py", line 103, in response_template
    return self.environment.get_template(template_id)
  File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/jinja2/environment.py", line 830, in get_template
    return self._load_template(name, self.make_globals(globals))
  File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/jinja2/environment.py", line 804, in _load_template
    template = self.loader.load(self, name, globals)
  File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/jinja2/loaders.py", line 125, in load
    code = environment.compile(source, name, filename)
  File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/jinja2/environment.py", line 581, in compile
    defer_init=defer_init)
  File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/jinja2/environment.py", line 543, in _generate
    optimized=self.optimized)
  File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/jinja2/compiler.py", line 82, in generate
    generator.visit(node)
  File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/jinja2/visitor.py", line 38, in visit
    return f(node, *args, **kwargs)
  File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/jinja2/compiler.py", line 754, in visit_Template
    self.blockvisit(node.body, frame)
  File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/jinja2/compiler.py", line 378, in blockvisit
    self.visit(node, frame)
  File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/jinja2/visitor.py", line 38, in visit
    return f(node, *args, **kwargs)
  File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/jinja2/compiler.py", line 1044, in visit_For
    if node.test:
AttributeError: 'For' object has no attribute 'test'

我正在使用moto和boto3的最新版本。

这里可能是什么问题?

1 个答案:

答案 0 :(得分:0)

解决了我的问题,但可能有助于使其他用户更清楚了解这种情况。

在虚拟环境中运行单元测试时,会遇到此错误,但是在虚拟环境之外本地运行时,代码可以工作。

在虚拟环境之外运行时,我还会从create_bucket和put_object boto3命令获得正确的响应,而不是之前收到的响应。