Home:ALL Converter>Django: test successful loading of static files

Django: test successful loading of static files

Ask Time:2012-12-05T19:26:59         Author:Amir Ali Akbari

Json Formatter

Is it possible to test successful loading of static page components in Django automatic test?

For example, using django test client, besides testing the client.get('x').status_code to be 200, I want to test if all page's static resources (linked css and js files) are successfully loaded.

If its not possible using client, Is there a plugin or suplementary test system, e.g. Selenium, to enable such type of tests?

Author:Amir Ali Akbari,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/13722228/django-test-successful-loading-of-static-files
Akshar Raaj :

I guess you want to find out if django's staticfile finder is able to find your static resources.\n\nConsider your app name is my_app. You have a folder called static inside it. You again have a folder called my_app inside it which contains your static resource, let's say some image named 'test.jpg'.\n\nmyapp\n|-- __init__.py \n`-- models.py\n`-- static\n `-- myapp\n `-- test.jpg\n\nfrom django.contrib.staticfiles import finders\nfrom django.contrib.staticfiles.storage import staticfiles_storage\n\nclass TestStaticFiles(TestCase):\n\"\"\"Check if app contains required static files\"\"\"\ndef test_images(self):\n abs_path = finders.find('myapp/test.jpg')\n self.assertTrue(staticfiles_storage.exists(abs_path))\n\n\nfinders looks inside static/ subdirectory of all apps to find the static resource.",
2013-04-17T16:44:55
yy