【问题标题】:Django tests taking a long time to startDjango 测试需要很长时间才能开始
【发布时间】:2018-10-16 16:27:00
【问题描述】:

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

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

最终,几分钟后我看到了
Creating test database for alias '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

【问题讨论】:

    标签: python django postgresql django-testing


    【解决方案1】:

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

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

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

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

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

    【讨论】:

      猜你喜欢
      • 2022-06-15
      • 2023-04-05
      • 1970-01-01
      • 1970-01-01
      • 2022-10-13
      • 2016-07-29
      • 1970-01-01
      • 1970-01-01
      • 2021-07-19
      相关资源
      最近更新 更多