0

I want to have different Testcase classes inside my test.py in my django project. One for Model testing (crud) one for view testing etc. I have the following classes

class EntryModelTest(TestCase):
    LOG = logging.getLogger(__name__)

    @classmethod
    def setup_database(cls):
       #database initialization


    @classmethod
    def setUpClass(cls):
        super(EntryModelTest, cls).setUpClass()
        cls.setup_database()
        cls.myModel = MyModel.objects.get(pk=1)
        #some queries (additions) that use things that were added in setup_database()

and a second testcase

class MyCalViewsTest(TestCase):
    LOG = logging.getLogger(__name__)

    @classmethod
    def setup_database(cls):
        #exactly the same code as above testcase(I know I could use Inheretiance. Will refactor)

    @classmethod
    def setUpClass(cls):
        #The same as above testcase
        cls.myModel = MyModel.objects.get(pk=1) #this gives the following error: DoesNotExist: PracticeData matching query does not exist.

The MyModel instance is created in setup_database method. I am not quite sure why. If the test database is destroyed after every testcase class is executed then the setup_database method should have recreated the MyModel instance with pk=1 so I would have existed. If it doesn't destroy the test database then the MyModel instance with pk=1 already exists so I shouldn't have any problems also. Any help would be appreciated.

Apostolos
  • 7,763
  • 17
  • 80
  • 150
  • This is very unclear. What does the setup_database method do? Django's TestCase class already creates the database and takes care of truncating it after every test. Why would you expect the pk autoincrement sequence to be reset? – Daniel Roseman Oct 16 '15 at 16:53
  • setup_database creates some model instances nothing more. I would expect th pk autoincrement sequence to be reset if the test database is destroyed after every test class.If not then the MyModel object with pk=1 should already exist in my test database thus it shouldn't give me the error. – Apostolos Oct 16 '15 at 16:59
  • But, your tests are showing that is not the case, and nothing in the documentation claims it does happen. The database is emptied, not destroyed. – Daniel Roseman Oct 16 '15 at 17:20
  • Why don't you use [model_mommy](https://github.com/vandersonmota/model_mommy)? ```mommy.make(MyModel, my_controlled_attribute='value')```. – raratiru Oct 16 '15 at 17:45
  • @DanielRoseman tests work when run individually, but not when run alltogether. Documentation states that database is destroyed when all tests have finished(https://goo.gl/wYfKyx). And I ask if you know what is a test. The test method inside a Testcase Class? The testcase class? – Apostolos Oct 16 '15 at 17:50

0 Answers0