gpt4 book ai didi

python - 如何使用mockito模拟os.path.join

转载 作者:行者123 更新时间:2023-12-01 01:47:10 25 4
gpt4 key购买 nike

我尝试模拟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)

有什么帮助吗?

最佳答案

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

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

关于python - 如何使用mockito模拟os.path.join,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51180486/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com