简体   繁体   中英

Using pytest-django and setup takes 7 or 8 minutes?

I am setting up pytest testing for our Django site and it is taking seven or eight (7 or 8) minutes to finish running a single test in which less than a hundred objects are created. This feels like its going to be a massive impediment to developing tests if I can only try five things an hour (given that I will be sure to waste several of those tries on stupid mistakes like typos in filepaths.)

platform win32 -- Python 3.9.1, pytest-6.2.2, py-1.10.0, pluggy-0.13.1 Django 3.1.5

Our site uses django-simple-history (which creates a history counterpart of every table) and django-taggit, and has maybe 25 different apps, of which a couple provide widely reused models (if that matters). Could be 100 or more models all up. My laptop has 8Gb RAM, and an older i7 CPU.

I have set the reusedb option in pytest.ini, but there is no perceptible change. Is this a fixable problem?

This is the code:

import pytest

@pytest.mark.django_db
def test_proj_creation():
    for i in range(5):
        groupname = "G" + str(i)
        group = Group.objects.create(name=groupname)
        username = "U" + str(i)
        user = User.objects.create(username=username)
        user.set_password(username)
        user.groups.set(Group.objects.all())
        proj_name = "P" + str(i)
        project = Project.objects.create(project_name=proj_name)
        project.target_groups.add(group)
        assert user.groups.count() == i+1

When run:

========================================================================================================= test session starts =========================================================================================================
platform win32 -- Python 3.9.1, pytest-6.2.2, py-1.10.0, pluggy-0.13.1
django: settings: mysite6.settings_components.development (from ini)
rootdir: C:\Users\Andrew\AzureKEN\KEN%20development, configfile: pytest.ini
plugins: django-4.1.0
collected 1 item                                                                                                                                                                                                                       

tests\pytest_site.py

And this is where the long pause happens.

Then:

========================================================================================================== warnings summary ===========================================================================================================
tests/pytest_site.py::test_proj_creation
  c:\users\andrew\djdev\lib\site-packages\django\db\models\fields\__init__.py:1367: RuntimeWarning: DateTimeField HistoricalBRVM.last_modified received a naive datetime (1901-01-01 00:00:00) while time zone support is active.
    warnings.warn("DateTimeField %s received a naive datetime (%s)"

-- Docs: https://docs.pytest.org/en/stable/warnings.html
============================================================================================== 1 passed, 1 warning in 695.40s (0:11:35) ===============================================================================================

I'm going to answer my own question - the issue here is that

@pytest.mark.django_db

invokes creation of a blank sqlite database with all the tables created, which is a lot of work. This is configured by running through all the django migrations scripts, and that process requires many minutes. I can reduce this a bit by using the migration "squash" commands to reduce redundancy. The --reuse-db option doesn't seem to work (?) but in any case, we would normally expect to have database changes that would prevent reuse of an existing database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM