- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试使用 PyTest_Mock 在我的 Python 项目中进行一些测试。我创建了一个非常简单的测试来尝试它,但我收到了一个 AttributeError 并且我不知道为什么。
模型.py
def square(x):
return x * x
if __name__ == '__main__':
res = square(5)
print("result: {}".format(res))
test_model.py
import pytest
from pytest_mock import mocker
import model
def test_model():
mocker.patch(square(5))
assert model.square(5) == 25
运行 python -m pytest
后我遇到了失败并出现以下错误:
def test_model():
> mocker.patch(square(5))
E AttributeError: 'function' object has no attribute 'patch'
test_model.py:7: AttributeError
最佳答案
您不需要导入 mocker
,它可以作为 fixture 使用,因此您只需将它作为参数传递到测试函数中即可:
def test_model(mocker):
mocker.patch(...)
square(5)
的计算结果为 25,因此 mocker.patch(square(5))
将有效地尝试修补数字 25。相反,通过作为参数的函数名称:要么
mocker.patch('model.square')
或
mocker.patch.object(model, 'square')
修补后,square(5)
将不再返回 25,因为原始函数已替换为可以返回任何内容的模拟对象,并且默认情况下将返回一个新的模拟对象。 assert model.square(5) == 25
将因此失败。通常,您修补东西以避免复杂的测试设置或模拟测试场景中所需的组件行为(例如,网站不可用)。在您的示例中,您根本不需要模拟。
完整的工作示例:
import model
def test_model(mocker):
mocker.patch.object(model, 'square', return_value='foo')
assert model.square(5) == 'foo'
关于python - 由于 AttributeError PyTest-Mock 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51713849/
我有一段代码是这样的: while(count = inputStream.readLine()) != null) { //do something } 在单元测试用例中,我正在模拟 inp
我有一段代码是这样的: while(count = inputStream.readLine()) != null) { //do something } 在单元测试用例中,我正在模拟 inp
假设我在文件中具有以下命名导出 customer.ts export const saveDetails = ()=>{} export const loadDetails = ()=>{} 假设我在
我一直试图绕过dart的模拟库,但似乎我仍然不明白。 在我的库中,我有一个对外部资源的HTTP请求,我想以此模拟它不要一直依赖外部资源。 我的库中的主类如下所示: SampleClass(String
使用 postman 模拟服务器存在问题。它不响应具有路由参数的请求。例如,我的 uri 如下所示: PUT : {{server_url}}/order/{id} 但是当我以这种方式调用模拟服务器时
将任何转换器或空属性转换添加到 Jest 配置时,模拟无法正常工作。下面是简单的代码。 Jest 配置: "transform": { any regex: any transformer } 模块说
我最近一直在做一个项目,该项目已经开始变得相当依赖,并且一直在探索使用 AutoMocking 容器来清理我的测试并使其不那么脆弱的想法。 我听说过 TDD/BDD 纯粹主义者反对使用它们的论点,例如
有谁知道为什么 UsernameExists 不会返回 True。我的语法一定在某个地方搞砸了。 [TestMethod()] public void GenerateUsername
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题? Update the question所以它是on-topic对于堆栈溢出。 10年前关闭。 Improve this qu
我得到一个 Moq 对象以在连续调用方法时返回不同的值。这是通过此扩展方法完成的: public static void ReturnsInOrder(this ISetup setup, param
rhino-mocks stub 和这里的期望有什么区别:在我看来它们的行为完全相同? mockContext.Stub(x => x.Find()) .Return(new List()
我正在尝试模拟方法 extra_get() 的调用,该方法通常返回一个字典列表。据我从模拟docs了解,如果我想返回iterable,我应该设置side_effect参数。 client.extra_
在我的 CentOS 6.2 机器的/var/lib/mock 文件夹下,我可以看到目标构建操作系统中的所有初始应用程序。如果我想添加 JDK 作为额外的应用程序,我该怎么做?谢谢! 最佳答案 只需将
我正在寻找一种让 stub 的返回值取决于其输入的干净方法。 目前我正在使用以下方法,但效果不佳。 metadataLogic.Expect(x => x.GetMake(args.Vehicle1.
我正在设置调用构建和执行查询的方法的期望。我想询问所用参数的属性。这可能吗 using (mocks.Record()) { Expect.Call(connection.Retrieve(S
有没有人有一个使用 Rhino Mocks 和 NInject 的自动模拟容器的实现? 最佳答案 好的,我使用 Moq 集成作为起点自己构建了一个。这很简单。你需要这 3 个类: public cla
使用后@patch在我的装饰器上它不再起作用了。我想进行一个失败并引发异常的调用,以便我可以检查我的装饰器是否正在捕获此异常,并正在调用某个函数。 mock do_sth_in_db让它引发异常是很容
谁能告诉我为什么下面的测试没有失败? [Test] public void uhh_what() { var a = MockRepository.GenerateMock(); a.
我找不到 Moq 和 Rhino 的具体功能比较。所有的问题都是“你更喜欢哪个以及为什么”,或者“这是你如何在 rhino 中进行简单的模拟以及如何在最小起订量中完成的”。 我在任何地方都找不到深入的
我正在尝试模拟数据存储库对象,但在 MockRepository 上设置期望后,它每次都返回 null。我的代码如下: [测试] 公共(public)无效 GetById_NotNull() { 预
我是一名优秀的程序员,十分优秀!