运行一下看看,发现我的测试案例失败了:
======================================================================
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
因为是历险记,所有把很多过程的东西拿出来了。最后,把我最后能用的代码传一份上来,希望能够有些帮助,如果过程中有什么不对的地方,也请指出,谢谢!!