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

模擬 - 如何在調(diào)用者上引發(fā)異常?

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

問題描述

假設(shè)這是代碼

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 上斷言它會通過.如何對調(diào)用者進(jìn)行斷言(在本例中為函數(shù) move)?

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

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

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 中捕獲了引發(fā)的異常.一個問題是您正在修補 shutil.rmtree,而不是 shutil.move,但您不能確定 shutil.rmtree 是否會永遠(yuǎn)被稱為.shutil.move 僅在成功復(fù)制目錄時才實際調(diào)用 shutil.rmtree ,但是由于您將 self.src_f 復(fù)制到自身,因此不會發(fā)生.雖然這不是一個很好的修補方法,因為 shutil.move 將調(diào)用 shutil.rmtree 的假設(shè)根本無法保證,并且取決于實現(xiàn).

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.

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

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)

這篇關(guān)于模擬 - 如何在調(diào)用者上引發(fā)異常?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

Python 3 Float Decimal Points/Precision(Python 3 浮點小數(shù)點/精度)
Converting Float to Dollars and Cents(將浮點數(shù)轉(zhuǎn)換為美元和美分)
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 中管理大量數(shù)字的除法?)
mean from pandas and numpy differ(pandas 和 numpy 的意思不同)
主站蜘蛛池模板: 亚洲欧洲激情 | 青青久草 | 亚洲一区二区三区在线视频 | 99资源站| 成人不卡 | 亚洲日韩中文字幕一区 | 亚洲日本视频 | 国产天堂| 亚洲 欧美 另类 日韩 | 精品久久久久久亚洲综合网站 | 男人的天堂在线视频 | 亚洲免费在线 | 欧美日韩电影免费观看 | 日韩小视频 | 中文字幕一区二区视频 | 亚洲欧美国产精品一区二区 | 欧美一区二区另类 | 99这里只有精品视频 | 中文字幕国产日韩 | 国产目拍亚洲精品99久久精品 | 亚洲精品中文字幕在线观看 | 国产在线视频在线观看 | 亚洲欧美日韩久久久 | 亚洲精品一区二区网址 | 国产精品一区一区 | 在线观看国产视频 | 欧美色综合 | 亚洲精品欧美 | 国产一区在线免费 | 中文字幕日韩欧美一区二区三区 | 欧美一级毛片在线播放 | 白浆在线| 亚洲精品第一 | 亚洲欧美精品在线 | 欧美精品99| 成人国产精品 | 国产精品久久久久久久7电影 | 亚洲 欧美 综合 | 亚洲午夜精品一区二区三区他趣 | 国产欧美视频一区二区 | 亚洲综合五月天婷婷 |