django unit test run slowly

在新建的 project 里测试挺快的,但是在 joyroost 里就挺慢。

  1. 对比了 manage.py 文件,没有什么大的区别。

  2. 将数据库由 mysql 更改为 sqlite3,还是慢。

  3. 复制一份出来逐个删除,看看问题出在哪里?

    • 是不是url太多的问题 ? 发现在 settings.py 的 INSTALLED_APPS中将 joyroost 注释掉,执行别的应用的 test 就很快了。
    • 是不是 joyroost 应用的 url 太多了呀? 屏蔽一些看看? 测试了,和url多少没有关系。
    • 那么问题出在哪里呢? 还好在 google 中找到了答案。如下:

    django-unit-testing-taking-a-very-long-time-to-create-test-database

The final solution that fixes my problem is to force Django to disable migration during testing, which can be done from the settings like this

    TESTING = len(sys.argv) > 1 and sys.argv[1] == 'test'
    if TESTING:
        print('=========================')
        print('In TEST Mode - Disableling Migrations')
        print('=========================')
    
        class DisableMigrations(object):
    
            def __contains__(self, item):
                return True
    
            def __getitem__(self, item):
                return "notmigrations"
    
        MIGRATION_MODULES = DisableMigrations()

or use https://pypi.python.org/pypi/django-test-without-migrations

My whole test now takes about 1 minute and a small app takes 5 seconds.

In my case, migrations are not needed for testing as I update tests as I migrate, and don't use migrations to add data. This won't work for everybody

这样在 settings.py 中设置后,速度果然快了。

