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

如何在具有模擬裝飾器的測試中使用 pytest capsy

How to use pytest capsys on tests that have mocking decorators?(如何在具有模擬裝飾器的測試中使用 pytest capsys?)
本文介紹了如何在具有模擬裝飾器的測試中使用 pytest capsys?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我一直在嘗試找到一種同時使用模擬裝飾器和 pytest capsys 的方法,但我找不到正確的方法.

I have being trying to find a way to use mocking decorators and pytest capsys at the same time but I wasn't able to find the right way to do it.

import pytest
import requests_mock


@requests_mock.mock()
def test_with_mock(m):
    pass

def test_with_capsys(capsys):
    pass


# how to write a test that works with both?

推薦答案

request-mock 的文檔:

As stated in the request-mock's docs:

pytest 有自己的注冊和加載自定義夾具的方法.requests-mock 提供了一個用 pytest 注冊的外部夾具,因此只需將其指定為參數(shù)即可使用.無需導入requests-mock,只需要安裝并指定參數(shù)requests_mock.

pytest has its own method of registering and loading custom fixtures. requests-mock provides an external fixture registered with pytest such that it is usable simply by specifying it as a parameter. There is no need to import requests-mock it simply needs to be installed and specify the argument requests_mock.

然后,fixture 提供與 requests_mock.Mocker 相同的接口,讓您可以按預期使用 requests-mock.

The fixture then provides the same interface as the requests_mock.Mocker letting you use requests-mock as you would expect.

>>> import pytest
>>> import requests

>>> def test_url(requests_mock):
...     requests_mock.get('http://test.com', text='data')
...     assert 'data' == requests.get('http://test.com').text
...

所以只需使用 requests_mock 夾具而不是裝飾器:

So just use the requests_mock fixture instead of the decorator:

def test_with_mock_and_capsys(requests_mock, capsys):
    pass

背景

pytest 不能與向測試函數(shù)添加位置參數(shù)的函數(shù)裝飾器一起使用.pytest 考慮所有參數(shù)

Background

pytest doesn't play along with function decorators that add positional arguments to the test function. pytest considers all arguments that

  • 不像在實例或類方法中那樣綁定到實例或類型;
  • 沒有默認值;
  • 不與 functools.partial;
  • 綁定
  • 不替換為 unittest.mock 模擬

被替換為夾具值,如果沒有找到適合任何參數(shù)的夾具,則會失敗.所以像

to be replaced with fixture values, and will fail if it doesn't find a suitable fixture for any argument. So stuff like

import functools
import pytest


def deco(func):
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        args += ('spam',)
        return func(*args, **kwargs)
    return wrapper


@deco
def test_spam(spam_arg):
    assert True

會失敗,而這正是 requests-mock 所做的.一種解決方法是通過關鍵字 args 傳遞 mocker:

will fail, and this is exactly what requests-mock does. A workaround to that would be passing the mocker via keyword args:

import pytest
import requests_mock


@requests_mock.Mocker(kw='m')
def test_with_mock_and_fixtures(capsys, **kwargs):
    m = kwargs['m']
    ...

但是既然 requests-mock 已經(jīng)提供了一個fixture,為什么還要使用裝飾器呢?

but since requests-mock already offers a fixture, why bother using the decorator?

這篇關于如何在具有模擬裝飾器的測試中使用 pytest capsys?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關文檔推薦

Python 3 Float Decimal Points/Precision(Python 3 浮點小數(shù)點/精度)
Converting Float to Dollars and Cents(將浮點數(shù)轉換為美元和美分)
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 的意思不同)
主站蜘蛛池模板: h视频亚洲| 一区二区免费在线观看 | 91国内精精品久久久久久婷婷 | 亚洲免费一区二区 | 特级黄色毛片 | 91欧美精品成人综合在线观看 | 久久久久久国产精品 | 国产精品黄色 | 国产精品自产拍 | 亚洲成av人片在线观看无码 | 一区二区三区视频在线 | 亚洲视频免费在线观看 | 午夜精品一区 | 99国产精品视频免费观看一公开 | 亚洲一区二区电影在线观看 | 完全免费在线视频 | 女同av亚洲女人天堂 | 五月香婷婷 | 亚洲成人精选 | 三a毛片| 国产精品一区二区在线 | 日韩视频在线一区二区 | 伊人国产精品 | av成人在线观看 | 亚洲精品久久久久久宅男 | 国产精品一区二区三区在线播放 | 美女网站视频免费黄 | 免费黄色网址视频 | 日韩欧美国产精品 | 特级黄色毛片 | 久久久久亚洲精品 | 福利国产| 国产精品永久免费观看 | 国产高清精品一区 | 欧美美女二区 | 欧美久久久久久 | 综合久久99| 国产www成人 | 米奇成人网 | 国产一区二区精品自拍 | 欧美2区 |