Django测试花了很长时间

2024-09-24 00:31:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在一个名为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

Tags: inpytestwebgamestatscreatetests
1条回答
网友
1楼 · 发布于 2024-09-24 00:31:04

我测试的类不是存储在单独的文件中,而是存储在一个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类之前暂停。在

相关问题 更多 >