Opened 4 months ago

Closed 4 months ago

#35722 closed Cleanup/optimization (wontfix)

Specify behaviour in TransactionTestCase.reset_sequences

Reported by: Slava M. Owned by:
Component: Documentation Version: dev
Severity: Normal Keywords: docs, test, documentation
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description (last modified by Slava M.)

https://docs.djangoproject.com/en/dev/topics/testing/advanced/#django.test.TransactionTestCase.reset_sequences

I suggest specifying behaviour of TransactionTestCase.reset_sequences when used in subtests https://docs.python.org/3/library/unittest.html#distinguishing-test-iterations-using-subtests.

i.e.

class TestsThatDependsOnPrimaryKeySequences(TransactionTestCase):
    reset_sequences = True

    def test_animal_pk(self):
        with self.subTest():
            lion = Animal.objects.create(name="lion", sound="roar")
            self.assertEqual(lion.pk, 1)
        with self.subTest():
            cat = Animal.objects.create(name="cat", sound="meow")
            # will this still work?
            self.assertEqual(cat.pk, 1)

add to documentation 1 sentence along the lines of:

  • "reset_sequences won't work with unittest's subTest()"
  • or ""reset_sequences also works with unittest's subTest()"

Change History (3)

comment:1 by Slava M., 4 months ago

Description: modified (diff)

comment:2 by Simon Charette, 4 months ago

If we are to add a note about subTest, I'm not entirely convinced it's needed, I believe we should only focus on the behavior of reset_sequences but all the database isolation features that TransactionTestCase and TestCase does provide within the context of distinct test_ method. In other words, no per subTest data isolation (sequence, transactions, mailbox, etc) is implemented and within the context of a TransactionTestCase and friends a subTest context is solely a manner to annotate part of a test case method body with some context.

in reply to:  2 comment:3 by Sarah Boyce, 4 months ago

Resolution: wontfix
Status: newclosed

Replying to Simon Charette:

...a subTest context is solely a manner to annotate part of a test case method body with some context.

Exactly - subTest is a unittest feature, not a Django feature, and any setUp() or tearDown() etc happens per test (not per subTest). The reset_sequences behavior is consistent to this. Hence, I don't think we need to add documentation to Django on this

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