如何嘲笑os.path.join操作系统使用mocki

2024-04-26 03:04:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着嘲笑mock.patch('os.path.join',lambda str:'test.txt')。在

我观察到以下错误:

INTERNALERROR> Traceback (most recent call last):
INTERNALERROR>   File "/usr/lib/python2.7/site-packages/_pytest/main.py", line 178, in wrap_session
INTERNALERROR>     session.exitstatus = doit(config, session) or 0
INTERNALERROR>   File "/usr/lib/python2.7/site-packages/_pytest/main.py", line 215, in _main
INTERNALERROR>     config.hook.pytest_runtestloop(session=session)
INTERNALERROR>   File "/usr/lib/python2.7/site-packages/pluggy/__init__.py", line 617, in __call__
INTERNALERROR>     return self._hookexec(self, self._nonwrappers + self._wrappers, kwargs)
INTERNALERROR>   File "/usr/lib/python2.7/site-packages/pluggy/__init__.py", line 222, in _hookexec
INTERNALERROR>     return self._inner_hookexec(hook, methods, kwargs)
INTERNALERROR>   File "/usr/lib/python2.7/site-packages/pluggy/__init__.py", line 216, in <lambda>
INTERNALERROR>     firstresult=hook.spec_opts.get('firstresult'),
INTERNALERROR>   File "/usr/lib/python2.7/site-packages/pluggy/callers.py", line 201, in _multicall
INTERNALERROR>     return outcome.get_result()
INTERNALERROR>   File "/usr/lib/python2.7/site-packages/pluggy/callers.py", line 77, in get_result
INTERNALERROR>     _reraise(*ex)  # noqa
INTERNALERROR>   File "/usr/lib/python2.7/site-packages/pluggy/callers.py", line 180, in _multicall
INTERNALERROR>     res = hook_impl.function(*args)
INTERNALERROR>   File "/usr/lib/python2.7/site-packages/_pytest/main.py", line 236, in pytest_runtestloop
INTERNALERROR>     item.config.hook.pytest_runtest_protocol(item=item, nextitem=nextitem)
INTERNALERROR>   File "/usr/lib/python2.7/site-packages/pluggy/__init__.py", line 617, in __call__
INTERNALERROR>     return self._hookexec(self, self._nonwrappers + self._wrappers, kwargs)
INTERNALERROR>   File "/usr/lib/python2.7/site-packages/pluggy/__init__.py", line 222, in _hookexec
INTERNALERROR>     return self._inner_hookexec(hook, methods, kwargs)
INTERNALERROR>   File "/usr/lib/python2.7/site-packages/pluggy/__init__.py", line 216, in <lambda>
INTERNALERROR>     firstresult=hook.spec_opts.get('firstresult'),
INTERNALERROR>   File "/usr/lib/python2.7/site-packages/pluggy/callers.py", line 201, in _multicall
INTERNALERROR>     return outcome.get_result()
INTERNALERROR>   File "/usr/lib/python2.7/site-packages/pluggy/callers.py", line 77, in get_result
INTERNALERROR>     _reraise(*ex)  # noqa
INTERNALERROR>   File "/usr/lib/python2.7/site-packages/pluggy/callers.py", line 180, in _multicall
INTERNALERROR>     res = hook_impl.function(*args)
INTERNALERROR>   File "/usr/lib/python2.7/site-packages/_pytest/runner.py", line 64, in pytest_runtest_protocol
INTERNALERROR>     runtestprotocol(item, nextitem=nextitem)
INTERNALERROR>   File "/usr/lib/python2.7/site-packages/_pytest/runner.py", line 79, in runtestprotocol
INTERNALERROR>     reports.append(call_and_report(item, "call", log))
INTERNALERROR>   File "/usr/lib/python2.7/site-packages/_pytest/runner.py", line 161, in call_and_report
INTERNALERROR>     report = hook.pytest_runtest_makereport(item=item, call=call)
INTERNALERROR>   File "/usr/lib/python2.7/site-packages/pluggy/__init__.py", line 617, in __call__
INTERNALERROR>     return self._hookexec(self, self._nonwrappers + self._wrappers, kwargs)
INTERNALERROR>   File "/usr/lib/python2.7/site-packages/pluggy/__init__.py", line 222, in _hookexec
INTERNALERROR>     return self._inner_hookexec(hook, methods, kwargs)
INTERNALERROR>   File "/usr/lib/python2.7/site-packages/pluggy/__init__.py", line 216, in <lambda>
INTERNALERROR>     firstresult=hook.spec_opts.get('firstresult'),
INTERNALERROR>   File "/usr/lib/python2.7/site-packages/pluggy/callers.py", line 196, in _multicall
INTERNALERROR>     gen.send(outcome)
INTERNALERROR>   File "/usr/lib/python2.7/site-packages/_pytest/skipping.py", line 123, in pytest_runtest_makereport
INTERNALERROR>     rep = outcome.get_result()
INTERNALERROR>   File "/usr/lib/python2.7/site-packages/pluggy/callers.py", line 77, in get_result
INTERNALERROR>     _reraise(*ex)  # noqa
INTERNALERROR>   File "/usr/lib/python2.7/site-packages/pluggy/callers.py", line 180, in _multicall
INTERNALERROR>     res = hook_impl.function(*args)
INTERNALERROR>   File "/usr/lib/python2.7/site-packages/_pytest/runner.py", line 328, in pytest_runtest_makereport
INTERNALERROR>     longrepr = item.repr_failure(excinfo)
INTERNALERROR>   File "/usr/lib/python2.7/site-packages/_pytest/python.py", line 672, in repr_failure
INTERNALERROR>     return self._repr_failure_py(excinfo, style=style)
INTERNALERROR>   File "/usr/lib/python2.7/site-packages/_pytest/python.py", line 665, in _repr_failure_py
INTERNALERROR>     return super(FunctionMixin, self)._repr_failure_py(excinfo, style=style)
INTERNALERROR>   File "/usr/lib/python2.7/site-packages/_pytest/nodes.py", line 295, in _repr_failure_py
INTERNALERROR>     tbfilter=tbfilter,
INTERNALERROR>   File "/usr/lib/python2.7/site-packages/_pytest/_code/code.py", line 472, in getrepr
INTERNALERROR>     return fmt.repr_excinfo(self)
INTERNALERROR>   File "/usr/lib/python2.7/site-packages/_pytest/_code/code.py", line 702, in repr_excinfo
INTERNALERROR>     reprtraceback = self.repr_traceback(excinfo)
INTERNALERROR>   File "/usr/lib/python2.7/site-packages/_pytest/_code/code.py", line 659, in repr_traceback
INTERNALERROR>     reprentry = self.repr_traceback_entry(entry, einfo)
INTERNALERROR>   File "/usr/lib/python2.7/site-packages/_pytest/_code/code.py", line 618, in repr_traceback_entry
INTERNALERROR>     reprargs = self.repr_args(entry) if not short else None
INTERNALERROR>   File "/usr/lib/python2.7/site-packages/_pytest/_code/code.py", line 539, in repr_args
INTERNALERROR>     args.append((argname, self._saferepr(argvalue)))
INTERNALERROR>   File "/usr/lib/python2.7/site-packages/_pytest/_code/code.py", line 533, in _saferepr
INTERNALERROR>     return py.io.saferepr(obj)
INTERNALERROR>   File "/usr/lib/python2.7/site-packages/py/_vendored_packages/apipkg.py", line 148, in __makeattr
INTERNALERROR>     result = importobj(modpath, attrname)
INTERNALERROR>   File "/usr/lib/python2.7/site-packages/py/_vendored_packages/apipkg.py", line 69, in importobj
INTERNALERROR>     module = __import__(modpath, None, None, ['__doc__'])
INTERNALERROR>   File "/usr/lib/python2.7/site-packages/_pytest/assertion/rewrite.py", line 97, in find_module
INTERNALERROR>     fn = os.path.join(pth, name.rpartition(".")[2] + ".py")
INTERNALERROR>   File "/usr/lib/python2.7/site-packages/mockito/mocking.py", line 88, in new_mocked_method
INTERNALERROR>     self, method_name, *args, **kwargs)
INTERNALERROR>   File "/usr/lib/python2.7/site-packages/mockito/mocking.py", line 44, in remembered_invocation_builder
INTERNALERROR>     return invoc(*args, **kwargs)
INTERNALERROR>   File "/usr/lib/python2.7/site-packages/mockito/invocation.py", line 164, in __call__
INTERNALERROR>     *params, **named_params)
INTERNALERROR>   File "/usr/lib/python2.7/site-packages/mockito/invocation.py", line 293, in answer_first
INTERNALERROR>     return self.answers.answer(*args, **kwargs)
INTERNALERROR>   File "/usr/lib/python2.7/site-packages/mockito/invocation.py", line 402, in answer
INTERNALERROR>     return a(*args, **kwargs)
INTERNALERROR> TypeError: <lambda>() takes exactly 1 argument (2 given)

有什么帮助吗?在


Tags: inpyselfreturnpytestlibpackagesusr
2条回答

好吧,这里典型的mockito用法是when(os.path).join(...).thenReturn('one_thing')。我在这里使用的文字...(省略号)在Python3中是有效的。在

对于python2.7,您可以

from mockito import args when(os.path).join(*args).thenReturn('one_thing')

os.path.join接受一个或多个参数,但您的替换(lambda str:'test.txt')只接受一个参数。这是错误的原因吗?在

如果是这样,您可以尝试lambda s, *args: "test.txt"。在

相关问题 更多 >

    热门问题