#21540: TestCase with multiple assertRaises fails with 
TransactionManagementError
-----------------------------------+--------------------------------------
     Reporter:  marfire            |                    Owner:  nobody
         Type:  Bug                |                   Status:  closed
    Component:  Testing framework  |                  Version:  1.6
     Severity:  Normal             |               Resolution:  invalid
     Keywords:                     |             Triage Stage:  Unreviewed
    Has patch:  0                  |      Needs documentation:  0
  Needs tests:  0                  |  Patch needs improvement:  0
Easy pickings:  0                  |                    UI/UX:  0
-----------------------------------+--------------------------------------
Changes (by aaugustin):

 * status:  new => closed
 * needs_better_patch:   => 0
 * resolution:   => invalid
 * needs_tests:   => 0
 * needs_docs:   => 0


Comment:

 The correct code is:

 {{{
     def test_unique(self):
         with self.assertRaises(IntegrityError):
             # roll back transaction to a clean state before letting the
 IntegrityError bubble up
             with transaction.atomic():
                 NameModel.objects.create(first_name="John",
 last_name="Hancock")
         with self.assertRaises(IntegrityError):
             # roll back transaction to a clean state before letting the
 IntegrityError bubble up
             with transaction.atomic():
                 NameModel.objects.create(first_name="A", last_name="Doe")
 }}}

 This is explained in the documentation of the new transaction management
 in Django 1.6. You're seeing the expected (if not totally user-friendly)
 behavior.

 In Django 1.5, this would not blow up in tests, but it would blow up in
 actual code. I understand that you find this error annoying, but it least
 you get the same behavior in tests and in actual code in Django 1.6.

 I've spent lot of time thinking about this behavior and it's a Really Hard
 Problem (at least for me). If you have concrete suggestions, let's discuss
 on the django-developers mailing list. You may want to check past
 discussions first, as well as the videos of [http://myks.org/talks my
 talks] at DjangoCon Europe and DjangoCon US 2013 for more background on
 the design.

 I don't think it's a good idea to wrap the body of an assertRaises block
 in a transaction automatically as it may be used for many purposes that do
 not involve transactions.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/21540#comment:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/065.2c41b493b1c9238fb9d2f5e6fd0b68d3%40djangoproject.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to