Opened 5 years ago

Closed 4 years ago

Last modified 4 years ago

#17312 closed Cleanup/optimization (fixed)

Testing examples should use django.utils.TestCase

Reported by: jcspray@… Owned by: nobody
Component: Documentation Version: master
Severity: Normal Keywords:
Cc: krw1243 Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

The 'Testing Django applications page' (https://docs.djangoproject.com/en/dev/topics/testing/) gives many examples of tests using unittest2.TestCase directly rather than django.test.TestCase.

For the DB to be reset between tests it is essential to use django.test.TestCase, and that is not made at all obvious in this documentation.

  • At least: a prominent warning should be present about the fact that DB side effects are possible when using unittest2.TestCase
  • Preferably: the examples should use django.utils.TestCase since it is a safer default.

Attachments (1)

17312.diff (2.1 KB) - added by Tim Graham 4 years ago.

Download all attachments as: .zip

Change History (9)

comment:1 Changed 5 years ago by krw1243

Cc: krw1243 added
Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset

comment:2 Changed 5 years ago by Preston Holmes

Triage Stage: UnreviewedAccepted

comment:3 Changed 5 years ago by Aymeric Augustin

Triage Stage: AcceptedDesign decision needed

Within Django itself, the convention is to use unittest.TestCase for tests that don't need django.test.TestCase, although I'm not entirely sure about the reason (performance?).

I'd like to see a benchmark before recommending to use django.test.TestCase unconditionally.

Last edited 5 years ago by Aymeric Augustin (previous) (diff)

comment:4 Changed 5 years ago by Preston Holmes

As the differences are already documented: https://docs.djangoproject.com/en/dev/topics/testing/#testcase

perhaps it would suffice just to have a quick note about database effects and a forward link to that section of the docs at the bottom of the introduction "writing unit tests" (just before the "writing doctests" section)

https://docs.djangoproject.com/en/dev/topics/testing/#writing-unit-tests.

Changed 4 years ago by Tim Graham

Attachment: 17312.diff added

comment:5 Changed 4 years ago by Tim Graham

Has patch: set
Triage Stage: Design decision neededAccepted

I think a warning is definitely appropriate. In fact, the current example has database side effects.

comment:6 Changed 4 years ago by Claude Paroz

Triage Stage: AcceptedReady for checkin

comment:7 Changed 4 years ago by Tim Graham <timograham@…>

Resolution: fixed
Status: newclosed

In 7df03268a467a9aec9c4c574c85317a738ca33ae:

Fixed #17312 - Warned about database side effects in tests.

Thanks jcspray for the suggestion.

comment:8 Changed 4 years ago by Tim Graham <timograham@…>

In 2545dc59bf44131a1f60f7393f29f025939f3a09:

[1.5.x] Fixed #17312 - Warned about database side effects in tests.

Thanks jcspray for the suggestion.

Backport of 7df03268a467a9aec9c4c574c85317a738ca33ae from master.

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