社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Django

Django 1.4:测试非常缓慢

guettli • 4 年前 • 1769 次点击  

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

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

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

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

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

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

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/129797
文章 [ 1 ]  |  最新文章 4 年前
guettli
Reply   •   1 楼
guettli    5 年前

你可以破解这个选项 --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 就一次。