• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2016 Google Inc. All Rights Reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15"""Setups the Django test environment and provides helper classes."""
16
17import django
18from django import test
19from django.contrib.sessions.backends.file import SessionStore
20from django.test.runner import DiscoverRunner
21
22django.setup()
23default_app_config = 'tests.contrib.django_util.apps.AppConfig'
24
25
26class TestWithDjangoEnvironment(test.TestCase):
27    @classmethod
28    def setUpClass(cls):
29        django.setup()
30        cls.runner = DiscoverRunner()
31        cls.runner.setup_test_environment()
32        cls.old_config = cls.runner.setup_databases()
33
34    @classmethod
35    def tearDownClass(cls):
36        cls.runner.teardown_databases(cls.old_config)
37        cls.runner.teardown_test_environment()
38
39    def setUp(self):
40        self.factory = test.RequestFactory()
41
42        store = SessionStore()
43        store.save()
44        self.session = store
45