'Django selenium - StaticLiveServerTestCase - data initialized setUpClass: data available in first method called but not in the second method called

EDIT 15/03/2022

as mentionned in this post, should use fixtures: Django's StaticLiveServerTestCase test pass alone but fail when grouped

So, I manage to use fixture. But for don't know why, one user can not login (but not for other) so that test fail

but I will open another post because issue do not correspond to title


I have issue when initializing data in setUpClass with StaticLiveServerTestCase.

I have define a method create_test_data() where I create all my objects (user, etc...).

Then I have defined a class method with setUpClass (where create_test_data() is called) and 2 methods for 2 differents tests.

My first test pass but not the second test. It seems that whereas data initialized are available for the first method/test1 there are no more available for the second/test2 method.

Test pass if there run separatly.

I tried using setUp method and SetUpTestData class method but neither works better.

test.py

def create_test_data():
   # objects created

class L_access_menu_creation_patient_TestCase(StaticLiveServerTestCase):
    
    fixtures = ['dumpdata.json'] # => using fixture

    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        cls.selenium = WebDriver()
        cls.selenium.implicitly_wait(1)
        cls.selenium.maximize_window()
        cls.date_saisie = timezone.now()
        cls.selenium.get(cls.live_server_url)
        # create_test_data() => deactivated

    # test1
    def test_menu_create_available_admin(self):
        username_input = self.selenium.find_element_by_name("username")
        username_input.send_keys('admin')
        password_input = self.selenium.find_element_by_name("password")
        password_input.send_keys('admin')

        # click sur le bouton 'connexion' pour valider login/mdp
        self.selenium.find_element_by_xpath('/html/body/form/button').click()

        # vérifier que le menu est affiché
        self.assertTrue(self.selenium.find_element_by_id('createmenu').is_enabled())

        # click sur le bouton 'deconnexion'
        self.selenium.find_element_by_id('dropdownMenuButton').click()
        self.selenium.find_element_by_xpath('/html/body/nav/div/div/a[3]').click()     
   
    # test2
    def test_menu_create_available_test1(self):

        # add permission to user test1
        self.test1 = User.objects.get(username='test1')
         content_type = ContentType.objects.get_for_model(Patient)
        permission = Permission.objects.get(
            codename='add_patient',
            content_type=content_type,
        )
        self.test1.user_permissions.add(permission)
        # same test as above for admin user

error raised: django.contrib.auth.models.User.DoesNotExist: User matching query does not exist.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source