看看我的 migrations,也是不少的:
swot@pp:~/pp/debug/joyroost/migrations$ ls
总用量 756
-rw-rw-r-- 1 swot swot 27255 5月 1 2016 0001_initial.py
-rw-rw-r-- 1 swot swot 15127 5月 1 2016 0001_initial.pyc
-rw-rw-r-- 1 swot swot 2105 5月 1 2016 0002_auto_20160501_0902.py
-rw-rw-r-- 1 swot swot 2048 5月 1 2016 0002_auto_20160501_0902.pyc
-rw-rw-r-- 1 swot swot 1164 5月 1 2016 0003_auto_20160501_0906.py
-rw-rw-r-- 1 swot swot 1055 5月 1 2016 0003_auto_20160501_0906.pyc
-rw-rw-r-- 1 swot swot 560 5月 1 2016 0004_auto_20160501_0908.py
-rw-rw-r-- 1 swot swot 846 5月 1 2016 0004_auto_20160501_0908.pyc
-rw-rw-r-- 1 swot swot 1655 5月 1 2016 0005_auto_20160501_0909.py
-rw-rw-r-- 1 swot swot 1899 5月 1 2016 0005_auto_20160501_0909.pyc
-rw-rw-r-- 1 swot swot 978 5月 1 2016 0006_auto_20160501_1056.py
-rw-rw-r-- 1 swot swot 1213 5月 1 2016 0006_auto_20160501_1056.pyc
-rw-rw-r-- 1 swot swot 1836 5月 4 2016 0007_auto_20160504_1142.py
-rw-rw-r-- 1 swot swot 1928 5月 4 2016 0007_auto_20160504_1142.pyc
-rw-rw-r-- 1 swot swot 937 5月 9 2016 0008_auto_20160509_1538.py
-rw-rw-r-- 1 swot swot 1197 5月 9 2016 0008_auto_20160509_1538.pyc
-rw-rw-r-- 1 swot swot 1381 5月 15 2016 0009_auto_20160515_1050.py
-rw-rw-r-- 1 swot swot 1367 5月 15 2016 0009_auto_20160515_1050.pyc
-rw-rw-r-- 1 swot swot 897 5月 15 2016 0010_auto_20160515_1116.py
-rw-rw-r-- 1 swot swot 1022 5月 15 2016 0010_auto_20160515_1116.pyc
-rw-rw-r-- 1 swot swot 1373 5月 16 2016 0011_auto_20160516_1445.py
-rw-rw-r-- 1 swot swot 1344 5月 16 2016 0011_auto_20160516_1445.pyc
-rw-rw-r-- 1 swot swot 975 5月 16 2016 0012_auto_20160516_2057.py
-rw-rw-r-- 1 swot swot 1175 5月 16 2016 0012_auto_20160516_2057.pyc
-rw-rw-r-- 1 swot swot 501 5月 16 2016 0013_auto_20160516_2110.py
-rw-rw-r-- 1 swot swot 882 5月 16 2016 0013_auto_20160516_2110.pyc
-rw-rw-r-- 1 swot swot 1687 5月 18 2016 0014_auto_20160518_2052.py
-rw-rw-r-- 1 swot swot 1577 5月 18 2016 0014_auto_20160518_2052.pyc
-rw-rw-r-- 1 swot swot 1095 5月 26 2016 0015_auto_20160526_0855.py
-rw-rw-r-- 1 swot swot 1244 5月 26 2016 0015_auto_20160526_0855.pyc
-rw-rw-r-- 1 swot swot 548 5月 26 2016 0016_poscard_out_num.py
-rw-rw-r-- 1 swot swot 904 5月 26 2016 0016_poscard_out_num.pyc
-rw-rw-r-- 1 swot swot 2638 6月 20 2016 0017_auto_20160620_1247.py
-rw-rw-r-- 1 swot swot 2599 6月 20 2016 0017_auto_20160620_1247.pyc
-rw-rw-r-- 1 swot swot 2010 6月 20 2016 0018_hiacall.py
-rw-rw-r-- 1 swot swot 1948 6月 20 2016 0018_hiacall.pyc
-rw-rw-r-- 1 swot swot 566 6月 24 2016 0019_auto_20160624_2044.py
-rw-rw-r-- 1 swot swot 917 6月 24 2016 0019_auto_20160624_2044.pyc
-rw-rw-r-- 1 swot swot 930 6月 25 2016 0020_auto_20160625_1106.py
-rw-rw-r-- 1 swot swot 1251 6月 25 2016 0020_auto_20160625_1106.pyc
-rw-rw-r-- 1 swot swot 1307 6月 25 2016 0021_auto_20160625_1410.py
-rw-rw-r-- 1 swot swot 1400 6月 25 2016 0021_auto_20160625_1410.pyc
-rw-rw-r-- 1 swot swot 2016 6月 29 2016 0022_auto_20160629_1421.py
-rw-rw-r-- 1 swot swot 2038 6月 29 2016 0022_auto_20160629_1421.pyc
-rw-rw-r-- 1 swot swot 868 6月 30 2016 0023_auto_20160630_1324.py
-rw-rw-r-- 1 swot swot 1109 6月 30 2016 0023_auto_20160630_1324.pyc
-rw-rw-r-- 1 swot swot 833 6月 30 2016 0024_auto_20160630_1535.py
-rw-rw-r-- 1 swot swot 1137 6月 30 2016 0024_auto_20160630_1535.pyc
-rw-rw-r-- 1 swot swot 1057 7月 28 2016 0025_auto_20160728_1447.py
-rw-rw-r-- 1 swot swot 1285 7月 28 2016 0025_auto_20160728_1447.pyc
-rw-rw-r-- 1 swot swot 581 7月 28 2016 0026_agency_b_phonecodeflag.py
-rw-rw-r-- 1 swot swot 940 7月 28 2016 0026_agency_b_phonecodeflag.pyc
-rw-rw-r-- 1 swot swot 888 7月 29 2016 0027_auto_20160729_1448.py
-rw-rw-r-- 1 swot swot 1178 7月 29 2016 0027_auto_20160729_1448.pyc
-rw-rw-r-- 1 swot swot 2022 9月 12 10:44 0028_auto_20160912_1044.py
-rw-rw-r-- 1 swot swot 2230 9月 12 10:45 0028_auto_20160912_1044.pyc
-rw-rw-r-- 1 swot swot 3313 9月 12 18:00 0029_auto_20160912_1800.py
-rw-rw-r-- 1 swot swot 2525 9月 12 18:00 0029_auto_20160912_1800.pyc
-rw-rw-r-- 1 swot swot 922 9月 19 19:35 0030_auto_20160919_1935.py
-rw-rw-r-- 1 swot swot 1278 9月 19 19:35 0030_auto_20160919_1935.pyc
-rw-rw-r-- 1 swot swot 2049 9月 23 10:48 0031_auto_20160923_1048.py
-rw-rw-r-- 1 swot swot 1882 9月 23 10:48 0031_auto_20160923_1048.pyc
-rw-rw-r-- 1 swot swot 578 9月 26 23:17 0032_auto_20160926_2317.py
-rw-rw-r-- 1 swot swot 939 9月 26 23:18 0032_auto_20160926_2317.pyc
-rw-rw-r-- 1 swot swot 780 9月 28 16:35 0033_auto_20160928_1635.py
-rw-rw-r-- 1 swot swot 1086 9月 28 16:35 0033_auto_20160928_1635.pyc
-rw-rw-r-- 1 swot swot 1068 10月 7 21:24 0034_auto_20161007_2124.py
-rw-rw-r-- 1 swot swot 1249 10月 7 21:24 0034_auto_20161007_2124.pyc
-rw-rw-r-- 1 swot swot 830 10月 9 10:14 0035_auto_20161009_1014.py
-rw-rw-r-- 1 swot swot 1133 10月 9 10:14 0035_auto_20161009_1014.pyc
-rw-rw-r-- 1 swot swot 5613 10月 21 15:06 0036_auto_20161021_1506.py
-rw-rw-r-- 1 swot swot 4163 10月 21 15:06 0036_auto_20161021_1506.pyc
-rw-rw-r-- 1 swot swot 1331 10月 26 14:43 0037_auto_20161026_1443.py
-rw-rw-r-- 1 swot swot 1381 10月 26 14:44 0037_auto_20161026_1443.pyc
-rw-rw-r-- 1 swot swot 594 10月 26 15:01 0038_auto_20161026_1501.py
-rw-rw-r-- 1 swot swot 964 10月 26 15:01 0038_auto_20161026_1501.pyc
-rw-rw-r-- 1 swot swot 578 10月 29 06:56 0039_auto_20161029_0656.py
-rw-rw-r-- 1 swot swot 939 10月 29 07:02 0039_auto_20161029_0656.pyc
-rw-rw-r-- 1 swot swot 679 10月 29 07:19 0040_auto_20161029_0719.py
-rw-rw-r-- 1 swot swot 1039 10月 29 07:19 0040_auto_20161029_0719.pyc
-rw-rw-r-- 1 swot swot 599 10月 29 09:12 0041_merchant_resume.py
-rw-rw-r-- 1 swot swot 976 10月 29 09:12 0041_merchant_resume.pyc
-rw-rw-r-- 1 swot swot 955 10月 29 09:15 0042_auto_20161029_0915.py
-rw-rw-r-- 1 swot swot 1174 10月 29 09:15 0042_auto_20161029_0915.pyc
-rw-rw-r-- 1 swot swot 603 10月 29 09:24 0043_auto_20161029_0924.py
-rw-rw-r-- 1 swot swot 925 10月 29 09:24 0043_auto_20161029_0924.pyc
-rw-rw-r-- 1 swot swot 578 10月 29 09:29 0044_auto_20161029_0929.py
-rw-rw-r-- 1 swot swot 939 10月 29 09:29 0044_auto_20161029_0929.pyc
-rw-rw-r-- 1 swot swot 574 10月 29 09:31 0045_auto_20161029_0931.py
-rw-rw-r-- 1 swot swot 935 10月 29 09:31 0045_auto_20161029_0931.pyc
-rw-rw-r-- 1 swot swot 562 10月 29 10:17 0046_auto_20161029_1017.py
-rw-rw-r-- 1 swot swot 910 10月 29 10:17 0046_auto_20161029_1017.pyc
-rw-rw-r-- 1 swot swot 397 10月 29 15:28 0047_remove_merchant_testpic.py
-rw-rw-r-- 1 swot swot 740 10月 29 15:28 0047_remove_merchant_testpic.pyc
-rw-rw-r-- 1 swot swot 641 10月 29 16:26 0048_auto_20161029_1626.py
-rw-rw-r-- 1 swot swot 1104 10月 29 16:26 0048_auto_20161029_1626.pyc
-rw-rw-r-- 1 swot swot 536 10月 29 16:28 0049_auto_20161029_1628.py
-rw-rw-r-- 1 swot swot 917 10月 29 16:28 0049_auto_20161029_1628.pyc
-rw-rw-r-- 1 swot swot 2427 11月 4 14:21 0050_auto_20161104_1421.py
-rw-rw-r-- 1 swot swot 1946 11月 4 14:22 0050_auto_20161104_1421.pyc
-rw-rw-r-- 1 swot swot 2175 11月 5 16:45 0051_auto_20161105_1645.py
-rw-rw-r-- 1 swot swot 1759 11月 5 16:45 0051_auto_20161105_1645.pyc
-rw-rw-r-- 1 swot swot 810 11月 8 17:38 0052_auto_20161108_1738.py
-rw-rw-r-- 1 swot swot 1119 11月 8 17:38 0052_auto_20161108_1738.pyc
-rw-rw-r-- 1 swot swot 1156 11月 9 16:16 0053_auto_20161109_1616.py
-rw-rw-r-- 1 swot swot 1298 11月 9 16:16 0053_auto_20161109_1616.pyc
-rw-rw-r-- 1 swot swot 1013 11月 12 10:53 0054_auto_20161112_1053.py
-rw-rw-r-- 1 swot swot 1181 11月 12 10:53 0054_auto_20161112_1053.pyc
-rw-rw-r-- 1 swot swot 767 11月 12 23:15 0055_auto_20161112_2315.py
-rw-rw-r-- 1 swot swot 1203 11月 12 23:15 0055_auto_20161112_2315.pyc
-rw-rw-r-- 1 swot swot 1194 11月 14 09:33 0056_auto_20161114_0933.py
-rw-rw-r-- 1 swot swot 1495 11月 14 09:33 0056_auto_20161114_0933.pyc
-rw-rw-r-- 1 swot swot 1636 11月 22 08:41 0057_auto_20161122_0841.py
-rw-rw-r-- 1 swot swot 1725 11月 22 08:41 0057_auto_20161122_0841.pyc
-rw-rw-r-- 1 swot swot 1478 12月 4 17:24 0058_auto_20161204_1724.py
-rw-rw-r-- 1 swot swot 1438 12月 4 17:24 0058_auto_20161204_1724.pyc
-rw-rw-r-- 1 swot swot 597 12月 4 18:40 0059_merchant_product.py
-rw-rw-r-- 1 swot swot 1027 12月 4 18:40 0059_merchant_product.pyc
-rw-rw-r-- 1 swot swot 2313 12月 5 17:38 0060_auto_20161205_1738.py
-rw-rw-r-- 1 swot swot 1892 12月 5 17:38 0060_auto_20161205_1738.pyc
-rw-rw-r-- 1 swot swot 3630 12月 11 21:26 0061_auto_20161211_2126.py
-rw-rw-r-- 1 swot swot 3331 12月 11 21:26 0061_auto_20161211_2126.pyc
-rw-rw-r-- 1 swot swot 578 12月 12 17:24 0062_auto_20161212_1724.py
-rw-rw-r-- 1 swot swot 939 12月 12 17:24 0062_auto_20161212_1724.pyc
-rw-rw-r-- 1 swot swot 1574 12月 13 19:43 0063_auto_20161213_1943.py
-rw-rw-r-- 1 swot swot 1677 12月 13 19:43 0063_auto_20161213_1943.pyc
-rw-rw-r-- 1 swot swot 2027 12月 19 17:56 0064_auto_20161219_1756.py
-rw-rw-r-- 1 swot swot 2169 12月 19 17:56 0064_auto_20161219_1756.pyc
-rw-rw-r-- 1 swot swot 513 12月 19 18:00 0065_auto_20161219_1800.py
-rw-rw-r-- 1 swot swot 871 12月 19 18:00 0065_auto_20161219_1800.pyc
-rw-rw-r-- 1 swot swot 397 12月 19 18:01 0066_remove_merchant_termpic.py
-rw-rw-r-- 1 swot swot 740 12月 19 18:01 0066_remove_merchant_termpic.pyc
-rw-rw-r-- 1 swot swot 589 12月 19 18:04 0067_merchant_termpic.py
-rw-rw-r-- 1 swot swot 999 12月 19 18:04 0067_merchant_termpic.pyc
-rw-rw-r-- 1 swot swot 2113 12月 24 11:41 0068_auto_20161224_1141.py
-rw-rw-r-- 1 swot swot 2171 12月 24 11:42 0068_auto_20161224_1141.pyc
-rw-rw-r-- 1 swot swot 1031 12月 30 11:14 0069_auto_20161230_1114.py
-rw-rw-r-- 1 swot swot 1211 12月 30 11:15 0069_auto_20161230_1114.pyc
-rw-rw-r-- 1 swot swot 1963 1月 4 06:56 0070_auto_20170104_0656.py
-rw-rw-r-- 1 swot swot 2500 1月 4 06:57 0070_auto_20170104_0656.pyc
-rw-rw-r-- 1 swot swot 980 1月 6 12:50 0071_auto_20170106_1250.py
-rw-rw-r-- 1 swot swot 1177 1月 6 12:50 0071_auto_20170106_1250.pyc
-rw-rw-r-- 1 swot swot 2680 1月 23 12:05 0072_auto_20170123_1205.py
-rw-rw-r-- 1 swot swot 2128 1月 23 12:05 0072_auto_20170123_1205.pyc
-rw-rw-r-- 1 swot swot 857 1月 23 15:35 0073_auto_20170123_1535.py
-rw-rw-r-- 1 swot swot 1087 1月 23 15:35 0073_auto_20170123_1535.pyc
-rw-rw-r-- 1 swot swot 3102 2月 6 10:42 0074_auto_20170206_1042.py
-rw-rw-r-- 1 swot swot 2197 2月 6 10:42 0074_auto_20170206_1042.pyc
-rw-rw-r-- 1 swot swot 2641 2月 6 12:13 0075_auto_20170206_1213.py
-rw-rw-r-- 1 swot swot 1857 2月 6 12:13 0075_auto_20170206_1213.pyc
-rw-rw-r-- 1 swot swot 1052 2月 10 11:09 0076_auto_20170210_1109.py
-rw-rw-r-- 1 swot swot 1540 2月 10 11:10 0076_auto_20170210_1109.pyc
-rw-rw-r-- 1 swot swot 11980 2月 20 17:45 0077_auto_20170220_1745.py
-rw-rw-r-- 1 swot swot 9076 2月 20 17:45 0077_auto_20170220_1745.pyc
-rw-rw-r-- 1 swot swot 2493 2月 22 17:01 0078_auto_20170222_1701.py
-rw-rw-r-- 1 swot swot 2587 2月 22 17:01 0078_auto_20170222_1701.pyc
-rw-rw-r-- 1 swot swot 4252 2月 23 16:36 0079_auto_20170223_1636.py
-rw-rw-r-- 1 swot swot 4008 2月 23 16:36 0079_auto_20170223_1636.pyc
-rw-rw-r-- 1 swot swot 912 2月 24 19:12 0080_auto_20170224_1912.py
-rw-rw-r-- 1 swot swot 1332 2月 24 19:12 0080_auto_20170224_1912.pyc
-rw-rw-r-- 1 swot swot 531 2月 24 19:19 0081_product_mortgage.py
-rw-rw-r-- 1 swot swot 908 2月 24 19:19 0081_product_mortgage.pyc
-rw-rw-r-- 1 swot swot 2564 3月 3 11:11 0082_auto_20170303_1111.py
-rw-rw-r-- 1 swot swot 2111 3月 3 11:12 0082_auto_20170303_1111.pyc
-rw-rw-r-- 1 swot swot 1169 3月 3 14:20 0083_auto_20170303_1420.py
-rw-rw-r-- 1 swot swot 1278 3月 3 14:20 0083_auto_20170303_1420.pyc
-rw-rw-r-- 1 swot swot 1332 3月 3 14:27 0084_auto_20170303_1427.py
-rw-rw-r-- 1 swot swot 1445 3月 3 14:27 0084_auto_20170303_1427.pyc
-rw-rw-r-- 1 swot swot 1131 3月 6 14:04 0085_auto_20170306_1404.py
-rw-rw-r-- 1 swot swot 1673 3月 6 14:05 0085_auto_20170306_1404.pyc
-rw-rw-r-- 1 swot swot 578 3月 9 08:34 0086_auto_20170309_0834.py
-rw-rw-r-- 1 swot swot 939 3月 9 08:34 0086_auto_20170309_0834.pyc
-rw-rw-r-- 1 swot swot 0 5月 1 2016 init.py
-rw-rw-r-- 1 swot swot 141 5月 1 2016 init.pyc

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,736评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,167评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,442评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,902评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,302评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,573评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,847评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,562评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,260评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,531评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,021评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,367评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,016评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,068评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,827评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,610评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,514评论 2 269

推荐阅读更多精彩内容