Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 对于django单元测试,为什么一些测试运行程序考虑生产数据库,而其他的则不考虑?_Python_Django_Unit Testing_Nose_Pytest - Fatal编程技术网

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

Python 对于django单元测试,为什么一些测试运行程序考虑生产数据库,而其他的则不考虑?,python,django,unit-testing,nose,pytest,Python,Django,Unit Testing,Nose,Pytest,作为启动django tutorial应用程序的一部分,我注意到某些测试运行人员在运行单元测试时正在进入生产数据库,而其他测试运行人员似乎忽略了它 我通过三个测试将django应用程序缩减为最基本的功能: 模型实例化(控制测试) 对空数据库的请求 对只包含一个元素的数据库的请求 我向生产数据库添加了一个元素,并通过EclipsePydev使用的测试运行程序运行它们 有关代码,请访问 django测试运行程序通过(声称正在创建测试数据库?): pytest运行程序通过(没有任何此类声明,尽管它

作为启动django tutorial应用程序的一部分,我注意到某些测试运行人员在运行单元测试时正在进入生产数据库,而其他测试运行人员似乎忽略了它

我通过三个测试将django应用程序缩减为最基本的功能:

  • 模型实例化(控制测试)
  • 对空数据库的请求
  • 对只包含一个元素的数据库的请求
我向生产数据库添加了一个元素,并通过EclipsePydev使用的测试运行程序运行它们

有关代码,请访问

django测试运行程序通过(声称正在创建测试数据库?):

pytest运行程序通过(没有任何此类声明,尽管它可能是隐藏的):

nose runner失败(它将生产数据库与测试相结合):

unittest2运行程序失败(原因相同):


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

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

声称“测试运行程序负责创建自己的测试数据库”


从中可以清楚地看到,它应该在测试数据库上运行测试。

据我所知,django nose只是将nose testrunner插入到django应用程序中,这样,
python manage.py test
使用nose而不是它使用的任何东西。这是不对的吗?我可以尝试使用django nose,但我认为当我使用常规nose测试django应用程序时,它有着根本的不同。。。正如文档所说,
python manage.py test
确实支持一个测试数据库。我的问题是,DB实例化实际上发生在哪里?是在django.test.TestCase内部,还是存在某种全局testrunner设置,可以在运行测试套件之前管理.py调用?@kenners,我想我对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'...
(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)
(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)