如何设置和拆卸临时 django 数据库以进行单元测试?
我想要一个包含一些单元测试的 python 模块,我可以将其传递给 hg bisect --command。
单元测试正在测试 django 应用程序的某些功能,但我认为我不能使用 hg bisect --command manage.py test mytestapp ,因为 mytestapp 必须在 settings.py 中启用,并且当 hg bisect
更新工作目录时,对 settings.py 的编辑将被破坏。
因此,我想知道以下内容是否是最好的方法:
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 数据库”和“拆除临时 django 数据库”应该是什么?
I would like to have a python module containing some unit tests that I can pass to hg bisect --command
.
The unit tests are testing some functionality of a django app, but I don't think I can use hg bisect --command manage.py test mytestapp
because mytestapp
would have to be enabled in settings.py, and the edits to settings.py would be clobbered when hg bisect
updates the working directory.
Therefore, I would like to know if something like the following is the best way to go:
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()
I should be most grateful if you could advise either:
- If there is a simpler way, perhaps subclassing
django.test.TestCase
but not editing settings.py or, if not; - What the lines above that say "Set up temporary django db" and "Tear down temporary django db" should be?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
破解了它。我现在拥有一个完全独立于任何可以使用测试数据库运行单元测试的 django 应用程序的 python 文件:
Cracked it. I now have one python file completely independent of any django app that can run unit tests with a test database:
您必须使用内部 Django TestCase 才能执行此操作。
它与单元测试完全兼容,因此您的代码不需要进行太多更改。
您可以了解有关django.test、装置的更多信息, flush 和 loaddata 命令。
如果您确实想使用装饰器来完成这项工作,您可以使用
call_command
在您的 python 程序中使用任何 django 命令。例如:You must use the internal Django TestCase to do so.
It's fully compatible with unittest so your code don't need to change much.
You can learn more about django.test, fixtures, flush and loaddata commands.
If you do want to use a decorator to do the job, you can use the
call_command
to use in your python program any django command. e.g :