简体   繁体   English

如何模拟pytest.fixture装饰器?

[英]How to mock pytest.fixture decorator?

I wanted to write unit tests for pytest fixtures present in conftest.py 我想为conftest.py中存在的pytest固定装置编写单元测试
How do I mock decorator pytest.fixture? 如何模拟装饰器pytest.fixture?

Conftest.py Conftest.py

import pytest 

@pytest.fixture(scope="session", autouse="True")
def get_ip(dict_obj):
    """Assume some functionality"""
    return dict_obj.get('ip')


@pytest.fixture(scope="class")
def get_server(create_obj):
    """Assume some functionality"""
    pass

test_conftest.py test_conftest.py

mock_fixture = patch('pytest.fixture', lambda x : x).start()
from tests.conftest import get_ip  


class TestConftestTests:

    def test_mgmt_ip(self):
        assert mgmt_ip({"ip": "10.192.174.15"}) == "10.192.174.15"

 E   TypeError: <lambda>() got an unexpected keyword argument 'scope' 

When I tried to mock pytest.fixture at the starting of the test module before importing functions to be tested, I am getting error - E TypeError: <lambda>() got an unexpected keyword argument 'scope' 当我在导入要测试的函数之前尝试在测试模块的开头模拟pytest.fixture时,出现错误E TypeError: <lambda>() got an unexpected keyword argument 'scope'

If I remove the lambda function , I am getting E AssertionError: assert <MagicMock name='fixture()()()' id='4443565456'> == '10.192.174.15' 如果删除lambda函数E AssertionError: assert <MagicMock name='fixture()()()' id='4443565456'> == '10.192.174.15'收到E AssertionError: assert <MagicMock name='fixture()()()' id='4443565456'> == '10.192.174.15'

test_conftest.py test_conftest.py

patch('pytest.fixture').start()
from tests.conftest import get_ip  


class TestConftestTests:

    def test_mgmt_ip(self):
        assert mgmt_ip({"ip": "10.192.174.15"}) == "10.192.174.15"

E       AssertionError: assert <MagicMock name='fixture()()()' id='4443565456'> == '10.192.174.15' 

Could someone help me to resolve the error ? 有人可以帮我解决错误吗? Thanks! 谢谢!

@pytest.fixture(scope='session') is going to call your lambda x: x with the kwarg scope . @pytest.fixture(scope='session')将使用kwarg scope调用您的lambda x: x A mock covering both the no-args and some-args versions of pytest.fixture might be: 涵盖pytest.fixture的no-args和some-args版本的pytest.fixture可能是:

pytest_fixture = lambda x=None, **kw: x if callable(x) else fixture

But it should be noted pytest.fixture doesn't do any registration by itself. 但是需要注意的是pytest.fixture本身并不做任何注册。 It only marks the function as a fixture, by setting the _pytestfixturefunction attribute on it; 它仅通过在函数上设置_pytestfixturefunction属性将其标记为设备。 afterward, pytest collects the fixture. 之后,pytest收集灯具。

I'm not sure you're barking up the right tree for whatever you might be trying to accomplish. 我不确定您是否会为尝试完成的事情而树立正确的树。 If you want to change what value a fixture has in certain contexts, classes and subclasses can be used to override parent fixtures. 如果要更改灯具在某些上下文中的值,可以使用类和子类覆盖父灯具。 If you're trying to see whether a fixture is used, creating a new class and overriding the fixture with your assertion can be useful. 如果您尝试查看是否使用了固定装置,则创建一个新类并用您的断言覆盖固定装置可能会很有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM