久久久久久久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 的意思不同)
主站蜘蛛池模板: 日韩精品在线免费观看 | 一级黄色片视频 | 亚洲另类色图 | 99精品网| 欧美亚洲激情 | 在线亚洲精品 | 我要看一级黄色片 | 欧美一级片在线 | 日韩精品在线观看视频 | 18在线观看免费入口 | 日皮视频免费看 | 中文字幕免费观看视频 | 九九精品在线观看 | 久久久久性 | 亚洲欧美另类在线 | www视频在线观看网站 | 国产精品久久久久久久久 | 国产一区二三区 | 中文字幕97 | 日韩在线观看 | 国产精品毛片va一区二区三区 | 成人小网站 | 欧美一级淫片 | 欧美视频在线播放 | 日本免费一级片 | 精品一区在线播放 | 亚洲精品久久久久avwww潮水 | 免费视频久久 | 亚洲一区久久 | 亚洲在线播放 | 欧美激情一区二区三区 | 欧美日韩亚洲综合 | 亚洲精品成人 | 精品少妇3p | 亚洲一区日韩 | 亚洲精品1区 | 性久久久久久 | 国产黄色在线观看 | 欧美色综合天天久久综合精品 | 91狠狠综合 | 丁香婷婷激情 |