开发者

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

开发者 https://www.devze.com 2023-02-22 20:28 出处:网络
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 creat

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.


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'
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号