Skip to content

Fixed #24522 -- Added a --shuffle option to DiscoverRunner. - #14137

Merged
felixxm merged 1 commit into
django:mainfrom
cjerdonek:ticket-24522-random-test-order
Jul 8, 2021
Merged

felixxm merged 1 commit into
django:mainfrom
cjerdonek:ticket-24522-random-test-order

Conversation

@cjerdonek

Copy link
Copy Markdown
Contributor

@cjerdonek
cjerdonek force-pushed the ticket-24522-random-test-order branch 6 times, most recently from 9d1a449 to 61fc7c7 Compare March 16, 2021 20:47
@cjerdonek
cjerdonek force-pushed the ticket-24522-random-test-order branch from 61fc7c7 to 26b01ff Compare March 24, 2021 10:45
@cjerdonek

Copy link
Copy Markdown
Contributor Author

Resolved conflicts following: #14145

@cjerdonek
cjerdonek force-pushed the ticket-24522-random-test-order branch from 26b01ff to 4e79cf6 Compare March 24, 2021 13:23
@cjerdonek
cjerdonek force-pushed the ticket-24522-random-test-order branch from 4e79cf6 to 5ec1583 Compare April 3, 2021 01:25
@cjerdonek

Copy link
Copy Markdown
Contributor Author

Rebased.

@felixxm felixxm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cjerdonek Thanks 👍 Looks great 🚀

Comment thread docs/ref/django-admin.txt Resolved
Comment thread docs/releases/4.0.txt Resolved
Comment thread docs/releases/4.0.txt Resolved
Comment thread docs/ref/django-admin.txt Resolved
Comment thread tests/runtests.py Resolved
@felixxm

felixxm commented Apr 22, 2021 •

Copy link
Copy Markdown
Member

I also wonder if can we do anything to make it easier to find a set of tests with an isolation issue, e.g.

