作为django教程应用的一部分,我注意到某些测试运行程序在运行单元测试时会访问生产数据库,而其他测试运行程序似乎会忽略它。
我将一个django应用程序精简到最基本的功能,包括三个测试:
- 模型实例化(控制测试) - 空数据库上的请求 - 仅有一个元素的数据库上的请求
我向生产数据库添加了一个元素,并通过Eclipse PyDev使用的测试运行程序对它们进行了测试。
代码可在我的GitHub网站上找到。
Django测试运行程序通过了(声称正在创建测试数据库?):
哪个部分的 nose / unittest2 导致了这些测试失败?为什么 pytest 能够正常工作?
我将一个django应用程序精简到最基本的功能,包括三个测试:
- 模型实例化(控制测试) - 空数据库上的请求 - 仅有一个元素的数据库上的请求
我向生产数据库添加了一个元素,并通过Eclipse PyDev使用的测试运行程序对它们进行了测试。
代码可在我的GitHub网站上找到。
Django测试运行程序通过了(声称正在创建测试数据库?):
(django)kenners@elendil:~/my_first_app$ python manage.py test demo
Creating test database for alias 'default'...
...
----------------------------------------------------------------------
Ran 3 tests in 0.135s
OK
Destroying test database for alias 'default'...
pytest运行器通过了(没有任何这样的声明,尽管可能被隐藏):(django)kenners@elendil:~/my_first_app$ python -m pytest demo/tests.py
=============================================================================== test session starts ================================================================================
platform linux2 -- Python 2.7.3 -- pytest-2.4.2
plugins: django
collected 3 items
demo/tests.py ...
============================================================================= 3 passed in 1.29 seconds =============================================================================
鼻涕跑步者失败了(它将生产数据库与测试结合起来):
(django)kenners@elendil:~/my_first_app$ python -m nose demo/tests.py
FF.
======================================================================
FAIL: test_no_available_things (demo.tests.DemoDatabaseTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/kenners/my_first_app/demo/tests.py", line 23, in test_no_available_things
self.assertEquals(0, pull_count_from_body(response))
AssertionError: 0 != 1
======================================================================
FAIL: test_one_available_things (demo.tests.DemoDatabaseTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/kenners/my_first_app/demo/tests.py", line 30, in test_one_available_things
self.assertEquals(1, pull_count_from_body(response))
AssertionError: 1 != 2
----------------------------------------------------------------------
Ran 3 tests in 0.334s
FAILED (failures=2)
unittest2运行器失败(出现相同的原因):
(django)kenners@elendil:~/my_first_app$ python -m unittest2 demo.tests
FF.
======================================================================
FAIL: test_no_available_things (demo.tests.DemoDatabaseTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "demo/tests.py", line 23, in test_no_available_things
self.assertEquals(0, pull_count_from_body(response))
AssertionError: 0 != 1
======================================================================
FAIL: test_one_available_things (demo.tests.DemoDatabaseTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "demo/tests.py", line 30, in test_one_available_things
self.assertEquals(1, pull_count_from_body(response))
AssertionError: 1 != 2
----------------------------------------------------------------------
Ran 3 tests in 0.348s
FAILED (failures=2)
哪个部分的 nose / unittest2 导致了这些测试失败?为什么 pytest 能够正常工作?
python manage.py test
使用nose而不是其他测试运行器。这个理解是否正确?我可以尝试使用django-nose,但我认为当我使用常规的nose来测试django应用程序时,它是根本不同的...就像文档所说的那样,python manage.py test
确实会启动一个测试数据库。我的问题是,这个数据库实例化实际上发生在哪里?是在django.test.TestCase内部,还是在manage.py在运行测试套件之前调用某种全局测试运行器设置? - kenners