Merge pull request #6 from mozilla/py.test-setup

add py.test setup with support for coverage
This commit is contained in:
jeads 2013-03-12 10:02:17 -07:00
Родитель 737ad35407 a2b0a00fcb
Коммит bb3d5c2b35
3 изменённых файлов: 70 добавлений и 0 удалений

1
.gitignore поставляемый
Просмотреть файл

@ -37,3 +37,4 @@ nosetests.xml
.vagrant
#local settings
treeherder/settings/local.py
htmlcov/

3
runtests.sh Normal file
Просмотреть файл

@ -0,0 +1,3 @@
#!/bin/sh
py.test tests/$* --cov-report html --cov treeherder

66
tests/conftest.py Normal file
Просмотреть файл

@ -0,0 +1,66 @@
import os
def pytest_sessionstart(session):
"""
Set up the test environment.
Set DJANGO_SETTINGS_MODULE and sets up a test database.
"""
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "treeherder.settings.base")
from django.conf import settings
from django.test.simple import DjangoTestSuiteRunner
# we don't actually let Django run the tests, but we need to use some
# methods of its runner for setup/teardown of dbs and some other things
session.django_runner = DjangoTestSuiteRunner()
# this provides templates-rendered debugging info and locmem mail storage
session.django_runner.setup_test_environment()
# support custom db prefix for tests for the main datazilla datasource
# as well as for the testproj and testpushlog dbs
prefix = getattr(settings, "TEST_DB_PREFIX", "")
settings.DATABASES["default"]["TEST_NAME"] = "{0}test_treeherder".format(prefix)
# this sets up a clean test-only database
session.django_db_config = session.django_runner.setup_databases()
def pytest_sessionfinish(session):
"""Tear down the test environment, including databases."""
print("\n")
session.django_runner.teardown_databases(session.django_db_config)
session.django_runner.teardown_test_environment()
def pytest_runtest_setup(item):
"""
Per-test setup.
Start a transaction and disable transaction methods for the duration of the
test. The transaction will be rolled back after the test. This prevents any
database changes made to Django ORM models from persisting between tests,
providing test isolation.
"""
from django.test.testcases import disable_transaction_methods
from django.db import transaction
transaction.enter_transaction_management()
transaction.managed(True)
disable_transaction_methods()
def pytest_runtest_teardown(item):
"""
Per-test teardown.
Roll back the Django ORM transaction
"""
from django.test.testcases import restore_transaction_methods
from django.db import transaction
restore_transaction_methods()
transaction.rollback()
transaction.leave_transaction_management()