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

模擬 - 如何在調用者上引發異常?

Mocking - How do I raise exception on the caller?(模擬 - 如何在調用者上引發異常?)
本文介紹了模擬 - 如何在調用者上引發異常?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

假設這是代碼

def move(*args, **kwargs):   
    try:
        shutil.move(source, destination)
    except Exception as e:
        raise e

在我的tests.py中

and in my tests.py

@patch.object(shutil, 'move')
def test_move_catch_exception(self, mock_rmtree):
    ''' Tests moving a target hits exception. '''
    mock_rmtree.side_effect = Exception('abc')
    self.assertRaises(Exception, move,
                             self.src_f, self.src_f, **self.kwargs)

它是這么說的

  File "unittests.py", line 84, in test_move_catch_exception
    self.src_f, self.src_f, **self.kwargs)
AssertionError: Exception not raised

如果我在 mock_rmtree 上斷言它會通過.如何對調用者進行斷言(在本例中為函數 move)?

If I assert on mock_rmtree it will pass. How can I assert on the caller (in this case, the function move)?

正如 aquavitae 指出的那樣,主要原因是復制粘貼錯誤,而且我一開始就斷言了一個元組.始終斷言正確的返回類型...

As aquavitae pointed out, the primary reasons was copy-paste error, and also I was asserting a tuple in the beginning. Always asseert with the right return type...

推薦答案

您的示例中有錯字,缺少 '.

You've got a typo in your example, missing a '.

不完全清楚你在問什么,但如果我理解正確的話,你問的是如何測試在 move 中捕獲了引發的異常.一個問題是您正在修補 shutil.rmtree,而不是 shutil.move,但您不能確定 shutil.rmtree 是否會永遠被稱為.shutil.move 僅在成功復制目錄時才實際調用 shutil.rmtree ,但是由于您將 self.src_f 復制到自身,因此不會發生.雖然這不是一個很好的修補方法,因為 shutil.move 將調用 shutil.rmtree 的假設根本無法保證,并且取決于實現.

Its not entirely clear what you're asking, but if I understand you correctly, you're asking how to test that a raised exception is caught inside move. One problem is that you're patching shutil.rmtree, not shutil.move, but you can't be certain thatshutil.rmtree will ever be called. shutil.move only actually calls shutil.rmtree if it successfully copies a directory, but since you're copying self.src_f to itself, this doesn't happen. This is not a very good way of patching it though, because the assumption that shutil.move will call shutil.rmtree at all is not guaranteed and is implementation dependent.

至于如何測試,簡單檢查返回值是否符合預期:

As for how to test it, simply check that the return value is as expected:

@patch.object(shutil, 'move')
def test_move_catch_exception(self, mock_move):
    ''' Tests moving a target hits exception. '''
    e = OSError('abc')
    mock_move.side_effect = e
    returns = move(self.src_f, self.src_f, **self.kwargs)
    assert returns == (False, e)

這篇關于模擬 - 如何在調用者上引發異常?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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 的意思不同)
主站蜘蛛池模板: 久久亚洲一区二区三区四区 | 欧美中文字幕 | 亚洲精品在线观看视频 | 免费高清成人 | 精品亚洲一区二区三区四区五区 | 一本一道久久a久久精品综合 | 在线观看日韩av | 免费九九视频 | 日韩电影免费观看中文字幕 | 国产精品日韩欧美一区二区三区 | 男女污污网站 | 黄网站在线播放 | xxxxxx国产| 午夜精品一区二区三区三上悠亚 | 成人三级网址 | 欧美成人高清 | 久久婷婷色 | 九九av | 久久在线视频 | 一区二区视频在线观看 | 亚洲一区中文字幕在线观看 | 精品国产一区二区三区久久狼黑人 | 国产精品99久久久久久宅男 | 韩国av一区二区 | 日韩精品视频在线播放 | 午夜精品一区二区三区免费视频 | 888久久久 | 一区二区三区四区不卡 | 国产视频中文字幕 | 粉嫩av久久一区二区三区 | 日韩亚洲视频 | 日韩av资源站 | 91精品中文字幕一区二区三区 | 欧美一区精品 | 亚洲欧洲色视频 | tube国产 | 国产女人叫床高潮大片免费 | 国产日产精品一区二区三区四区 | 偷牌自拍 | 一级片av | 一区二区精品视频 |