selenium - Pytest Xdist 并行执行,防止重新创建数据库
问题描述
我正在尝试通过使用 4 个线程的并行执行 (-n=4) 来加速我的 python Django Web 应用程序中的 Selenium 测试
在前 4 个测试中,3 个给出以下错误:
[test setup] [Test Error Output]
Got an error creating the test database: (1007, "Can't create database 'test1database'; database exists")
我知道我必须指定在并行测试执行之前运行一次设置,以防止多次尝试创建数据库,但是我将如何在 pytest xdist 配置中强制执行此操作?
解决方案
对于每个线程,您可能有一个不同的数据库。夹具允许您这样worker_id
做
https://github.com/pytest-dev/pytest-xdist#identifying-the-worker-process-during-a-test
@pytest.fixture()
def test_database(worker_id):
return CreateDatabase("test{}database".format(worker_id))
更新
此github 问题评论显示了 OP 原始问题的解决方案。它还使用共享模板创建 N 个数据库。这带来了一个有趣的转折,即同步对夹具中共享资源的访问。
推荐阅读
- django - Django REST框架嵌套序列化程序中的“此字段是必需的”
- .net - 如何解决从前端向另一个使用另一个 .NET Core 应用程序作为身份服务器的 MVC 应用程序发出请求的应用程序中的 CORS 问题
- postgresql - 检查一组列是否具有科学价值
- flutter - flutter_launcher_icons --- 在 pubspec.yaml 中检测到错误:
- mysql - MySql,在 ON DUPLICATE KEY 内的子查询中使用值
- excel - Laravel excel:在使用集合时读取特定行
- c# - 我应该如何获得内联的 json 引用而不是获得它的引用 - 不应该有额外的属性 additionalProperty: 定义
- r - 如何在 R 中每 n 行创建一个具有相同 id 的新列?
- sql-server - 有没有办法将 Dynamic 365 中的数据导入我的 SQL Server 数据库?
- java - 如何确保 Azure Function 收到的消息只有一次?