- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我得到了我的程序:
# module/core_functions.py
def new_input(question):
print(question)
value = input(">").strip()
return value
def main_function():
#do things
new_input("question1")
#do other things
new_input("question2")
...
我写了一个单元测试:
import unittest
from unittest.mock import patch, Mock
from module.core_functions import main_function
class MytestClass(unittest.TestCase):
@patch('module.core_functions.new_input')
def test_multiple_answer(self, mock_input):
mock_input.return_value = Mock()
mock_input.side_effect = ['Answer1', 'Answer2']
result = main_function()
self.assertIn('ExpectedResult', result)
这工作得很好(我使用nose2
来运行我的所有测试)。
随着我的代码变得越来越大,我想让其他人参与我的项目,并且我需要模块化我的功能以使修改更容易和更干净。
因此,我将 new_input
函数放入新文件 module/io.py
中,并获得了一个新的子函数:
# module/subfunctions.py
from module.io import new_input
def subfunction():
# do things
new_input("question")
# do things
return things
我的核心程序演变为:
# module/core_functions.py
from module.io import new_input
from module.subfunctions import subfunction
def main_function():
#do things
new_input("question1")
#do other things
subfuntion()
...
所以问题是要模拟的函数位于多个地方:在主函数和一些子函数中。我找不到一种方法让我的单元测试工作(我无法预测主函数或一个子函数是否需要 AnswerX)。
您知道如何修复我的测试以使其正常工作吗?谢谢(我希望我已经说得相当清楚了)。
我尝试过类似的方法:
@patch('module.core_functions.new_input')
@patch('module.subfunctions.new_input')
def test_multiple_answer(self, mock_input):
mock_input.return_value = Mock()
mock_input.side_effect = ['Answer1', 'Answer2']
result = main_function()
self.assertIn('ExpectedResult', result)
但我收到错误:TypeError: test_multiple_answer() 采用 2 个位置参数,但给出了 3 个
。
最佳答案
我刚刚问了自己同样的问题,虽然我很确定答案,但我写了一个愚蠢的小但可以理解的概念证明。
就像@whats-done-is 指出的那样,有两种方法可以解决这个问题;使用 mock.patch()
的 new 参数指定要内联的模拟对象,或者为每个 @patch 注释添加一个参数。
def expensive_function():
return 12345
from utils import expensive_function
from module2 import method2
def method1():
return {'method1': expensive_function(), 'method2': method2()}
from utils import expensive_function
def method2():
return expensive_function()
import unittest
from unittest.mock import patch
from module1 import method1
def mock_expensive_function():
return 123
class TestCases(unittest.TestCase):
def test_unpatched(self):
self.assertEqual({'method1': 12345, 'method2': 12345}, method1())
@patch('module1.expensive_function', new=mock_expensive_function)
def test_method1_patched(self):
self.assertEqual({'method1': 123, 'method2': 12345}, method1())
@patch('module2.expensive_function', new=mock_expensive_function)
def test_method2_patched(self):
self.assertEqual({'method1': 12345, 'method2': 123}, method1())
@patch('module1.expensive_function', new=mock_expensive_function)
@patch('module2.expensive_function', new=mock_expensive_function)
def test_both_patched_inline(self):
self.assertEqual({'method1': 123, 'method2': 123}, method1())
@patch('module1.expensive_function')
@patch('module2.expensive_function')
def test_both_patched_magicmock(self, mock_in_module2, mock_in_module1):
mock_in_module1.return_value = mock_expensive_function()
mock_in_module2.return_value = mock_expensive_function()
self.assertEqual({'method1': 123, 'method2': 123}, method1())
if __name__ == '__main__':
unittest.main()
关于Python Mock.Patch 在多个地方使用的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44480674/
我正在尝试使用 Android SDK 中的 Draw9Patch 工具,但在使用 Draw9Patch 打开我的图像后,它立即将我的所有图像区域显示为“坏补丁”。我手动绘制了定义可拉伸(stretc
我有一个运行补丁命令的构建工具,如果补丁命令返回非零值,它将导致构建失败。我正在应用一个可能已经或可能尚未应用的补丁,所以我使用 -N选项 patch ,它应该跳过。但是,当它确实跳过时,patch返
patching file chrome/browser/gpu_process_host_ui_shim.cc Unreversed patch detected! Skipping patch.
尝试查看哪些模型最适合api(更新少,但对象结构可能经常更改,高读取应用程序) 我有这样的资源 Epic(ID、名称、描述、开始日期、结束日期、状态、故事) 故事(ID、名称、说明、开始日期、结束日期
我创建了新问题,因为我觉得上一个问题已经得到解答,这属于一个新的地方。 我跑 bitbake core-image-minimal我收到以下错误: ERROR: systemd-1_232-r0 do
我正在使用 ColdFusion 10 与 PayPal 的服务器和 for some requests 进行通信我需要执行 HTTP PATCH 请求 are not supported by CF
我试图了解这两种模拟方法之间的区别。有人可以帮助区分它们吗?对于这个例子,我使用 passlib 库。 from passlib.context import CryptContext from un
WebTarget webTarget = httpClient.target(url); Invocation.Builder invocationBuilder = webTarget.reque
当提到“提交补丁”时,补丁这个词究竟是什么意思? 我已经看到它被大量使用,尤其是在开源世界中。它是什么意思,提交补丁到底涉及什么? 最佳答案 这是一个文件,其中列出了已更改的代码文件之间的差异。它通常
对于 matplotlib.patches,patch.contains_point(xy) 方法似乎与 patch.get_path().contains_point(xy) 不同,至少在拥有之后将
这是什么RFC 5789说: The PATCH method requests that a set of changes described in the request entity be ap
在 Draw 9-patch 中,一切看起来都很好。 但是,我的 SDK 说 9-patch png 格式不正确。因为我有类似 11-patch png 的东西。因为我不希望小抓取区域被缩放。如何让它
我创建了一个使用 javax.xml.ws.Endpoint 来创建 REST 端点的类: @WebServiceProvider @ServiceMode(value = javax.xml.ws.
语境 我有一个 spring boot (version 2.2.6.RELEASE) web 项目。 从这个 Web 应用程序(我称之为“APP1”)我想使用来自另一个 Web 应用程序的 PATC
在为我的应用程序编写单元测试时,我一直使用 @mock.patch 和 @patch.object 装饰器。但是现在,当我使用装饰器进行一些单元测试时,我收到错误消息“TypeError: stati
我在使用@mock.patch.object 函数时观察到 nosetests 的一个非常奇怪的行为: 当我同时运行多个测试时,我得到的结果与单独运行它们时不同。具体来说,在某些情况下,当我一起运行多
我正在使用 RestSharp v107。 我想更新测试用例的迭代路径。我可以使用 Postman 更新它,但使用 RestSharp 我收到“BAD Request”-“您必须在请求正文中传递有效的
我已经阅读了 GNU 项目中关于开源和其他许可证的文章。某些许可证允许您将更改发布为补丁,而不是完整的源代码(例如 Q 公共(public)许可证或 gnuplot 许可证)。这是什么意思?这样的补丁
有谁知道免费的优质补丁程序?您知道,可以将其中包含旧程序的目录放入其中,然后将其与具有新版本的目录进行比较,然后吐出一个补丁,这仅仅是两者之间的区别? 另外,我正在寻找可以修补整个目录的东西,而不仅仅
由于我当时一直在使用 Subversion 和 shell 工具,git-gui这些都是不可能的。是否有任何 shell 工具可以交互式地逐行应用补丁? 最佳答案 尝试通过 --dry-run选项 p
我是一名优秀的程序员,十分优秀!