【发布时间】:2022-02-02 21:18:06
【问题描述】:
我有以下测试代码,
from unittest.mock import Mock, patch
import boto3
import boto3.resources
import boto3.resources.base
import boto3.session
import pytest
from moto import mock_ec2
@pytest.fixture(scope="function")
def aws_credentials():
"""Mocked AWS Credentials for moto."""
os.environ["AWS_ACCESS_KEY_ID"] = "testing"
os.environ["AWS_SECRET_ACCESS_KEY"] = "testing"
os.environ["AWS_SECURITY_TOKEN"] = "testing"
os.environ["AWS_SESSION_TOKEN"] = "testing"
os.environ["MOTO_ALLOW_NONEXISTENT_REGION"] = "True"
os.environ["AWS_DEFAULT_REGION"] = "testing"
@pytest.fixture
def get_session(aws_credentials):
"""boto3 Session"""
return boto3.session.Session()
# and other tests code
运行良好。但是,我添加以下test_method后出现以下错误
@mock_ec2
def test_get_instances(get_session):
"""Test ...."""
错误是:
ImportError while importing test module 'C:\Users\....\test_n1.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
..\..\..\..\anaconda3\lib\importlib\__init__.py:126: in import_module
return _bootstrap._gcd_import(name[level:], package, level)
tests\unit_tests\test_node.py:85: in <module>
@mock_ec2
..\..\..\..\anaconda3\lib\site-packages\moto\__init__.py:17: in f
module = importlib.import_module(module_name, "moto")
..\..\..\..\anaconda3\lib\importlib\__init__.py:126: in import_module
return _bootstrap._gcd_import(name[level:], package, level)
..\..\..\..\anaconda3\lib\site-packages\moto\ec2\__init__.py:1: in <module>
from .models import ec2_backends
..\..\..\..\anaconda3\lib\site-packages\moto\ec2\models.py:29: in <module>
from moto.core import BaseBackend
..\..\..\..\anaconda3\lib\site-packages\moto\core\__init__.py:4: in <module>
from .responses import ActionAuthenticatorMixin
..\..\..\..\anaconda3\lib\site-packages\moto\core\responses.py:11: in <module>
from moto.core.exceptions import DryRunClientError
..\..\..\..\anaconda3\lib\site-packages\moto\core\exceptions.py:2: in <module>
from jinja2 import DictLoader, Environment
E ImportError: cannot import name 'DictLoader'
【问题讨论】:
标签: python amazon-web-services pytest boto3 moto