Opened 2 years ago

Closed 2 years ago

#33762 closed Cleanup/optimization (wontfix)

test_random_ordering not really testing randomization

Reported by: Sicong Owned by: Sicong
Component: Core (Other) Version:
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Unit test 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)

Change History (2)

comment:1 by Sicong, 2 years ago

Owner: changed from nobody to Sicong
Status: newassigned

comment:2 by Mariusz Felisiak, 2 years ago

Component: Testing frameworkCore (Other)
Resolution: wontfix
Status: assignedclosed

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.

Note: See TracTickets for help on using tickets.
Back to Top