Opened 4 years ago

Closed 4 years ago

Last modified 3 years ago

#31395 closed New feature (fixed)

Make TestCase.setUpTestData enforce in-memory data isolation.

Reported by: Simon Charette Owned by: Simon Charette
Component: Testing framework Version: dev
Severity: Normal Keywords:
Cc: Adam Johnson 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 following is copied from a two years old django-developers thread that received an initial warm welcome.

Since then the third-party app implementing this feature has not received much feedback but I know a few developers have been using it and my usage in a few projects didn't surface any issues.

---

Django 1.8 introduced the TestCase.setUpTestData() class method as a mean to
speed up test fixtures initialization as compared to using setUp().

As I've come to use this feature and review changes from peers using it in
different projects the fact that test data assigned during its execution
couldn't be safely altered by test methods without compromising test isolation
has often be the source of confusion and frustration.

While the setUpTestData documentation mentions this limitation[1] and ways to
work around it by using refresh_from_db() in setUp() I believe it defeats
the whole purpose of the feature; avoiding unnecessary roundtrips to the
database to speed up execution. Given TestCase goes through great lengths to
ensure database level data isolation I believe it should do the same with class
level in-memory data assigned during setUpTestData.

In order to get rid of this caveat of the feature I'd like to propose an
adjustment to ensure such in-memory test data isolation.

Change History (7)

comment:1 by Adam Johnson, 4 years ago

Cc: Adam Johnson added
Version: 3.0master

comment:2 by Simon Charette, 4 years ago

Has patch: set

comment:3 by Mariusz Felisiak, 4 years ago

Owner: changed from nobody to Simon Charette
Status: newassigned
Triage Stage: UnreviewedAccepted

comment:4 by Adam Johnson, 4 years ago

Triage Stage: AcceptedReady for checkin

comment:5 by Mariusz Felisiak <felisiak.mariusz@…>, 4 years ago

Resolution: fixed
Status: assignedclosed

In 3cf80d3f:

Fixed #31395 -- Made setUpTestData enforce in-memory data isolation.

Since it's introduction in Django 1.8 setUpTestData has been suffering
from a documented but confusing caveat due to its sharing of attributes
assigned during its execution with all test instances.

By keeping track of class attributes assigned during the setUpTestData
phase its possible to ensure only deep copies are provided to test
instances on attribute retreival and prevent manual setUp gymnastic to
work around the previous lack of in-memory data isolation.

Thanks Adam Johnson for the extensive review.

comment:6 by Mariusz Felisiak <felisiak.mariusz@…>, 4 years ago

In 94f63b92:

Refs #31395 -- Relied on setUpTestData() test data isolation in various tests.

comment:7 by Mariusz Felisiak <felisiak.mariusz@…>, 3 years ago

In 2e10abe:

Refs #31395 -- Removed support for assigning objects which don't support deepcopy() in setUpTestData().

Per deprecation timeline.

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