Login

local django test

Author:
jedie
Posted:
May 25, 2007
Language:
Python
Version:
.96
Tags:
testing test local-test
Score:
2 (after 2 ratings)

Sometimes I would like to test a codesnippet without a complete django environment. Here is a small "local test script" you can use for this ;)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
    A small local django test.
    ~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    Sometimes I would like to test a codesnippet without a complete
    django environment. Here is a small "local test script" you can
    use for this ;)

    http://www.djangosnippets.org/snippets/252/
    http://www.python-forum.de/topic-10600.html (de)

    :copyleft: 2007 by Jens Diemer
    :license: GNU GPL
"""

import os, sys

# insert django to the sys.path (optional):
#os.chdir("../django/")
#sys.path.insert(0, os.getcwd())

from django.core import management
from django.conf import settings

# setup a fake settings module
os.environ["DJANGO_SETTINGS_MODULE"] = "django.conf.global_settings"
settings.__file__ = "./django/conf/global_settings.py"

# Use a SQLite memory database for the local test:
settings.DATABASE_ENGINE = "sqlite3"
settings.DATABASE_NAME = ":memory:"

# modify some settings for you tests, if needed:
settings.INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
)

# Create the django database and environment:
print "init django, create tables...",
management.setup_environ(settings)
management.call_command('syncdb', verbosity=0, interactive=False)
print "OK\n"

#______________________________________________________________________________
# Now, you can play. Put your test code here:

from django.contrib.auth.models import User
from django import newforms as forms

UserForm = forms.form_for_model(User)
form = UserForm()
html_code = form.as_p()

print html_code

More like this

  1. django soaplib test client by erny 6 years, 5 months ago
  2. local django model test by jedie 6 years, 11 months ago
  3. Fake SSL Middleware for Tests and Local Development by DrMeers 5 years, 4 months ago
  4. Localizing a ChoiceField by eRAZOR 7 years, 1 month ago
  5. Db Mock by akhavr 8 years, 1 month ago

Comments

Please login first before commenting.