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

在 Django 單元測試中使用 mock 修補 celery 任務

Using mock to patch a celery task in Django unit tests(在 Django 單元測試中使用 mock 修補 celery 任務)
本文介紹了在 Django 單元測試中使用 mock 修補 celery 任務的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在嘗試使用 python 模擬庫來修補在我的 django 應用程序中保存模型時運行的 Celery 任務,以查看它是否被正確調用.

I'm trying to use the python mock library to patch a Celery task that is run when a model is saved in my django app, to see that it's being called correctly.

基本上,任務是在 myapp.tasks 中定義的,并像這樣在我的 models.py 文件的頂部導入:

Basically, the task is defined inside myapp.tasks, and is imported at the top of my models.py-file like so:

from .tasks import mytask

...然后使用 mytask.delay(foo, bar) 在模型內部的 save() 上運行.到目前為止一切順利 - 當我實際運行 Celeryd 等時效果很好.

...and then runs on save() inside the model using mytask.delay(foo, bar). So far so good - works out fine when I'm actually running Celeryd etc.

我想構建一個模擬任務的單元測試,只是為了檢查它是否被正確的參數調用,并且實際上并沒有嘗試運行 Celery 任務.

I want to construct a unit test that mocks the task, just to check that it gets called with the correct arguments, and doesn't actually try to run the Celery task ever.

所以在測試文件中,我在標準 TestCase 中有這樣的內容:

So in the test file, I've got something like this inside of a standard TestCase:

from mock import patch # at the top of the file

# ...then later
def test_celery_task(self):
    with patch('myapp.models.mytask.delay') as mock_task:
        # ...create an instance of the model and save it etc
        self.assertTrue(mock_task.called)

...但它永遠不會被調用/總是錯誤的.我嘗試了各種化身(修補 myapp.models.mytask ,并檢查是否調用了 mock_task.delay .我從模擬文檔中收集到導入路徑至關重要,谷歌搜索告訴我它應該是在被測模塊內部看到的路徑(應該是 myapp.models.mytask.delay 而不是 myapp.tasks.mytask.delay,如果我理解正確的話).

...but it never gets called/is always false. I've tried various incarnations (patching myapp.models.mytask instead, and checking if mock_task.delay was called instead. I've gathered from the mock docs that the import path is crucial, and googling tells me that it should be the path as it is seen inside the module under tests (which would be myapp.models.mytask.delay rather than myapp.tasks.mytask.delay, if I understand it correctly).

我哪里錯了?修補 Celery 任務是否有一些特定的困難?我可以修補 celery.task(用作 mytask 的裝飾器)嗎?

Where am I going wrong here? Is there some specific difficulties in patching Celery tasks? Could I patch celery.task (which is used as a decorator to mytask) instead?

推薦答案

您遇到的問題與這是 Celery 任務這一事實無關.你只是碰巧修補了錯誤的東西.;)

The issue that you are having is unrelated to the fact that this is a Celery task. You just happen to be patching the wrong thing. ;)

具體來說,您需要找出哪個視圖或其他文件正在導入mytask"并在那里對其進行修補,因此相關行如下所示:

Specifically, you need to find out which view or other file is importing "mytask" and patch it over there, so the relevant line would look like this:

with patch('myapp.myview.mytask.delay') as mock_task:

這里有更多的味道:

http://www.voidspace.org.uk/python/mock/patch.html#where-to-patch

這篇關于在 Django 單元測試中使用 mock 修補 celery 任務的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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 的意思不同)
主站蜘蛛池模板: 日本三级电影免费观看 | 蜜桃毛片 | 久久久免费精品 | 91正在播放 | 久久婷婷色 | 欧美一二三| 夜夜操天天操 | 亚洲人成人网 | 高清18麻豆 | 在线视频国产一区 | 欧洲一级毛片 | 91视频在线| 日韩一区二区三区在线 | 中文一区二区视频 | 亚洲在线视频 | 国产一区二区三区视频 | 欧洲精品一区 | 激情欧美一区二区三区中文字幕 | 国产成人福利在线观看 | 一区二区三区四区不卡视频 | 日韩欧美在线免费观看 | 怡红院怡春院一级毛片 | 日韩一区二区免费视频 | 亚洲成人福利在线观看 | 欧美 日韩 国产 在线 | 成人精品系列 | 午夜国产 | 色网站入口 | 亚洲一区二区久久 | 激情福利视频 | 国产激情视频网址 | 成人免费看 | 亚洲一区二区免费电影 | 91麻豆精品国产91久久久资源速度 | 欧美精品三区 | 亚州中文| 日韩欧美一区二区三区免费观看 | 日韩不卡一区二区 | 国产一区二区精品在线观看 | aaaaaaa片毛片免费观看 | 亚洲品质自拍视频网站 |