0

i have a initial_data command in my project and when i try to create my test database and run this command "py manage.py initial_data" it's not working because this command makes data in main database and not test_database, actually can't understand which one is test_database. so i give up to use this command,and i have other method to resolve my problem . and i decided to create manually my initial_data. initial_data command actually makes my required data in my other tables. and why i try to create data? because i have greater than 500 tests and this tests required to other test datas. so i have this tests:


class BankServiceTest(TestCase):


    def setUp(self):
        self.bank_model = models.Bank
        self.country_model = models.Country

    def test_create_bank_object(self):
        self.bank_model.objects.create(name='bank')
        re1 = self.bank_model.objects.get(name='bank')
        self.assertEqual(re1.name, 'bank')

    def test_create_country_object(self):
        self.country_model.objects.create(name='country', code=100)
        bank = self.bank_model.objects.get(name='bank')
        re1 = self.country_model.objects.get(code=100)
        self.assertEqual(re1.name, 'country')

i want after running "test create bank object " to have first function datas in second function actually this method Performs the command "initial_data".

so what is the method for this problem.

i used unit_test.TestCase but it not working for me. this method actually makes test data in main data base and i do not want this method. or maybe i used bad of this method.

actually i need to keep my data in running test.

Manoj Tolagekar
  • 1,816
  • 2
  • 5
  • 22
HOSSEIN
  • 1
  • 1
  • This isn't really possible, tests aren't really run in a specific order so forget chaining data between tests, `test_create_country_object` might run before `test_create_bank_object` causing your tests to fail. Each test should be independent of the other since you are doing **unit testing**. – Abdul Aziz Barkat Dec 01 '22 at 06:25
  • 1
    Does this answer your question? [Python / Django - How to get all methods in a unittest class to share the same database?](https://stackoverflow.com/questions/33323364/python-django-how-to-get-all-methods-in-a-unittest-class-to-share-the-same-d) Also see [Loading fixtures in django unit tests](https://stackoverflow.com/questions/2470634/loading-fixtures-in-django-unit-tests) – Abdul Aziz Barkat Dec 01 '22 at 06:34
  • Well, I have to say that I found a solution to my question , and found a method in testcases.py in django test and And I noticed that a function would rollback each of my tests after they finished. So I avoided this by overriding this function. – HOSSEIN Dec 03 '22 at 07:45

0 Answers0