我有python文件
$ tree .
.
├── app
│ ├── __init__.py
│ └── my_module.py
├── setup.py
├── test_my_module.py
└── tox.ini
1 directory, 5 files
my_module.py
$ cat app/my_module.py
from pymongo import MongoClient
client = MongoClient('mongodb://localhost:27017/')
post = client.test_database.posts.find_one()
def ret_value():
return db.posts
tox.ini
$ cat tox.ini
[tox]
envlist = py35
[testenv]
deps=
coverage>=4.1
flake8
pylint
pytest-cov==2.2.1
pytest==3.0.7
pytest-mock
pytest-xdist
pymongo
mock
commands=py.test -n 5 -l
setenv =
XYZ = 123
test_my_module.py
$ cat test_my_module.py
import os
import mock
with mock.patch('app.my_module.MongoClient'):
from app import my_module
def test_my_module_method():
assert os.getenv('XYZ') == '123'
在my_module.py
中,它会在导入时创建mongodb连接。我想在我的测试用例中导入my_module
之前先嘲笑它。
它给出了错误
$ tox -v
using tox.ini: /private/tmp/testing/tox.ini
using tox-2.9.1 from /Library/Python/2.7/site-packages/tox/__init__.pyc
GLOB sdist-make: /private/tmp/testing/setup.py
/private/tmp/testing$ /usr/bin/python /private/tmp/testing/setup.py sdist --formats=zip --dist-dir /private/tmp/testing/.tox/dist >/private/tmp/testing/.tox/log/tox-0.log
py35 reusing: /private/tmp/testing/.tox/py35
py35 inst-nodeps: /private/tmp/testing/.tox/dist/Tox Testing-0.0.0.zip
/private/tmp/testing$ /private/tmp/testing/.tox/py35/bin/pip install -U --no-deps /private/tmp/testing/.tox/dist/Tox Testing-0.0.0.zip >/private/tmp/testing/.tox/py35/log/py35-4.log
/private/tmp/testing$ /private/tmp/testing/.tox/py35/bin/pip freeze >/private/tmp/testing/.tox/py35/log/py35-5.log
py35 installed: apipkg==1.4,astroid==1.5.3,coverage==4.4.2,execnet==1.5.0,flake8==3.5.0,isort==4.2.15,lazy-object-proxy==1.3.1,mccabe==0.6.1,mock==2.0.0,pbr==3.1.1,py==1.5.2,pycodestyle==2.3.1,pyflakes==1.6.0,pylint==1.7.4,pymongo==3.5.1,pytest==3.0.7,pytest-cov==2.2.1,pytest-forked==0.2,pytest-mock==1.6.3,pytest-xdist==1.20.1,six==1.11.0,Tox-Testing==0.0.0,wrapt==1.10.11
py35 runtests: PYTHONHASHSEED='2635490598'
py35 runtests: commands[0] | py.test -n 5 -l
/private/tmp/testing$ /private/tmp/testing/.tox/py35/bin/py.test -n 5 -l
=============================================================================== test session starts ===============================================================================
platform darwin -- Python 3.5.2, pytest-3.0.7, py-1.5.2, pluggy-0.4.0
rootdir: /private/tmp/testing, inifile:
plugins: xdist-1.20.1, mock-1.6.3, forked-0.2, cov-2.2.1
gw0 [0] / gw1 [0] / gw2 [0] / gw3 [0] / gw4 [0]
scheduling tests via LoadScheduling
===================================================================================== ERRORS ======================================================================================
_______________________________________________________________________ ERROR collecting test_my_module.py ________________________________________________________________________
test_my_module.py:5: in <module>
with mock.patch('app.my_module.MongoClient'):
.tox/py35/lib/python3.5/site-packages/mock/mock.py:1353: in __enter__
self.target = self.getter()
.tox/py35/lib/python3.5/site-packages/mock/mock.py:1523: in <lambda>
getter = lambda: _importer(target)
.tox/py35/lib/python3.5/site-packages/mock/mock.py:1210: in _importer
thing = _dot_lookup(thing, comp, import_path)
.tox/py35/lib/python3.5/site-packages/mock/mock.py:1199: in _dot_lookup
__import__(import_path)
app/my_module.py:5: in <module>
post = client.test_database.posts.find_one()
.tox/py35/lib/python3.5/site-packages/pymongo/collection.py:1109: in find_one
for result in cursor.limit(-1):
.tox/py35/lib/python3.5/site-packages/pymongo/cursor.py:1132: in next
if len(self.__data) or self._refresh():
.tox/py35/lib/python3.5/site-packages/pymongo/cursor.py:1055: in _refresh
self.__collation))
.tox/py35/lib/python3.5/site-packages/pymongo/cursor.py:892: in __send_message
**kwargs)
.tox/py35/lib/python3.5/site-packages/pymongo/mongo_client.py:933: in _send_message_with_response
server = topology.select_server(selector)
.tox/py35/lib/python3.5/site-packages/pymongo/topology.py:214: in select_server
address))
.tox/py35/lib/python3.5/site-packages/pymongo/topology.py:189: in select_servers
self._error_message(selector))
E pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 61] Connection refused
_______________________________________________________________________ ERROR collecting test_my_module.py ________________________________________________________________________
test_my_module.py:5: in <module>
with mock.patch('app.my_module.MongoClient'):
.tox/py35/lib/python3.5/site-packages/mock/mock.py:1353: in __enter__
self.target = self.getter()
.tox/py35/lib/python3.5/site-packages/mock/mock.py:1523: in <lambda>
getter = lambda: _importer(target)
.tox/py35/lib/python3.5/site-packages/mock/mock.py:1210: in _importer
thing = _dot_lookup(thing, comp, import_path)
.tox/py35/lib/python3.5/site-packages/mock/mock.py:1199: in _dot_lookup
__import__(import_path)
app/my_module.py:5: in <module>
post = client.test_database.posts.find_one()
.tox/py35/lib/python3.5/site-packages/pymongo/collection.py:1109: in find_one
for result in cursor.limit(-1):
.tox/py35/lib/python3.5/site-packages/pymongo/cursor.py:1132: in next
if len(self.__data) or self._refresh():
.tox/py35/lib/python3.5/site-packages/pymongo/cursor.py:1055: in _refresh
self.__collation))
.tox/py35/lib/python3.5/site-packages/pymongo/cursor.py:892: in __send_message
**kwargs)
.tox/py35/lib/python3.5/site-packages/pymongo/mongo_client.py:933: in _send_message_with_response
server = topology.select_server(selector)
.tox/py35/lib/python3.5/site-packages/pymongo/topology.py:214: in select_server
address))
.tox/py35/lib/python3.5/site-packages/pymongo/topology.py:189: in select_servers
self._error_message(selector))
E pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 61] Connection refused
_______________________________________________________________________ ERROR collecting test_my_module.py ________________________________________________________________________
test_my_module.py:5: in <module>
with mock.patch('app.my_module.MongoClient'):
.tox/py35/lib/python3.5/site-packages/mock/mock.py:1353: in __enter__
self.target = self.getter()
.tox/py35/lib/python3.5/site-packages/mock/mock.py:1523: in <lambda>
getter = lambda: _importer(target)
.tox/py35/lib/python3.5/site-packages/mock/mock.py:1210: in _importer
thing = _dot_lookup(thing, comp, import_path)
.tox/py35/lib/python3.5/site-packages/mock/mock.py:1199: in _dot_lookup
__import__(import_path)
app/my_module.py:5: in <module>
post = client.test_database.posts.find_one()
.tox/py35/lib/python3.5/site-packages/pymongo/collection.py:1109: in find_one
for result in cursor.limit(-1):
.tox/py35/lib/python3.5/site-packages/pymongo/cursor.py:1132: in next
if len(self.__data) or self._refresh():
.tox/py35/lib/python3.5/site-packages/pymongo/cursor.py:1055: in _refresh
self.__collation))
.tox/py35/lib/python3.5/site-packages/pymongo/cursor.py:892: in __send_message
**kwargs)
.tox/py35/lib/python3.5/site-packages/pymongo/mongo_client.py:933: in _send_message_with_response
server = topology.select_server(selector)
.tox/py35/lib/python3.5/site-packages/pymongo/topology.py:214: in select_server
address))
.tox/py35/lib/python3.5/site-packages/pymongo/topology.py:189: in select_servers
self._error_message(selector))
E pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 61] Connection refused
_______________________________________________________________________ ERROR collecting test_my_module.py ________________________________________________________________________
test_my_module.py:5: in <module>
with mock.patch('app.my_module.MongoClient'):
.tox/py35/lib/python3.5/site-packages/mock/mock.py:1353: in __enter__
self.target = self.getter()
.tox/py35/lib/python3.5/site-packages/mock/mock.py:1523: in <lambda>
getter = lambda: _importer(target)
.tox/py35/lib/python3.5/site-packages/mock/mock.py:1210: in _importer
thing = _dot_lookup(thing, comp, import_path)
.tox/py35/lib/python3.5/site-packages/mock/mock.py:1199: in _dot_lookup
__import__(import_path)
app/my_module.py:5: in <module>
post = client.test_database.posts.find_one()
.tox/py35/lib/python3.5/site-packages/pymongo/collection.py:1109: in find_one
for result in cursor.limit(-1):
.tox/py35/lib/python3.5/site-packages/pymongo/cursor.py:1132: in next
if len(self.__data) or self._refresh():
.tox/py35/lib/python3.5/site-packages/pymongo/cursor.py:1055: in _refresh
self.__collation))
.tox/py35/lib/python3.5/site-packages/pymongo/cursor.py:892: in __send_message
**kwargs)
.tox/py35/lib/python3.5/site-packages/pymongo/mongo_client.py:933: in _send_message_with_response
server = topology.select_server(selector)
.tox/py35/lib/python3.5/site-packages/pymongo/topology.py:214: in select_server
address))
.tox/py35/lib/python3.5/site-packages/pymongo/topology.py:189: in select_servers
self._error_message(selector))
E pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 61] Connection refused
_______________________________________________________________________ ERROR collecting test_my_module.py ________________________________________________________________________
test_my_module.py:5: in <module>
with mock.patch('app.my_module.MongoClient'):
.tox/py35/lib/python3.5/site-packages/mock/mock.py:1353: in __enter__
self.target = self.getter()
.tox/py35/lib/python3.5/site-packages/mock/mock.py:1523: in <lambda>
getter = lambda: _importer(target)
.tox/py35/lib/python3.5/site-packages/mock/mock.py:1210: in _importer
thing = _dot_lookup(thing, comp, import_path)
.tox/py35/lib/python3.5/site-packages/mock/mock.py:1199: in _dot_lookup
__import__(import_path)
app/my_module.py:5: in <module>
post = client.test_database.posts.find_one()
.tox/py35/lib/python3.5/site-packages/pymongo/collection.py:1109: in find_one
for result in cursor.limit(-1):
.tox/py35/lib/python3.5/site-packages/pymongo/cursor.py:1132: in next
if len(self.__data) or self._refresh():
.tox/py35/lib/python3.5/site-packages/pymongo/cursor.py:1055: in _refresh
self.__collation))
.tox/py35/lib/python3.5/site-packages/pymongo/cursor.py:892: in __send_message
**kwargs)
.tox/py35/lib/python3.5/site-packages/pymongo/mongo_client.py:933: in _send_message_with_response
server = topology.select_server(selector)
.tox/py35/lib/python3.5/site-packages/pymongo/topology.py:214: in select_server
address))
.tox/py35/lib/python3.5/site-packages/pymongo/topology.py:189: in select_servers
self._error_message(selector))
E pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 61] Connection refused
============================================================================= pytest-warning summary ==============================================================================
WC1 None pytest_funcarg__cov: declaring fixtures using "pytest_funcarg__" prefix is deprecated and scheduled to be removed in pytest 4.0. Please remove the prefix and use the @pytest.fixture decorator instead.
WC1 None pytest_funcarg__cov: declaring fixtures using "pytest_funcarg__" prefix is deprecated and scheduled to be removed in pytest 4.0. Please remove the prefix and use the @pytest.fixture decorator instead.
WC1 None pytest_funcarg__cov: declaring fixtures using "pytest_funcarg__" prefix is deprecated and scheduled to be removed in pytest 4.0. Please remove the prefix and use the @pytest.fixture decorator instead.
WC1 None pytest_funcarg__cov: declaring fixtures using "pytest_funcarg__" prefix is deprecated and scheduled to be removed in pytest 4.0. Please remove the prefix and use the @pytest.fixture decorator instead.
WC1 None pytest_funcarg__cov: declaring fixtures using "pytest_funcarg__" prefix is deprecated and scheduled to be removed in pytest 4.0. Please remove the prefix and use the @pytest.fixture decorator instead.
WC1 None pytest_funcarg__cov: declaring fixtures using "pytest_funcarg__" prefix is deprecated and scheduled to be removed in pytest 4.0. Please remove the prefix and use the @pytest.fixture decorator instead.
=================================================================== 6 pytest-warnings, 5 error in 34.93 seconds ===================================================================
ERROR: InvocationError: '/private/tmp/testing/.tox/py35/bin/py.test -n 5 -l'
_____________________________________________________________________________________ summary _____________________________________________________________________________________
ERROR: py35: commands failed
在导入之前有没有办法模拟它?