Testing against Django installed in '/django/django' with up to 8 processes
Using shuffle seed: 9154518955 (given)
Found 743 tests.
./runtests.py migrations schema --shuffle=9154518955
======================================================================
ERROR: test_migrate_with_system_checks (migrations.test_commands.MigrateTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/lib/python3.8/unittest/case.py", line 60, in testPartExecutor
    yield
  File "/usr/lib/python3.8/unittest/case.py", line 676, in run
    self._callTestMethod(testMethod)
  File "/usr/lib/python3.8/unittest/case.py", line 633, in _callTestMethod
    method()
  File "/django/django/test/utils.py", line 430, in inner
    return func(*args, **kwargs)
  File "/django/tests/migrations/test_commands.py", line 78, in test_migrate_with_system_checks
    call_command('migrate', skip_checks=False, no_color=True, stdout=out)
  File "/django/django/core/management/__init__.py", line 181, in call_command
    return command.execute(*args, **defaults)
  File "/django/django/core/management/base.py", line 398, in execute
    output = self.handle(*args, **options)
  File "/django/django/core/management/base.py", line 89, in wrapped
    res = handle_func(*args, **kwargs)
  File "/django/django/core/management/commands/migrate.py", line 75, in handle
    self.check(databases=[database])
  File "/django/django/core/management/base.py", line 469, in check
    raise SystemCheckError(msg)
django.core.management.base.SystemCheckError: SystemCheckError: System check identified some issues:

ERRORS:
unmigrated_app.SillyModel.silly_tribble: (fields.E307) The field unmigrated_app.SillyModel.silly_tribble was declared with a lazy reference to 'migrations.tribble', but app 'migrations' isn't installed.

I really tried but couldn't find a reason for this error.

@felixxm

felixxm commented Apr 22, 2021

Copy link
Copy Markdown
Member

In most of cases isolation issue is caused by a test from the same class, but you must to run them in a specific order that is generated only if you call reorder_tests() with the same shuffler and all tests. I was able to find a pair of test by removing test classes manually from all_tests:

$ ./runtests.py migrations.test_commands.MigrateTests.test_showmigrations_unmigrated_app migrations.test_commands.MigrateTests.test_migrate_with_system_checks --parallel=1
Testing against Django installed in '/django/django'
Found 2 tests.
Creating test database for alias 'default'...
Creating test database for alias 'other'...
System check identified no issues (0 silenced).
.E
======================================================================
ERROR: test_migrate_with_system_checks (migrations.test_commands.MigrateTests)
----------------------------------------------------------------------
...

@cjerdonek

cjerdonek commented Apr 22, 2021 •

Copy link
Copy Markdown
Contributor Author

Thanks for your good comments, @felixxm!

I also wonder if can we do anything to make it easier to find a set of tests with an isolation issue, e.g.

Do you mean making it easier to find some new error in the test suite, or do you mean, once you do have some error, making it easier to find a minimal set of tests causing that same error?

@felixxm

felixxm commented Apr 22, 2021

Copy link
Copy Markdown
Member

Do you mean making it easier to find some new error in the test suite, or do you mean, once you do have some error, making it easier to find a minimal set of tests causing that same error?

The second one, finding a minimal set of tests. In most of causes the order of the tests is crucial, so you need to have the same order but you don't need to run all tests. In described case it was enough to run tests in the same class (see #14300). Nevermind (grumbling... 😄 ) , let's do not complicate it, I can always grep output, etc.

@cjerdonek

Copy link
Copy Markdown
Contributor Author

Ah, I actually think I might know just the solution for this (learned from another context).

But just to clarify, is one of the issues that when you restrict to a smaller set of tests, the order changes, even if you use the same shuffle seed?

@felixxm

felixxm commented Apr 22, 2021

Copy link
Copy Markdown
Member

But just to clarify, is one of the issues that when you restrict to a smaller set of tests, the order changes, even if you use the same shuffle seed?

Yes.

@cjerdonek

Copy link
Copy Markdown
Contributor Author

Okay, so this is the idea that might be worth using then: https://arxiv.org/abs/1808.10016

It's called "consistent sampling." It's not too hard to implement and has that property (order is preserved when restricted to any subset). It might be better to do separately in a subsequent PR.

@felixxm

felixxm commented Apr 23, 2021

Copy link
Copy Markdown
Member

It might be better to do separately in a subsequent PR.

Yes, this is a separate issue,

@cjerdonek
cjerdonek force-pushed the ticket-24522-random-test-order branch 4 times, most recently from ab422fe to 6b97061 Compare April 25, 2021 11:04
@cjerdonek

Copy link
Copy Markdown
Contributor Author

@felixxm I went ahead and implemented the consistent sampling now because it requires a slightly incompatible API (the shuffler's shuffle() method needs a key argument).

@cjerdonek
cjerdonek force-pushed the ticket-24522-random-test-order branch 4 times, most recently from 519c4d9 to 27087fc Compare April 27, 2021 07:50
Comment thread django/test/runner.py Resolved
Comment thread django/test/runner.py Resolved
Comment thread django/test/runner.py Resolved
Comment thread django/test/runner.py Resolved
Comment thread tests/runtests.py Resolved
@cjerdonek
cjerdonek force-pushed the ticket-24522-random-test-order branch from 27087fc to 117bc9d Compare June 2, 2021 03:21
@cjerdonek

Copy link
Copy Markdown
Contributor Author

Thanks a lot for the review and comments, @adamchainz. I addressed each of your comments by either replying with an explanation or making your suggested edits in the code.

@cjerdonek

Copy link
Copy Markdown
Contributor Author

FYI, I found a bug in other code while trying out this PR:

$ ./runtests.py --shuffle 100 test_runner

See: https://code.djangoproject.com/ticket/32808

@cjerdonek
cjerdonek force-pushed the ticket-24522-random-test-order branch 2 times, most recently from 5a66d1d to c1274ff Compare June 5, 2021 00:09
@cjerdonek

Copy link
Copy Markdown
Contributor Author

Resolved conflicts.

@felixxm felixxm self-assigned this Jul 2, 2021

@felixxm felixxm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cjerdonek Thanks for updates 🥇 I left small comments and discovered two more isolation issues while testing this patch (1ff0ea6, #14595), 5 in total (90916f0, ac2e6e6, 2f435e7) 🔥

Comment thread django/test/runner.py Resolved
Comment thread docs/releases/4.0.txt Resolved
Comment thread docs/ref/django-admin.txt Resolved
Comment thread docs/ref/django-admin.txt Resolved
Comment thread docs/topics/testing/overview.txt Resolved
Comment thread docs/topics/testing/advanced.txt Resolved
Comment thread django/test/runner.py Resolved
Comment thread django/test/runner.py Resolved
@cjerdonek

Copy link
Copy Markdown
Contributor Author

@cjerdonek Thanks for updates 🥇 I left small comments and discovered two more isolation issues while testing this patch (1ff0ea6, #14595), 5 in total (90916f0, ac2e6e6, 2f435e7) 🔥

Thanks, @felixxm!! PR #14478 is another isolation issue I found using this option and that you fixed..

@cjerdonek
cjerdonek force-pushed the ticket-24522-random-test-order branch 2 times, most recently from c4da4ac to 1fb4d77 Compare July 7, 2021 18:24
@cjerdonek

Copy link
Copy Markdown
Contributor Author

Thanks for all your review comments, @felixxm. I incorporated all of them (unless I missed something, which I don't think I did).

Comment thread tests/test_runner/tests.py Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved this class to a separate file.

@felixxm
felixxm force-pushed the ticket-24522-random-test-order branch from 1fb4d77 to 90ba716 Compare July 8, 2021 05:29
@felixxm

felixxm commented Jul 8, 2021

Copy link
Copy Markdown
Member

@cjerdonek Many thanks 🚀 🏆

@felixxm
felixxm merged commit 90ba716 into django:main Jul 8, 2021
@cjerdonek
cjerdonek deleted the ticket-24522-random-test-order branch July 24, 2021 20:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants