Skip to content

Using --keepdb causes test failures #5969

Open
@florean

Description

@florean

Using the --keepdb options for testing, which saves about a minute of applying migrations, causes around a hundred test failures. It appears that some of the data created by migrations used by tests, namely users and courts, is getting deleted from the test database. If you also use --parallel, Django runs all tests against clones of the test database (test_courtlistener_1, etc.). In that case, the test database isn't changed and deleting those clones fixes the problem, but it isn't automatic, so you have to manually do that between test runs. So some test is deleting that data outside of a transaction.

One solution might be to convert tests to only use data they explicitly create, rather than relying on implicitly created objects from migrations, but it would probably be worth tracking down and eliminating the root cause.

Example runs using the command docker exec -e SELENIUM_DEBUG=1 -e SELENIUM_TIMEOUT=30 cl-django ./manage.py test cl --verbosity=3 --exclude-tag selenium --parallel=1 --timing --durations 10 --keepdb

initial run:

----------------------------------------------------------------------
Ran 1510 tests in 261.842s

OK (skipped=53)
Preserving test database for alias 'default' ('test_courtlistener_1')...
Preserving test database for alias 'default' ('test_courtlistener_2')...
Preserving test database for alias 'default' ('test_courtlistener')...
Total database setup took 62.094s
  Creating 'default' took 61.754s
  Cloning 'default' took 0.181s
  Cloning 'default' took 0.159s
Total database teardown took 0.000s
Total run took 327.935s

second run:

----------------------------------------------------------------------
Ran 1421 tests in 142.359s

FAILED (failures=5, errors=186, skipped=53)
Preserving test database for alias 'default' ('test_courtlistener_1')...
Preserving test database for alias 'default' ('test_courtlistener_2')...
Preserving test database for alias 'default' ('test_courtlistener')...
Total database setup took 0.693s
  Creating 'default' took 0.672s
  Cloning 'default' took 0.010s
  Cloning 'default' took 0.011s
Total database teardown took 0.000s
Total run took 145.636s

Some snipped example errors look like:

======================================================================
ERROR: test_make_a_docket_alert (cl.alerts.tests.tests.DocketAlertAPITests.test_make_a_docket_alert)
Can we make a docket alert?
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/opt/venv/lib/python3.13/site-packages/django/test/testcases.py", line 1400, in setUpClass
    cls.setUpTestData()
    ^^^^^^^^^^^
  File "/opt/courtlistener/cl/alerts/tests/tests.py", line 2600, in setUpTestData
    cls.court = Court.objects.get(id="scotus")
    ^^^^^^^^^^^^^^^
cl.search.models.Court.DoesNotExist: Court matching query does not exist.
======================================================================
ERROR: test_nda_get_document_number_fallback (cl.recap.tests.test_recap_email.GetDocumentNumberForAppellateDocuments.test_nda_get_document_number_fallback)
This test verifies if we can get the PACER document number for
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/local/lib/python3.13/unittest/case.py", line 603, in _callSetUp
    self.setUp()
    ~~~~~~~~~~^^
  File "/opt/courtlistener/cl/recap/tests/test_recap_email.py", line 2906, in setUp
    self.user = User.objects.get(username="recap-email")
    ^^^^^^^^^^^^^^^
django.contrib.auth.models.User.DoesNotExist: User matching query does not exist.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions