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

在鼻子下測試 Python 代碼時,我應該如何驗證日志

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

問題描述

我正在嘗試編寫一個簡單的單元測試,以驗證在特定條件下,我的應用程序中的類將通過標準日志記錄 API 記錄錯誤.我不知道測試這種情況的最干凈的方法是什么.

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)通過它的日志插件捕獲日志輸出,但這似乎是為了作為失敗測試的報告和調(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) 或使用適當?shù)哪M庫來模擬日志模塊.
  • 編寫或使用現(xiàn)有的鼻子插件來捕獲輸出并進行驗證.

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

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.

期待您對此的提示和技巧...

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': [],
        }

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

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

相關文檔推薦

Patch __call__ of a function(修補函數(shù)的 __call__)
How to call self in a mock method of an object in Python?(如何在 Python 中對象的模擬方法中調(diào)用 self?)
Mocking only a single method on an object(僅模擬對象上的單個方法)
Mocking a subprocess call in Python(在 Python 中模擬子進程調(diào)用)
Checking call order across multiple mocks(檢查多個模擬的調(diào)用順序)
How to properly use mock in python with unittest setUp(如何通過 unittest setUp 在 python 中正確使用 mock)
主站蜘蛛池模板: 一区二区三区久久久 | 欧美福利视频 | 亚洲综合在线播放 | 狠狠的干狠狠的操 | 视频一区二区三区在线观看 | 在线观看av网站永久 | 久久九九99 | 古典武侠第一页久久777 | 久久精品女人天堂av | 91精品国产乱码久久久 | 91资源在线播放 | 久久亚洲一区二区三区四区 | 国产一区二区三区视频 | 色婷婷国产精品综合在线观看 | 免费看一区二区三区 | 秋霞国产 | 国产精品国产精品 | 久久精品二区亚洲w码 | 国产精品久久久久无码av | 国产乱码精品一区二区三区中文 | 久久久久九九九女人毛片 | 国产精品一区二区视频 | 草草视频在线观看 | 最新中文字幕在线 | 亚洲国产偷 | 黄色一级免费观看 | 久久精品成人热国产成 | 7777奇米影视| 久久大全 | 亚洲视频一区二区三区 | 日韩在线中文字幕 | 成人亚洲精品 | 黄色大片免费网站 | 久优草 | 日韩精品av| 国产黄色小视频 | 日韩一区在线视频 | 男女网站免费观看 | 少妇一级淫片aaaaaaaaa | 精品视频99 | 黄色网址在线免费播放 |