[英]Errors when running tests in parallel
Multiple issues arise when I try to run tests in parallel.当我尝试并行运行测试时会出现多个问题。
According to the docs, "test_" is prepended to the database name specified in DATABASES.根据文档,在 DATABASES 中指定的数据库名称前面加上了“test_”。 I used the name "postgres", so the database created when running tests is called test_postgres .
我使用了名称“postgres”,因此运行测试时创建的数据库称为test_postgres 。 When running tests in parallel, the following databases are created (which is expected): test_postgres_1 , test_postgres_2 , test_postgres_3 , and test_postgres_4 .
并行运行测试时,会创建以下数据库(这是预期的): test_postgres_1 、 test_postgres_2 、 test_postgres_3和test_postgres_4 。 When running all tests with the
--parallel=4
option, however, every test fails with the following message: django.db.utils.OperationalError: FATAL: database "postgres_x" does not exist
where x can be 1, 2, 3 or 4. I can see that the following databases have been created: test_postgres_x
where x can be 1, 2, 3 or 4. Where's "postgres_x" coming from?但是,当使用
--parallel=4
选项运行所有测试时,每个测试都会失败并显示以下消息: django.db.utils.OperationalError: FATAL: database "postgres_x" does not exist
where x can be 1, 2, 3 or 4. 我可以看到已经创建了以下数据库: test_postgres_x
其中 x 可以是 1、2、3 或 4。“postgres_x” test_postgres_x
而来? Why isn't "test_" being prepended to these?为什么没有在这些前面加上“test_”?
Furthermore, if I manually create the expected databases postgres_x
(x = 1 to 4), the migrations applied to the "main" database aren't applied to the clones.此外,如果我手动创建预期的数据库
postgres_x
(x = 1 到 4),则应用于“主”数据库的迁移不会应用于克隆。 This results in errors like this: django.db.utils.ProgrammingError: relation "users_user" does not exist
.这会导致如下错误:
django.db.utils.ProgrammingError: relation "users_user" does not exist
。 Roughly 1/4 tests pass when using 4 cores.使用 4 个内核时,大约 1/4 的测试通过。
Lastly, if I try to migrate postgres_x
by using migrate --database=postgres_x
, I get: django.db.utils.ConnectionDoesNotExist: The connection postgres_x doesn't exist
.最后,如果我尝试迁移
postgres_x
通过migrate --database=postgres_x
,我得到: django.db.utils.ConnectionDoesNotExist: The connection postgres_x doesn't exist
。
I have ensured that all tests are isolated just so I can run them in parallel.我确保所有测试都是隔离的,这样我就可以并行运行它们。 What am I supposed to do?
我应该做些什么?
Instead of building your test harness yourself I suggest using pytest
and pytest-django
and pytest-xdist
this will handle the db creation and migration for each parallel worker.我建议使用
pytest
和pytest-django
以及pytest-xdist
而不是自己构建测试工具,这将处理每个并行工作程序的数据库创建和迁移。 ( pytest
can run Django UnitTest tests without modification) (
pytest
无需修改即可运行 Django UnitTest 测试)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.