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

在鼻子下測(cè)試 Python 代碼時(shí),我應(yīng)該如何驗(yàn)證日志

How should I verify a log message when testing Python code under nose?(在鼻子下測(cè)試 Python 代碼時(shí),我應(yīng)該如何驗(yàn)證日志消息?)
本文介紹了在鼻子下測(cè)試 Python 代碼時(shí),我應(yīng)該如何驗(yàn)證日志消息?的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我正在嘗試編寫一個(gè)簡(jiǎn)單的單元測(cè)試,以驗(yàn)證在特定條件下,我的應(yīng)用程序中的類將通過標(biāo)準(zhǔn)日志記錄 API 記錄錯(cuò)誤.我不知道測(cè)試這種情況的最干凈的方法是什么.

I'm trying to write a simple unit test that will verify that, under a certain condition, a class in my application will log an error via the standard logging API. I can't work out what the cleanest way to test this situation is.

我知道鼻子已經(jīng)通過它的日志插件捕獲日志輸出,但這似乎是為了作為失敗測(cè)試的報(bào)告和調(diào)試幫助.

I know that nose already captures logging output through it's logging plugin, but this seems to be intended as a reporting and debugging aid for failed tests.

我可以看到的兩種方法是:

The two ways to do this I can see are:

  • 以零碎的方式 (mymodule.logging = mockloggingmodule) 或使用適當(dāng)?shù)哪M庫(kù)來模擬日志模塊.
  • 編寫或使用現(xiàn)有的鼻子插件來捕獲輸出并進(jìn)行驗(yàn)證.

如果我采用前一種方法,我想知道將全局狀態(tài)重置為模擬日志模塊之前的狀態(tài)的最簡(jiǎn)潔方法.

If I go for the former approach, I'd like to know what the cleanest way to reset the global state to what it was before I mocked out the logging module.

期待您對(duì)此的提示和技巧...

Looking forward to your hints and tips on this one...

推薦答案

我曾經(jīng)模擬記錄器,但是在這種情況下我發(fā)現(xiàn)最好使用記錄處理程序,所以我基于 jkp 建議的文件(現(xiàn)在已經(jīng)死了,但緩存在 互聯(lián)網(wǎng)檔案)

I used to mock loggers, but in this situation I found best to use logging handlers, so I wrote this one based on the document suggested by jkp(now dead, but cached on Internet Archive)

class MockLoggingHandler(logging.Handler):
    """Mock logging handler to check for expected logs."""

    def __init__(self, *args, **kwargs):
        self.reset()
        logging.Handler.__init__(self, *args, **kwargs)

    def emit(self, record):
        self.messages[record.levelname.lower()].append(record.getMessage())

    def reset(self):
        self.messages = {
            'debug': [],
            'info': [],
            'warning': [],
            'error': [],
            'critical': [],
        }

這篇關(guān)于在鼻子下測(cè)試 Python 代碼時(shí),我應(yīng)該如何驗(yàn)證日志消息?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

Patch __call__ of a function(修補(bǔ)函數(shù)的 __call__)
How to call self in a mock method of an object in Python?(如何在 Python 中對(duì)象的模擬方法中調(diào)用 self?)
Mocking only a single method on an object(僅模擬對(duì)象上的單個(gè)方法)
Mocking a subprocess call in Python(在 Python 中模擬子進(jìn)程調(diào)用)
Checking call order across multiple mocks(檢查多個(gè)模擬的調(diào)用順序)
How to properly use mock in python with unittest setUp(如何通過 unittest setUp 在 python 中正確使用 mock)
主站蜘蛛池模板: 香蕉久久网 | 91亚洲精品久久久电影 | 久久久久久久久久久成人 | 久久精品aaa | 欧美一区二区三区在线视频 | 欧美天堂在线观看 | 亚洲国产精品99久久久久久久久 | 亚洲高清视频一区二区 | 亚洲免费一区二区 | 日本三级全黄三级三级三级口周 | 日韩精品中文字幕在线 | 国产一区二区三区四区三区四 | 在线观看免费观看在线91 | 精品亚洲一区二区三区 | 综合久久99| 精品自拍视频 | 日韩成人免费 | 97av在线| 99精品在线 | 91精品国产91久久久久久 | 久草视频网站 | 国产精品日韩欧美一区二区三区 | 视频一区在线观看 | 国产在视频一区二区三区吞精 | 看特级黄色片 | 国产精品一区二区三区四区五区 | 欧美一区二区三区电影 | 国产乱精品一区二区三区 | 精品一区二区三区在线视频 | 日本一区二区三区在线观看 | 精品国产乱码久久久久久88av | 国产1区| 国产在线观 | 国内精品久久精品 | 黄网站在线播放 | 免费在线观看成年人视频 | 成人一级视频在线观看 | 九九免费视频 | 99热这里只有精品8 激情毛片 | 亚洲视频中文字幕 | 午夜日韩|