Indeed, it was that obvious.

class OrderBugTest(TestCase):

    @classmethod
    def setUpTestData(cls):
        cls.order = OrderFactory()
        cls.order.go_available()
        cls.order.go_suspended()

    def setUp(self):
        self.order.refresh_from_db()

    def test_1(self):
        """ Ok. """
        self.order.go_completed()
        self.assertEqual(self.order.state, 'completed')

    def test_2(self):
        """ Fails, order is modified from test_1. """
        self.assertEqual(self.order.state, 'suspended')

Thanks!

On Monday, 1 June 2015 15:06:31 UTC+2, Tim Graham wrote:
>
> It looks like you may be using in-memory models instead of saving them to 
> the database? In case your go_available() method does save the 
> OrderFactory instance, you'll still need to refresh the object from the 
> database using Model.refresh_from_db() in test_2().
>
> On Monday, June 1, 2015 at 5:20:03 AM UTC-4, Gagaro wrote:
>>
>> Hi,
>>
>> I have an hard time understanding how I am supposed to use setUpTestData. 
>> The documentation isn't very clear about it (
>> https://docs.djangoproject.com/en/1.8/topics/testing/tools/#django.test.TestCase.setUpTestData
>> ).
>>
>> "The class-level atomic block described above allows the creation of 
>> initial data at the class level, once for the whole TestCase.". and "Note 
>> that if the tests are run on a database with no transaction support (for 
>> instance, MySQL with the MyISAM engine), setUpTestData() will be called 
>> before each test, negating the speed benefits." let me think that the 
>> changes made by the test should be rollbacked after each test.
>>
>> So I would expect the following to works:
>>
>> class OrderBugTest(TestCase):
>>
>>     @classmethod
>>     def setUpTestData(cls):
>>         cls.order = OrderFactory()
>>         cls.order.go_available()
>>         cls.order.go_suspended()
>>
>>     def test_1(self):
>>         """ Ok. """
>>         self.order.go_completed()
>>         self.assertEqual(self.order.state, 'completed')
>>
>>     def test_2(self):
>>         """ Fails, order is modified from test_1. """
>>         self.assertEqual(self.order.state, 'suspended')
>>
>> Does this means only static data should be created this way ? Can't I 
>> modify data created this way in the test ? Am I doing something wrong ?
>>
>> Thanks.
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/226699fb-5248-4ecc-b371-17f778e819d4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to