【发布时间】:2010-03-28 15:41:27
【问题描述】:
我想要一个包含一些单元测试的 python 模块,我可以将它们传递给hg bisect --command。
单元测试正在测试 django 应用程序的某些功能,但我认为我不能使用 hg bisect --command manage.py test mytestapp,因为必须在 settings.py 中启用 mytestapp,而对 settings.py 的编辑将是当hg bisect 更新工作目录时被破坏。
因此,我想知道以下方法是否是最好的方法:
import functools, os, sys, unittest
sys.path.append(path_to_myproject)
os.environ['DJANGO_SETTINGS_MODULE'] = 'myapp.settings'
def with_test_db(func):
"""Decorator to setup and teardown test db."""
@functools.wraps
def wrapper(*args, **kwargs):
try:
# Set up temporary django db
func(*args, **kwargs)
finally:
# Tear down temporary django db
class TestCase(unittest.TestCase):
@with_test_db
def test(self):
# Do some tests using the temporary django db
self.fail('Mark this revision as bad.')
if '__main__' == __name__:
unittest.main()
如果您能提供任何建议,我将不胜感激:
- 如果有更简单的方法,可能是子类化
django.test.TestCase但不编辑settings.py,或者,如果没有; - 上面写着“设置临时 django db”和“拆除临时 django db”的行应该是什么?
【问题讨论】:
标签: python django unit-testing mercurial