如何在Django中测试单个应用程序(不是项目)? [英] How to test single application (not project) in Django?
问题描述
我想测试我的小型应用程序,我将其保存在单独的程序包中.现在,我创建了一个"test_project"目录,在该目录中创建了一个测试项目,并且我正在使用该项目的manage.py来运行测试.但我一直在想-是否有更好的方法?可以使用某些默认配置(例如sqlite数据库)启动单个应用程序的测试吗?
I would like to test my small application, that I keep in a separate package. Right now I created a "test_project" directory, created a test project there and I am using the project's manage.py to run tests. But I keep wondering - is there a better method? Is it possible to launch a single app's tests, perhaps with some default configuration (like, sqlite database)?
推荐答案
可以单独运行单个应用程序的测试,而无需为此目的创建Django测试项目.一种方法是在应用程序的根目录中创建一个 runtests.py
,以设置Django设置并运行 ./manage.py以编程方式测试your_app
. runtests.py
的一个示例是Django自己的:runtests.py (文档).
It is possible to run a single app's tests standalone, without creating a Django test project for that purpose. One way of doing so is by creating a runtests.py
in your app's root dir which setups Django settings and runs ./manage.py test your_app
programmatically. One example of runtests.py
is Django's own: runtests.py (documentation).
Django自己的 runtests.py
是一个很好的参考,但是我发现在大多数情况下它都是令人费解的.以下是几个替代示例:
Django's own runtests.py
is a good reference but I find it convoluted for most cases. Below are a couple of alternative examples:
这篇关于如何在Django中测试单个应用程序(不是项目)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!