Search code examples
python-3.xpytest

Pytest parametrize with values from fixture


I have a pytest fixture that takes a URL and returns some data:

@pytest.fixture
def queue_entry(url):
    return QueueEntry(url=url)

I am using httpbin in my tests.

@pytest.mark.parametrize('queue_entry', ???, indirect=True)
async def test_fetch_with_headers(self, env, httpbin, queue_entry):
    queue_entry = QueueEntry(httpbin.url + "/foo")

The URL itself comes from the httpbin fixture. What is the best way pass httpbin.url (defined by httpbin fixture) to my queue_entry fixture?


Solution

  • Fixtures can request other fixtures, so in your case queue_entry fixture can request httpbin fixture to get url created by it:

    
    import pytest
    
    @pytest.fixture
    def httpbin():
        url = "http://..."
        return url
    
    
    @pytest.fixture
    def queue_entry(httpbin):
        return QueueEntry(url=httpbin)
    

    For parametrizing a test from fixture, you can check my below answer:

    Pytest - parametrizing a test method from a fixture