This is a py.test plugin to help you test your django application.
supports python 2.6-3.2 and django 1.4-1.5
- support pytest-xdist
- currently each node will create its own database (there is not way to run transaction tests synchronously on a single node)
- support for nested setup_module/setup_class
- every module or class is surrounded with savepoint
- lazy savepoints
- savepoints won't be created unless your test function and/or fixture accessing database saving few round-trips
- djangos TransactionTestCase, TestCase and plain unittests TestCase are supported.
pip install git+https://github.com/krya/pydjango#egg=pydjango
First of all you need to make this plugin aware of your django settings. To do so you have few options:
make an environment variable:
export DJANGO_SETTINGS_MODULE=myproject.settings
pass settings path to py.test invocation:
py.test --django-settings=myproject.settings
make a pytest.ini file in root folder with following content:
[pytest] DJANGO_SETTINGS_MODULE=myproject.settings
and finally you can set up your settings in python code. Just make a conftest.py file in root folder with:
from django.conf import settings def pytest_configure(): settings.configure(DATABASES={'default':{'ENGINE':'django.db.backends.sqlite3'}})
Now can run tests with py.test conmmand.
- client
- Client instance
- test_user
- user instace with username test
- admin_user
- superuser instance with username admin
- anon_user
- AnonymousUser instance
- settings
- django's settings module
- uclient
- a client instance with logged in test_user
- aclient
- a client instance with logged in admin_user
both uclient and aclient are logged in using django.contrib.auth.backends.ModelBackend.
There are also imported apps available as fixtures named by subpackage name. So for instance if you have django.contrib.auth in your INSTALLED_APPS you can use that package in your tests without importing it in every test function:
def test_smth(auth):
assert auth.models.User.objects.count() == 0
You can even use model's name defined in your project as a fixture:
def test_my_model(User):
assert User.objects.count() == 0
- --create-db
- Force database creation. Destroys db if it exists from previous run
- --skip-trans
- skip all transactional tests (LiveServerTestCase or TransactionTestCase)
- --migrate
- runs syncdb and south migrations (may be slow in big projects so let user decide). does nothing when --create-db used