2

I'm using django 1.3 and writing some selenium test & django unit tests. I want to know if its possible to run the tests without creating the databases & loading fixtures everytime?

I stumbled upon this SO thread which gives a good way to test without creating a database but that still flushes the database fixtures & reloads them every time. I do not want even that to happen. I just want the tests to read / write the database I have set up once. I do not want it to create database / load fixtures every time I run any test.

I'd be glad to provide any further info if required to sort this out.

Thanks in advance! :)

Community
  • 1
  • 1
Sandeep Raju Prabhakar
  • 18,652
  • 8
  • 35
  • 44
  • Is this what you are looking for? http://stackoverflow.com/questions/4606756/how-can-i-specify-a-database-for-django-tests-to-use-instead-of-having-it-build – Ngenator Jul 16 '13 at 20:40
  • @Ngenator tried that out but I am getting some errors, `DatabaseError: relation "django_content_type" does not exist LINE 1: ..."."app_label", "django_content_type"."model" FROM "django_co...` – Sandeep Raju Prabhakar Jul 16 '13 at 21:25
  • I'm not sure what the problem is. I actually don't believe this is a good idea though, the point of setting up and tearing down the database each time is for consistency. The fact that you plan on using an existing database may invalidate some of your tests due to the fact that models can change and data may not be valid at some point in the future. – Ngenator Jul 16 '13 at 22:10
  • yes, I understand. But how will I test during writing the test cases if it takes a lot of time for each run. – Sandeep Raju Prabhakar Jul 16 '13 at 22:40
  • 1
    You can run individual tests https://docs.djangoproject.com/en/dev/topics/testing/overview/#running-tests Just make sure you only use fixtures that are necessary for the tests, there is no need to fill the database with useless info that isn't relevant. Setting up and tearing down the database shouldn't really be taking that long, if it is, you should look into it, there may be something set up incorrectly. Also, you shouldn't be writing tests in a "try, fix, try, fix" way, tests that are properly designed ahead of time should be very simple to implement. – Ngenator Jul 16 '13 at 22:51

1 Answers1

1

I was able to do this by hacking into some django code. The parts that need to be edited is,

FILE: django/db/backends/sqlite3/creation.py

change the code as follows:

  1. setting confirm = 'yes' in line 55
  2. commenting out all occurrences of os.remove(test_database_name)

FILE: django/db/backends/creation.py

change the code as follows

  1. comment 359 to 376 (the syncdb & flush part in create_test_db function.
  2. almost everything in _create_test_db. (almost everything == code part which does the undesired stuff which we are trying to eliminate)
  3. almost everything in _destroy_test_db.
  4. almost everything in destroy_test_db.

Hope that helps!

Sandeep Raju Prabhakar
  • 18,652
  • 8
  • 35
  • 44