[Django] #33762: test_random_ordering not really testing randomization

9 views
Skip to first unread message

Django

unread,
Jun 1, 2022, 8:33:52 PM6/1/22
to django-...@googlegroups.com
#33762: test_random_ordering not really testing randomization
------------------------------------------------+------------------------
Reporter: Sicong | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Testing framework | Version:
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
------------------------------------------------+------------------------
Unit test
[https://github.com/django/django/blob/6f73eb9d90cfec684529aab48d517e3d6449ba8c/tests/ordering/tests.py#L235
test_random_ordering] only checks the length of result queryset. It's not
checking if order_by("?") giving same result every time.

My proposed solution is like this. It's very unlikely that all 10 items
generated with order_by("?") are the same.
{{{
def test_random_ordering(self):
"""
Use '?' to order randomly.
"""
articles = []
# Iterate 10 times, items in result set shouldn't be all the same
for _ in range(10):
articles.append(list(Article.objects.order_by("?")))

self.assertNotEqual(articles.count(articles[0]), 10)

}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/33762>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jun 1, 2022, 8:34:04 PM6/1/22
to django-...@googlegroups.com
#33762: test_random_ordering not really testing randomization
-------------------------------------+-------------------------------------
Reporter: Sicong | Owner: Sicong
Type: | Status: assigned
Cleanup/optimization |
Component: Testing framework | Version:
Severity: Normal | Resolution:

Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sicong):

* owner: nobody => Sicong
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/33762#comment:1>

Django

unread,
Jun 2, 2022, 12:16:25 AM6/2/22
to django-...@googlegroups.com
#33762: test_random_ordering not really testing randomization
-------------------------------------+-------------------------------------
Reporter: Sicong | Owner: Sicong
Type: | Status: closed
Cleanup/optimization |
Component: Core (Other) | Version:
Severity: Normal | Resolution: wontfix

Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* status: assigned => closed
* resolution: => wontfix
* component: Testing framework => Core (Other)


Comment:

> It's very unlikely that all 10 items generated with order_by("?") are
the same.

Unfortunately, it's still possible. I don't see much value in adding a
non-deterministic flaky test.

--
Ticket URL: <https://code.djangoproject.com/ticket/33762#comment:2>

Reply all
Reply to author
Forward
0 new messages