python - How to create django view inside tests -
i have utility, consist of middleware, , need test it. need view emulate user request , i'm trying create view inside test , use in testcase. don't know how create , assign test application.
i have settings test django application in run_tests.py
file:
from django.conf import settings, global_settings app_name = 'my_app' conf_kwargs = dict( databases={ 'default': { 'engine': 'django.db.backends.sqlite3', 'name': 'test.db', 'test_name': 'test.db' } }, site_id=1, middleware_classes=global_settings.middleware_classes + ('my_app.middleware.mymiddleware',), installed_apps=( 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', ) ) settings.configure(**conf_kwargs) django.test.utils import get_runner runner = get_runner(settings)() failures = runner.run_tests((app_name,))
and in tests.py
file i'm trying:
from django.contrib.auth.models import user django.http import httpresponse django.test import requestfactory, testcase def active_view(request): return httpresponse('<h1>test view has responsed<h1>') class activeuserstest(testcase): def setup(self): self.factory = requestfactory() self.user = user.objects.create_user(username='test', email='test@test.com', password='secret') def test_view(self): request = self.factory.get(active_view) request.user = self.user response = active_view(request) self.assertequal(response.status_code, 200)
should separate test application goals? example running django 1.6
Comments
Post a Comment