Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django unittest's: How to set up temporary view with temporary url conf for unittest's

I created a couple Middleware classes and want to test them against some real views now. Since my app is not including any views or url conf itself I was wondering if there is a way to temporary create views and url configuration just for testing (similar to a test database) within Django's test framework. In the end I would like to use the test client to check these temporary views against errors coming from the Middleware. I didn't find some solution for this in the docs.

I know I could create a test view within my app (in views.py) and point from the projects ROOT_URLCONF to it, but I don't want to force a user to do this when using my app and wanting to test my Middleware classes.

Any ideas?

Thanks.

like image 907
Torsten Engelbrecht Avatar asked Apr 08 '11 13:04

Torsten Engelbrecht


1 Answers

Yes this is possible if you are using the django.test.TestCase. Here are the docs on setting the url configuration for a specific test case: https://docs.djangoproject.com/en/1.8/topics/testing/tools/#urlconf-configuration

When I have done this in the past I typically break up my test suite like a sub-app (without models):

tests
    __init__.py
    urls.py
    views.py
    base.py

Then in the test case you would set:

class MiddlewareTestCase(TestCase):
    urls = 'appname.tests.urls'
like image 72
Mark Lavin Avatar answered Jan 02 '23 07:01

Mark Lavin