久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

'function' 對象沒有屬性 'assert_call_once_w

#39;function#39; object has no attribute #39;assert_called_once_with#39;(function 對象沒有屬性 assert_call_once_with)
本文介紹了'function' 對象沒有屬性 'assert_call_once_with'的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在嘗試使用 pytest 和 pytest_mock 運行以下測試

I'm trying to run the following test using pytest and pytest_mock

def rm(filename):
    helper(filename, 5)

def helper(filename):
    pass

def test_unix_fs(mocker):
    mocker.patch('module.helper')
    rm('file')
    helper.assert_called_once_with('file', 5)

但我得到異常 AttributeError: 'function' object has no attribute 'assert_called_once_with'

我做錯了什么?

推薦答案

你不能在 vanilla 函數上執行 .assert_call_once_with 函數:你首先需要包裝它與 mock.create_autospec 裝飾器.比如:

You can not perform a .assert_called_once_with function on a vanilla function: you first need to wrap it with the mock.create_autospec decorator. So for instance:

import unittest.mock as mock

def rm(filename):
    helper(filename, 5)

def helper(filename):
    pass

helper = mock.create_autospec(helper)

def test_unix_fs(mocker):
    mocker.patch('module.helper')
    rm('file')
    helper.assert_called_once_with('file', 5)

或者更優雅:

import unittest.mock as mock

def rm(filename):
    helper(filename, 5)

@mock.create_autospec
def helper(filename):
    pass

def test_unix_fs(mocker):
    mocker.patch('module.helper')
    rm('file')
    helper.assert_called_once_with('file', 5)

請注意,斷言將失敗,因為您僅使用 'file' 調用它.所以一個有效的測試是:

Note that the assertion will fail, since you call it only with 'file'. So a valid test would be:

import unittest.mock as mock

def rm(filename):
    helper(filename, 5)

@mock.create_autospec
def helper(filename):
    pass

def test_unix_fs(mocker):
    mocker.patch('module.helper')
    rm('file')
    helper.assert_called_once_with('file')

編輯:如果函數是在某個模塊中定義的,您可以將其包裝在本地的裝飾器中.例如:

EDIT: In case the function is defined in some module, you can wrap it in a decorator locally. For example:

import unittest.mock as mock
from some_module import some_function

some_function = mock.create_autospec(some_function)

def test_unix_fs(mocker):
    some_function('file')
    some_function.assert_called_once_with('file')

這篇關于'function' 對象沒有屬性 'assert_call_once_with'的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Python 3 Float Decimal Points/Precision(Python 3 浮點小數點/精度)
Converting Float to Dollars and Cents(將浮點數轉換為美元和美分)
What are some possible calculations with numpy or scipy that can return a NaN?(numpy 或 scipy 有哪些可能的計算可以返回 NaN?)
Python float to ratio(Python浮動比率)
How to manage division of huge numbers in Python?(如何在 Python 中管理大量數字的除法?)
mean from pandas and numpy differ(pandas 和 numpy 的意思不同)
主站蜘蛛池模板: 成人精品在线视频 | 精品乱码一区二区 | 国产美女精品视频 | 精品国产一区二区三区久久狼黑人 | 国产高潮av | av一区二区三区四区 | 国产精品一区二区三区四区 | 亚洲精品片 | 成人做爰www免费看视频网站 | 国产乱码久久久久久 | 日本久久久一区二区三区 | 亚洲精品一二三区 | 精品三级在线观看 | 午夜专区 | 青青草综合网 | 999久久久久久久久 国产欧美在线观看 | 久久久这里只有17精品 | 99视频 | 99免费在线观看视频 | 久久精品国产亚洲a | av在线播放网址 | 亚洲国产成人av | 国产 日韩 欧美 中文 在线播放 | 无码一区二区三区视频 | 日日噜| 北条麻妃一区二区三区在线观看 | 丁香婷婷久久久综合精品国产 | 精品中文字幕一区二区三区 | 成人免费黄色片 | 亚洲国产成人久久综合一区,久久久国产99 | 国产目拍亚洲精品99久久精品 | 一区二区中文 | 成人国产午夜在线观看 | 亚洲视频免费观看 | 久久综合久久综合久久综合 | 亚洲一区二区三区在线播放 | 少妇精品久久久久久久久久 | 一区二区三区视频在线观看 | 国产特黄一级 | 婷婷桃色网 | 国产精品中文字幕一区二区三区 |