Django测试需要很长时间才能开始

时间:2018-05-06 20:59:25

标签: python django postgresql django-testing

我正在使用名为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

1 个答案:

答案 0 :(得分:0)

我测试的类没有存储在单个文件中,而是存储在单个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类之前停止了。