最近需要测试一个服务器端的django项目,查看了一下django的文档,发现django为了更加方便的对web应用进行测试,提供了一些便捷的测试方法。并且,专门有一篇文档讲诉如何测试django应用。
快速横扫了一下文档后,初步印象是django默认支持Doctests和Unit tests两个测试框架的,同时提供了一些辅助的测试类,比如Test Client、TestCase、Email Service。通过Client,可以方便的发起一个get或者post请求,并且取得返回结果。而TestCase也是对 unittest.TestCase进行了进一步的封装,省去了很多重复要写的代码,比如定义一个self.client。Email Service提供了方便的邮件发送的方法。
使用Test Client有什么好处呢?
1. 它不需要Web服务器运行起来。
2. 它执行更加快速。
3. 它能非常方便的取到被测应用内部的东西。
最近需要测试一个服务器端的django项目,查看了一下django的文档,发现django为了更加方便的对web应用进行测试,提供了一些便捷的测试方法。并且,专门有一篇文档讲诉如何测试django应用。
http://docs.djangoproject.com/en/dev/topics/testing/
快速横扫了一下文档后,初步印象是django默认支持Doctests和Unit tests两个测试框架的,同时提供了一些辅助的测试类,比如Test Client、TestCase、Email Service。通过Client,可以方便的发起一个get或者post请求,并且取得返回结果。而TestCase也是对unittest.TestCase进行了进一步的封装,省去了很多重复要写的代码,比如定义一个self.client。Email Service提供了方便的邮件发送的方法。
使用Test Client有什么好处呢?
1. 它不需要Web服务器运行起来。
2. 它执行更加快速。
3. 它能非常方便的取到被测应用内部的东西。
好的,既然那么好的东西,赶紧试用一下。首先,我新建了一个测试工程testdjango,设置了一个简单的页面,通过访问"/test",会返回一个字符串"abc"。
#
urls.py
urlpatterns
=
patterns(
''
,
(r
'
^test$
'
,
'
views.test
'
),
#
views.py
from
django.http
import
HttpResponse
def
test(request):
return
HttpResponse(
'
abc
'
)
接下来,编写一个测试案例:
#
tests.py
from
django.test
import
TestCase
class
ViewTest(TestCase):
def
test(self):
response
=
self.client.get(
'
/test
'
)
self.failUnlessEqual(
'
abc
'
, response.
content
)
如何才能找到你的测试案例呢?文档中说,会加载settings里INSTALLED_APPS的模块,并且在该模块目录的models.py和tests.py中查找测试案例。因此,我在settings的INSTALLED_APPS里添加了'testdjango',testdjango目录中正好有tests.py。
接下来要运行测试案例,由于安装文档的说明, 运行所有INSTALLED_APPS里的测试案例只要运行:
python manage.py test
如果想指定运行,可以:
python manage.py test testdjango
OK,运行一下,非常不幸,出现了如下异常:
"You haven't set the DATABASE_ENGINE setting yet."
数据库??测试案例为什么还需要设置数据库?回过头仔细看下文档,发现确实有test database一节,大概含义是会生成一个临时的测试数据库,名字是test_前缀,如果想改还可以设定TEST_DATABASE_NAME属性。如果是sqlite,则默认是:memory:方式,即记在内存的方式。为了简单起见,我设置一个sqlite的。。
DATABASE_ENGINE
=
'
sqlite3
'
#
'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
DATABASE_NAME
=
'
:memory:
'
#
Or path to database file if using sqlite3.
DATABASE_USER
=
''
#
Not used with sqlite3.
DATABASE_PASSWORD
=
''
#
Not used with sqlite3.
DATABASE_HOST
=
''
#
Set to empty string for localhost. Not used with sqlite3.
DATABASE_PORT
=
''
#
Set to empty string for default. Not used with sqlite3.
再次执行,看到输出窗口开始输出了:
Creating test database
突然又是一个异常:
" UnboundLocalError: local variable 'int_alias' referenced before assignment "
这个问题让我google了好一阵,在djangoproject里还专门有一个ticket报了这个问题,但最后确认为不是django的bug不了了之。
http://code.djangoproject.com/ticket/10930
奇怪的是,只有在WingIDE中启动才会出现,在命令行里执行就不会有这个错误。后来看了一下WingIDE的设置,原来可以Debug里的Exception可以设置Never Report和Always Report,在Never Report里添加UnboundLocalError,同时在Always Report里去掉,这个异常就没有出现了。
再次运行,开始看到一大段的输出,时不时还有一些异常:
django.template.TemplateDoesNotExist: registration/password_change_form.html
输出大致如下:
Creating test database
Creating table auth_permission
Creating table auth_group
Creating table auth_user
Creating table auth_message
Creating table django_content_type
Creating table django_session
Creating table django_site
Installing index
for
auth.Permission model
Installing index
for
auth.Message model
EE..E
EEEEEEE
.
======================================================================
ERROR: test_password_change_fails_with_invalid_old_password (django.contrib.auth.tests.views.ChangePasswordTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File
"
D:\Python25\Lib\site-packages\django\contrib\auth\tests\views.py
"
, line
156
,
in
test_password_change_fails_with_invalid_old_password
'
new_password2
'
:
'
password1
'
,
。。。。。
File
"
D:\Python25\Lib\site-packages\django\template\loader.py
"
, line
74
,
in
find_template_source
raise
TemplateDoesNotExist, name
TemplateDoesNotExist: registration
/
password_change_form.html
原来运行的是manage.py test,默认把INSTALLED_APP里的其他模块的测试案例也运行了起来。这些异常也是允许那些测试案例抛出的,而我定义的测试案例貌似没有运行。来回看了几遍文档,百思不得其解。最后,打算直接看django的源码,为何没有加载我的测试案例,而像django.contrib.auth里的测试案例却都能执行起来。于是我一步一步的跟进去调试。最后发现:
首先会查找当前目录的models.py,如果这个模块Import失败的话不会再继续从tests模块里找。
也就是说,我必须定义一个models.py文件,即使里面没有内容。于是,我添加了一个models.py文件问题就解决了!
添加一个没什么用的models.py和设置数据库参数,看上去似乎对我来说没什么意义,但在某些情况下,这样的方式能够比较简单的解决之前的问题然后展开测试。 再细看文档时,其中还有提到的是,我们可以自己设置一个test runner,安装我们定义的方式去执行。听上去不错,这样我就可以不用去添加什么models.py和修改数据库设置了。django默认是会加载django.test.simple.run_tests函数,我们需要做的就是一个自己写一个run_tests函数,通过设置TEST_RUNNER属性,加载我们的test runner。
通过查看默认的test runner代码,大致了解了基本原理后,我对默认的test runner进行了大量的简化,去除了models.py的加载,去除了数据库的依赖。下面是我的test runner,代码很短:
import
unittest
from
django.conf
import
settings
from
django.test.utils
import
setup_test_environment, teardown_test_environment
from
django.test.testcases
import
OutputChecker, DocTestRunner, TestCase
TEST_MODULE
=
'
tests
'
def
build_suite(label):
suite
=
unittest.TestSuite()
try
:
app_path
=
label
+
'
.
'
+
TEST_MODULE
test_module
=
__import__
(app_path, {}, {}, TEST_MODULE)
except
ImportError, e:
test_module
=
None
if
test_module:
if
hasattr(test_module,
'
suite
'
):
suite.addTest(test_module.suite())
else
:
suite.addTest(unittest.defaultTestLoader.loadTestsFromModule(test_module))
return
suite
def
run_tests(test_labels, verbosity
=
1
, interactive
=
True, extra_tests
=
[]):
setup_test_environment()
settings.DEBUG
=
False
suite
=
unittest.TestSuite()
if
test_labels:
for
label
in
test_labels:
suite.addTest(build_suite(label))
for
test
in
extra_tests:
suite.addTest(test)
result
=
unittest.TextTestRunner(verbosity
=
verbosity).run(suite)
teardown_test_environment()
return
len(result.failures)
+
len(result.errors)
不要忘记了设在TEST_RUNNER:
TEST_RUNNER
=
'
testdjango.testrunner.run_tests
'
运行一下看看,发现我的测试案例失败了:
======================================================================
ERROR: test (testdjango.tests.testviews.ViewTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File
"
D:\Python25\Lib\site-packages\django\test\testcases.py
"
, line
242
,
in
__call__
self._pre_setup()
File
"
D:\Python25\Lib\site-packages\django\test\testcases.py
"
, line
217
,
in
_pre_setup
self._fixture_setup()
File
"
D:\Python25\Lib\site-packages\django\test\testcases.py
"
, line
439
,
in
_fixture_setup
if
not
settings.DATABASE_SUPPORTS_TRANSACTIONS:
File
"
D:\Python25\Lib\site-packages\django\utils\functional.py
"
, line
273
,
in
__getattr__
return
getattr(self._wrapped, name)
AttributeError:
'
Settings
'
object has no attribute
'
DATABASE_SUPPORTS_TRANSACTIONS
'
----------------------------------------------------------------------
Ran 0 tests
in
0.000s
FAILED (errors
=
1
)
为什么还需要依赖数据库??哦,原来我在测试案例中用到了本文
开头提到的django封装后的TestCase,它的内部是有数据库相关的操作的。看来,要使用我这个test runner,就不能再使用django的TestCase了,而是使用unittest.TestCase
了。因此,修改测试案例如下:
import
unittest
from
django.test
import
Client
class
ViewTest(unittest.TestCase):
def
test(self):
self.client
=
Client()
response
=
self.client.get(
'
/test
'
)
self.failUnlessEqual(response.status_code,
200
)
self.failUnlessEqual(
'
abc
'
, response.content)
大功告成!输出结果:
----------------------------------------------------------------------
Ran
1
test
in
0.937s
OK
因为是历险记,所有把很多过程的东西拿出来了。最后,把我最后能用的代码传一份上来,希望能够有些帮助,如果过程中有什么不对的地方,也请指出,谢谢!!
/Files/coderzh/testdjango.rar
公众号:hacker-thinking (一个程序员的思考)
独立博客:
http://blog.coderzh.com
博客园博客将不再更新,请关注我的「微信公众号」或「独立博客」。
作为一个程序员,思考程序的每一行代码,思考生活的每一个细节,思考人生的每一种可能。
文章版权归本人所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。