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)

鼻子 / unittest2 的哪一部分导致这些测试失败?为什么 pytest 工作?

4

1 回答 1

1

在运行单元测试时接触生产数据库是绝对不合适的。单元测试通常应该在模拟数据库上工作。集成测试应该在真实的但测试数据库上工作。但是生产数据库呢?你为什么要拿你的真实数据冒险?

Django 文档声称“测试运行器负责创建自己的测试数据库”。

django-nose 文档中可以清楚地看到它应该在测试数据库上运行测试。

于 2013-10-07T06:36:50.587 回答