Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 禁用特定pytest标记上的自动使用装置_Python_Pytest_Pytest Django - Fatal编程技术网

Python 禁用特定pytest标记上的自动使用装置

Python 禁用特定pytest标记上的自动使用装置,python,pytest,pytest-django,Python,Pytest,Pytest Django,是否可以防止仅在特定标记上执行带有autouse=True的“功能范围”夹具 我将以下装置设置为autouse,以便自动模拟所有传出请求: @pytest.fixture(autouse=True) def no_requests(monkeypatch): monkeypatch.setattr("requests.sessions.Session.request", MagicMock()) 但是我有一个名为endtoend的标记,我用它来定义一系列测试,这些测试允许外部请求进行更

是否可以防止仅在特定标记上执行带有
autouse=True
的“功能范围”夹具

我将以下装置设置为autouse,以便自动模拟所有传出请求:

@pytest.fixture(autouse=True)
def no_requests(monkeypatch):
    monkeypatch.setattr("requests.sessions.Session.request", MagicMock())
但是我有一个名为
endtoend
的标记,我用它来定义一系列测试,这些测试允许外部请求进行更健壮的端到端测试。我想在所有测试(绝大多数)中注入
no_请求
,但不是在以下测试中:

@pytest.mark.endtoend
def test_api_returns_ok():
    assert make_request().status_code == 200

这可能吗?

我找不到一种方法来禁用带有
autouse=True
的装置,但我确实找到了一种方法来恢复在我的
no\u请求中所做的更改。有一个方法
undo
,可以还原堆栈上的所有补丁,因此我可以在endtoend测试中调用它,如下所示:

@pytest.mark.endtoend
def test_api_returns_ok(monkeypatch):
    monkeypatch.undo()
    assert make_request().status_code == 200

取消或更改自动使用将很困难,而且可能不可能

你不能自动使用,因为它是自动的。也许您可以根据标记的情况更改自动使用夹具。但这将是一种刻薄和困难的做法

可能与:

import pytest
from _pytest.mark import MarkInfo
我找不到这样做的方法,但是可能
@pytest.fixture(autouse=True)
可以获得MarkInfo,如果它返回'endtoend',fixture将不会设置该属性。但您还必须在夹具参数中设置一个条件

i、 e.:
@pytest.fixture(True=MarkInfo,autouse=True)
。差不多吧。但是我找不到办法


建议您组织测试以防止出现这种情况

您可以通过以下方式将no_请求与endtoend测试分开:

  • 限制自动使用装置的范围
  • 将no_请求放入一个类中
  • 不要使其自动使用,只需将其传递到所需的每个def的参数中即可
  • 像这样:

    class NoRequests:
        @pytest.fixture(scope='module', autouse=True)
        def no_requests(monkeypatch):
            monkeypatch.setattr("requests.sessions.Session.request", MagicMock())
        def test_no_request1(self):
            # do stuff here
        # and so on
    
    这是很好的做法。也许另一个组织能帮上忙


    但在您的情况下,可能最容易
    monkeypatch.undo()
    您也可以使用夹具中的来检查测试中使用的标记,如果设置了特定标记,则不做任何操作:

    import pytest
    
    @pytest.fixture(autouse=True)
    def autofixt(request):
        if 'noautofixt' in request.keywords:
            return
        print("patching stuff")
    
    def test1():
        pass
    
    @pytest.mark.noautofixt
    def test2():
        pass
    
    使用
    -vs
    输出:

    x.py::test1 patching stuff
    PASSED
    x.py::test2 PASSED
    

    如果在特定的模块或类中有
    end-to-end
    测试,您也可以只在本地执行
    no\u请求
    fixture,例如,假设您将所有集成测试分组在一个名为
    end\u-to\u-end.py的文件中:

    #test_end_to_end.py
    @pytest.fixture(autouse=True)
    def no_请求():
    返回
    def test_api_返回_ok():
    #应该提出真正的要求。
    断言发出请求()。状态代码==200
    
    答案非常聪明!非常有用!作为附录<代码>请求。当标记存在时,关键字[“noautofixit”]
    仅返回bool
    True
    。如果您的标记有args,最好是
    请求.node.iter\u标记(“noautofixit”)
    ,然后您可以使用
    名称
    args
    kwargs
    访问
    标记
    对象。