久久久久久久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色综合 | 亚洲精品国产成人 | 久久国产秒 | 中文字幕不卡在线观看 | 免费视频一区 | 一级黄片一级毛片 | av在线播放网站 | 四虎首页| 人成久久| japan21xxxxhd美女| 国产成人精品一区二 | 中文字幕一区二区三区精彩视频 | 色久电影 | 欧洲精品在线观看 | 欧美区日韩区 | 久久精品一区 | 精品国产视频 | 爱爱免费视频 | 日日碰狠狠躁久久躁96avv | 97超碰在线播放 | 久久视频免费看 | 91日日 | 成人一区二 | 中文字幕视频在线看 | 日韩欧美高清 | 99精品视频一区二区三区 | com.色.www在线观看 | 国产精品视频免费观看 | 免费久久网 | 免费麻豆视频 | 日韩精品一区二区三区中文在线 | 欧美日韩视频网站 | 91精品国产美女在线观看 | xx性欧美肥妇精品久久久久久 | av大全在线 | 亚洲www啪成人一区二区麻豆 | 国产精品一区一区三区 | 精品一区二区三区在线视频 | 一区视频在线免费观看 | 亚洲欧美视频一区 | 久久国产精品一区二区三区 |