用于并行运行的测试的内存动态数据库



我使用的是python3.9,在我的项目中,我有一堆集成测试,这些测试使用moto 4.0.7与内存中的dynamo数据库一起运行。我的配置设置看起来像这样-

@pytest.fixture(scope="session", name="dynamodb_server")
def dynamodb_server_fixture() -> Emitter[str]:
with Popen(["moto_server", "-p3000"]) as process:  # nosec
yield "http://localhost:3000"
process.kill()

@pytest.fixture(scope="session", name="dynamodb_resource")
def dynamodb_resource_fixture(dynamodb_server: str) -> Emitter[Any]:
resource = boto3.resource("dynamodb", endpoint_url=dynamodb_server)
original_getter = aws.resource
def resource_override(name: str) -> Any:
if name == "dynamodb":
return resource
return original_getter(name)
with patch("core_aws.aws.resource") as function:
function.side_effect = resource_override
yield resource

@pytest.fixture(scope="session", name="dynamodb_client")
def dynamodb_client_fixture(dynamodb_server: str) -> Emitter[Any]:
client = boto3.client("dynamodb", endpoint_url=dynamodb_server)
original_getter = aws.client
def client_override(name: str, endpoint_url: Optional[str] = None) -> Any:
if name == "dynamodb":
return client
return original_getter(name, endpoint_url)
with patch("core_aws.aws.client") as function:
function.side_effect = client_override
yield client

@pytest.fixture(scope="session", autouse=True)
def my_test_table(dynamodb_resource: Any, dynamodb_client: Any) -> Emitter[None]:
original = MyTestTable.__metadata__.table
MyTestTable.__metadata__.table = tables.create(dynamodb_resource, dynamodb_client, MyTestTable.__metadata__)
yield
MyTestTable.__metadata__.table = original

正如您所理解的,MyTestTable已生成并可用于我的所有集成测试。因此pytest -s tests/integration可以很好地工作。但是,如果我想使用pytest -n 10 -s tests/integration并行运行测试,那么它就会以错误开始失败

botocore.errorfactory.ResourceInUseException: An error occurred (ResourceInUseException) when calling the CreateTable operation: Resource in use

有人能告诉我我的配置出了什么问题吗?scope="session"是罪魁祸首吗?

简单的解决方案是从所有负责创建表的设备中删除autouse=True。我现在可以并行运行所有测试了。

相关内容

  • 没有找到相关文章

最新更新