Python 我可以有一个通用的fixture并调用与测试中不同的值?

Python 我可以有一个通用的fixture并调用与测试中不同的值?,python,pytest,fixtures,Python,Pytest,Fixtures,我是python和pytest的新手,现在我了解了基本的工作原理。但我找不到以下场景的文档: 我创建了一个通用的装置,在我的项目中使用了默认的模拟值,并且在测试中使用了这个装置,它工作得很好。比如: #fixture @pytest.fixture() def generic_fixture(mocker): mocker.patch('project.some_class.url', "http://some_url:5002") #test def test_1

我是python和pytest的新手,现在我了解了基本的工作原理。但我找不到以下场景的文档:

我创建了一个通用的装置,在我的项目中使用了默认的模拟值,并且在测试中使用了这个装置,它工作得很好。比如:

#fixture
@pytest.fixture()
def generic_fixture(mocker):
    mocker.patch('project.some_class.url', "http://some_url:5002")

#test
def test_1(generic_fixture):
    do_something() # method that uses values from my generic fixture
    assert True
#fixture
@pytest.fixture()
def generic_fixture(mocker, parameter_url):
    if parameter_url == None
        mocker.patch('project.some_class.url', "http://some_url:5002")
    else:
        mocker.patch('project.some_class.url', parameter_url)

#test
def test_1(generic_fixture(parameter_url)):
    do_something() # method that uses values from my generic fixture
    assert True
现在我想知道是否有办法将测试中的参数传递给fixture调用,以做出一些决策,比如:

#fixture
@pytest.fixture()
def generic_fixture(mocker):
    mocker.patch('project.some_class.url', "http://some_url:5002")

#test
def test_1(generic_fixture):
    do_something() # method that uses values from my generic fixture
    assert True
#fixture
@pytest.fixture()
def generic_fixture(mocker, parameter_url):
    if parameter_url == None
        mocker.patch('project.some_class.url', "http://some_url:5002")
    else:
        mocker.patch('project.some_class.url', parameter_url)

#test
def test_1(generic_fixture(parameter_url)):
    do_something() # method that uses values from my generic fixture
    assert True
我怎样才能做好呢?我查看了monkeypath、@pytest.mark.parametrize和其他一些结构,但它们都不能满足我的要求。

您可以使用它们

#fixture
@pytest.fixture()
def generic_fixture(request, mocker):
    parameter_url = request.node.get_closest_marker("generic_fixture_url")
    ...

#test
@pytest.mark.generic_fixture_url("url")
def test_1(generic_fixture):
    ...

其他的选择是把你的固定装置变成一个或在你的测试模块上。

D Malan的答案对我帮助很大

我将把我的最终解决方案放在这里,因为我使用了上述答案所描述的内容和其他内容,我认为它对具有类似结构的人是有用的,因为至少对我来说,很难找到它(如果更好的话,有人注意到我,所以我把这个答案放在问题中):

conftest.py

def pytest_configure(config):
    # to avoid warnings
    config.addinivalue_line("markers", "my_fixture_url")

@pytest.fixture()
    def my_fixture(request):
        if request.node.get_closest_marker("my_fixture_url") and request.node.get_closest_marker("my_fixture_url").args[0]:
            something = request.node.get_closest_marker("my_fixture_url").args[0]
        else:
            something = 'www.google.com'
            ...
test.py

@pytest.mark.my_fixture_url({'url': 'www.stackoverflow.com'})
def test_1(my_fixture):
    ...

def test_2(my_fixture):
    ...
我在mark中使用了字典,因为在某些情况下,我希望发送多个参数,和/或使用每个参数覆盖某些默认值

注:我使用夹具进行了第二次测试,但没有标记。我可以这样做,并使用fixture中的默认值

这样,我可以在需要默认值和需要覆盖部分夹具配置的情况下使用相同的夹具


我希望它对某人有所帮助:D

可以将您的解决方案作为答案发布。你也可以接受@DMalan给出的答案,如果它给了你答案的话。谢谢Bean先生让我记住一些答案是正确的。我给了一些时间看看是否有人有不同的意见。在本例中,我将标记我的,因为正如我所说,@DMalan对于理解我所怀疑的结构部分非常有帮助。但我认为完整的解决方案可能对某些人有用,至少我没有找到所有部分的示例,这对我来说确实是一个挑战。如果你不同意,让我知道,这样我可以改变它。