对于Django单元测试,为什么有些测试运行程序考虑生产数据库,而其他程序则不考虑?

3
作为django教程应用的一部分,我注意到某些测试运行程序在运行单元测试时会访问生产数据库,而其他测试运行程序似乎会忽略它。
我将一个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 能够正常工作?
1个回答

1
在运行单元测试时触及生产数据库是完全不恰当的。单元测试通常应该在模拟数据库上工作。集成测试应该使用真实的,但测试数据库。但是生产数据库呢?为什么要冒着风险去处理真实数据? Django文档声称“测试运行程序会创建自己的测试数据库”。
django-nose文档中可以清楚地看到它应该在测试数据库上运行测试。

据我所知,django-nose只是将nose测试运行器插入到django应用程序中,以便python manage.py test使用nose而不是其他测试运行器。这个理解是否正确?我可以尝试使用django-nose,但我认为当我使用常规的nose来测试django应用程序时,它是根本不同的...就像文档所说的那样,python manage.py test确实会启动一个测试数据库。我的问题是,这个数据库实例化实际上发生在哪里?是在django.test.TestCase内部,还是在manage.py在运行测试套件之前调用某种全局测试运行器设置? - kenners
@kenners,我不认为我对Django有足够深入的了解来回答这个问题。 :( - Denis

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接