代码之家  ›  专栏  ›  技术社区  ›  guettli

Django 1.4:测试非常缓慢

  •  0
  • guettli  · 技术社区  · 4 年前

    我正在开发一个仍然使用Django 1.4的旧代码库

    这意味着漂亮的旗帜 --keepdb 没有。

    运行这样一个测试需要50秒:

    time manage.py test myapp.tests.test_foo.FooTestCase.test_something
    

    我的 test_something 方法很快。测试数据库的创建需要很长时间。

    50秒对于高效的编辑测试周期来说太长了。我能做什么(除了更快的硬件)?

    1 回复  |  直到 4 年前
        1
  •  0
  •   guettli    4 年前

    你可以破解这个选项 --keepdb 输入源代码。

    你需要编辑这个文件 django/db/backends/creation.py 这样地:

    256c256,259
    <         self._create_test_db(verbosity, autoclobber)
    ---
    >         skip = True
    > 
    >         if not skip:
    >             self._create_test_db(verbosity, autoclobber)
    264,281c267,285
    <         # Report syncdb messages at one level lower than that requested.
    <         # This ensures we don't get flooded with messages during testing
    <         # (unless you really ask to be flooded)
    <         call_command('syncdb',
    <             verbosity=max(verbosity - 1, 0),
    <             interactive=False,
    <             database=self.connection.alias,
    <             load_initial_data=False)
    < 
    <         # We need to then do a flush to ensure that any data installed by
    <         # custom SQL has been removed. The only test data should come from
    <         # test fixtures, or autogenerated from post_syncdb triggers.
    <         # This has the side effect of loading initial data (which was
    <         # intentionally skipped in the syncdb).
    <         call_command('flush',
    <             verbosity=max(verbosity - 1, 0),
    <             interactive=False,
    <             database=self.connection.alias)
    ---
    >         if not skip:
    >             # Report syncdb messages at one level lower than that requested.
    >             # This ensures we don't get flooded with messages during testing
    >             # (unless you really ask to be flooded)
    >             call_command('syncdb',
    >                 verbosity=max(verbosity - 1, 0),
    >                 interactive=False,
    >                 database=self.connection.alias,
    >                 load_initial_data=False)
    > 
    >             # We need to then do a flush to ensure that any data installed by
    >             # custom SQL has been removed. The only test data should come from
    >             # test fixtures, or autogenerated from post_syncdb triggers.
    >             # This has the side effect of loading initial data (which was
    >             # intentionally skipped in the syncdb).
    >             call_command('flush',
    >                 verbosity=max(verbosity - 1, 0),
    >                 interactive=False,
    >                 database=self.connection.alias)
    391,393c395,398
    <         time.sleep(1)
    <         cursor.execute("DROP DATABASE %s"
    <                        % self.connection.ops.quote_name(test_database_name))
    ---
    > 
    >         #time.sleep(1)
    >         #cursor.execute("DROP DATABASE %s"
    >         #               % self.connection.ops.quote_name(test_database_name))
    

    现在运行测试需要6秒而不是50秒。

    如果修改数据库模式,则需要设置 skip = False 就一次。