vendredi 24 février 2017

Django Same Test fails sometimes and sometimes is successful

I created a Test for my own Admin LogEntry Message. So sometimes this Test is successful and sometimes it fails. I can't understand why. Do you see the mistake here? I use this code to test my app:

python3 manage.py test

Here is the TestCase:

class AdminLogTests(TestCase):
    @classmethod
    def setUpTestData(cls):
        cls.admin_user = ProfileUser.objects.create(
            email='test_admin@test.de', first_name='Admin', family_name='Testadmin',
            is_active=True, is_superuser=True)
        cls.admin_user.set_password('testtest')
        cls.admin_user.save()

    def setUp(self):
        self.client = Client()

    def test_log_admin_create_user(self):
        self.client.login(email='test_admin@test.de', password='testtest')
        response = self.client.post('/admin/addressbook/profileuser/add/', {
            'address': 'Herr', 'first_name': 'Max', 'family_name': 'Meiser', 
            'email': 'test1@test.test', 'password1': 'testtest', 
            'password2': 'testtest', })
        test1_user = ProfileUser.objects.get(email='test1@test.test')
        response = LogEntry.objects.filter(
            action_flag=1, object_id=test1_user.pk)
        print(response)
        response = self.client.get('/admin/addressbook/profileuser/{}/history/'.format(test1_user.pk))
        self.assertContains(response, 'Admin Testadmin added Max Meiser')
        test1_user.delete()

And here the error:

FAIL: test_log_admin_create_user (addressbook.tests.AdminLogTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/app/addressbook/tests.py", line 180, in test_log_admin_create_user
self.assertContains(response, 'Admin Testadmin added Max Meiser')
  File "/usr/local/lib/python3.5/dist-packages/django/test/testcases.py", line 382, in assertContains
    self.assertTrue(real_count != 0, msg_prefix + "Couldn't find %s in response" % text_repr)
AssertionError: False is not true : Couldn't find 'Admin Testadmin added Max Meiser' in response

Aucun commentaire:

Enregistrer un commentaire