Opened 8 years ago

Closed 8 years ago

#18271 closed Cleanup/optimization (fixed)

Make TransactionTestCase flush tables after test run instead of before

Reported by: Andreas Pelme Owned by: nobody
Component: Testing framework Version: 1.4
Severity: Normal Keywords:
Cc: Triage Stage: Design decision needed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


TransactionTestCase does not clean up after a test is run. When the next TransactionTestCase is run, tables are flushed and fixtures are loaded.

It would be better if the flush took place in _post_teardown, which would leave the database in a clean state after each test. Then test order does not matter, TransactionTestCase and TestCases can be executed in arbitrary order.

There is a related ticket #12408 to reorder doctests to also run before TransactionTestCase. If TransactionTestCase is changed, no reordering will be necessary.

django-developers discussion:

Change History (3)

comment:1 Changed 8 years ago by Andreas Pelme

Initial patch is available as a pull request:

Last edited 8 years ago by Andreas Pelme (previous) (diff)

comment:2 Changed 8 years ago by Anssi Kääriäinen

Component: UncategorizedTesting framework
Triage Stage: UnreviewedDesign decision needed

I am going to mark this design decision needed. I do think this is a good idea, but there are two problems:

  1. If a test case screws up cleanup it will cause problems for itself currently, after the patch it will cause problems for the next test case which makes debugging much harder.
  2. Is this somehow backwards incompatible. The behavior was documented.

I think neither of the above is a blocker issue. The first issue can be annoying, but is is pretty much there already - if the cleanup doesn't work correctly, the breakage will be dependent on the previous test case anyways. I can't come up with any reason why moving the flush from before to after would be an issue... The backwards compatibility does need more investigation, though.

The main reason why I am marking this DDN is that maybe there was some reason why the flush was ran before the tests. So, lets keep this in DDN until everybody has had time to participate in the django-developers thread. If there are no objections in a week or so I am fine with moving this forward.

comment:3 Changed 8 years ago by Ramiro Morales <cramm0@…>

Resolution: fixed
Status: newclosed

In [f758bdab5eec3e615598948dd5bcf9bb7b910c9d]:

Fixed #18271 -- Changed stage at which TransactionTestCase flushes DB tables.

Previously, the flush was done before the test case execution and now
it is performed after it.

Other changes to the testing infrastructure include:

  • TransactionTestCase now doesn't reset autoincrement sequences either (previous behavior can achieved by using reset_sequences.) With this, no implicit such reset is performed by any of the provided TestCase classes.
  • New ordering of test cases: All unittest tes cases are run first and doctests are run at the end.

THse changes could be backward-incompatible with test cases that relied
on some kind of state being preserved between tests. Please read the
relevant sections of the release notes and testing documentation for
further details.

Thanks Andreas Pelme for the initial patch. Karen Tracey and Anssi
Kääriäinen for the feedback and Anssi for reviewing.

This also fixes #12408.

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