python - Django 测试需要很长时间才能启动

标签 python django postgresql django-testing

我正在一个名为 stats 的 django 应用程序上运行测试,该应用程序使用 Postgre 数据库。该项目将所有测试保存在文件夹 ./stats/tests/ 中。

当我运行一个测试,即 python manage.py test stats.tests.test_dataGeneration 时,我的命令行中的文本光标只是闪烁,几分钟没有任何反应。在这个早期阶段,我什至无法使用键盘中断来立即停止测试(键盘中断至少需要 30 秒才能注册)。

最终,几分钟后我看到了
正在为别名“default”(“test_statsdb”)创建测试数据库...
创建用于测试的数据库的消息。从这一点开始,测试总是在不到 10 秒的时间内完成(如 django 输出 Ran XX tests in 5.00s)。

我不知道或不明白为什么测试需要这么长时间才能开始,或者在开始的几分钟内究竟执行了什么。几天前,我在测试该应用程序时没有遇到任何问题。使用 --keepdb 在测试运行之间回收测试数据库不会显着改善运行测试所需的时间。

我希望有人能阐明正在发生的事情。

当我使用 verbosity=2 运行测试结果的示例输出以获取更多详细信息:
>python manage.py test stats.tests.test_dataGeneration --verbosity=2

Creating test database for alias 'default' ('test_statsdb')...
Operations to perform:
  Synchronize unmigrated apps: messages, portfolio_web, staticfiles, updateDatabase
  Apply all migrations: auth, contenttypes, sessions, stats
Synchronizing apps without migrations:
  Creating tables...
    Running deferred SQL...
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0001_initial... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying sessions.0001_initial... OK
  Applying stats.0001_initial... OK
  Applying stats.0002_auto_20180312_1632... OK
  Applying stats.0003_auto_20180321_2336... OK
  Applying stats.0004_auto_20180321_2353... OK
  Applying stats.0005_auto_20180423_1700... OK
System check identified no issues (0 silenced).
test_create_method_calls_create_game_with_correct_fields (stats.tests.test_dataGeneration.TestGameManager) ... FAIL
test_finds_tournament_instance (stats.tests.test_dataGeneration.TestGameManager) ... ok
test_sets_match_length (stats.tests.test_dataGeneration.TestGameManager) ... FAIL
test_can_create_tournaments_given_api_input (stats.tests.test_dataGeneration.TestProcessor) ... ERROR
test_correct_game_mode_being_selected_by_create_game_fucntion (stats.tests.test_dataGeneration.TestProcessor) ... FAIL
test_get_heroes (stats.tests.test_dataGeneration.TestProcessor) ... 
ok
test_get_players (stats.tests.test_dataGeneration.TestProcessor) ... 
ok
test_pass_data_method_calls_model_manager_equivalent (stats.tests.test_dataGeneration.TestProcessor) ...
ok
test_returns_match_ids (stats.tests.test_dataGeneration.TestProcessor) ... 
ok
test_tournament_creation_also_returns_tournament_ids_of_created_objects (stats.tests.test_dataGeneration.TestProcessor) ... FAIL
test_tournament_filter (stats.tests.test_dataGeneration.TestProcessor) ...
ok

======================================================================
ERROR: test_can_create_tournaments_given_api_input (stats.tests.test_dataGeneration.TestProcessor)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\dev\Django code\portfolio_web\stats\tests\timer.py", line 8, in wrapper
    f(*args,**kwargs)
  File "C:\dev\Django code\portfolio_web\stats\tests\test_dataGeneration.py", line 77, in test_can_create_tournaments_given_api_input
    num_tournaments_start = Tournaments.objects.all()
NameError: name 'Tournaments' is not defined

======================================================================
FAIL: test_create_method_calls_create_game_with_correct_fields (stats.tests.test_dataGeneration.TestGameManager)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\dev\Django code\portfolio_web\stats\tests\test_dataGeneration.py", line 143, in test_create_method_calls_create_game_with_correct_fields
    self.fail("finish test")
AssertionError: finish test

======================================================================
FAIL: test_sets_match_length (stats.tests.test_dataGeneration.TestGameManager)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\dev\Django code\portfolio_web\stats\tests\test_dataGeneration.py", line 158, in test_sets_match_length
    self.fail("finish test, implement functionality")
AssertionError: finish test, implement functionality

======================================================================
FAIL: test_correct_game_mode_being_selected_by_create_game_fucntion (stats.tests.test_dataGeneration.TestProcessor)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\dev\Django code\portfolio_web\stats\tests\timer.py", line 8, in wrapper
    f(*args,**kwargs)
  File "C:\dev\Django code\portfolio_web\stats\tests\test_dataGeneration.py", line 136, in test_correct_game_mode_being_selected_by_create_game_fucntion
    self.fail("Make sure the game mode is the correct one!")
AssertionError: Make sure the game mode is the correct one!

======================================================================
FAIL: test_tournament_creation_also_returns_tournament_ids_of_created_objects (stats.tests.test_dataGeneration.TestProcessor)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\dev\Django code\portfolio_web\stats\tests\timer.py", line 8, in wrapper
    f(*args,**kwargs)
  File "C:\dev\Django code\portfolio_web\stats\tests\test_dataGeneration.py", line 93, in test_tournament_creation_also_returns_tournament_ids_of_created_objects
    self.assertEqual(tournament_ids, [data[0]['leagueid'], data[2]['leagueid']])
AssertionError: Lists differ: [] != ['30', '37']

Second list contains 2 additional elements.
First extra element 0:
'30'

- []
+ ['30', '37']

----------------------------------------------------------------------
Ran 11 tests in 0.015s

最佳答案

我测试的类没有存储在单独的文件中,而是存储在单个 Controller.py 中该文件还创建了一个名为 API 的类的实例. API初始化大约需要 5-10 分钟。未声明 Python 类(和函数);相反,它们是真实的现场陈述。因此,解释器读取并执行文件中的所有代码,而不是仅仅挑选出类定义。当他们从 Controller.py 导入类时,这是在我的测试文件中完成的

因此,在我的代码的最后一行是
API_INSTANCE = API(*args)
初始化API在每次测试期间。 API在初始化期间收集大量数据,这在读取测试文件中的导入时极大地减慢了测试速度。

问题的明显解决方案是将上面的代码行包装如下:

if __name__ == '__main__':
    API_INSTANCE = API(*args)

程序在 Creating test database... 之前停止的事实消息应该表明代码在执行 django.test.TestCase 之前停滞了类。

关于python - Django 测试需要很长时间才能启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50204473/

相关文章:

python - 为什么这段 Python 代码可以正常工作?请解释

django - django-pjax 应该如何处理重定向?

django - 从 Django 测试访问 Django 测试服务器

arrays - 值数组中的 PostgreSQL PL/pgSQL 随机值

javascript - 我如何将现有数据库连接到 sequelize.js?

postgresql - 触发器过程的一致性(行触发器之前)Postgresql

python - 逐行将文本字符串附加到 Python 中的各个行以避免内存错误 - 更快的替代方法?

Python 代码在 print 语句期间/之后停止输出,但代码的同一部分在作为其自己的程序隔离时可以工作。这是怎么回事?

python - 如何使用 appscript 和 Python 将文件列表输入到应用程序中?

django - 在 django 2.2 中的此测试中不允许对 'cache' 的数据库查询