How to parametrize test that uses a fixture that is not directly needed

Viewed 48

I have the following scenario:

@pytest.fixture
def mocker_search_posts(mocker):
    post = Post(
        id=1,
        title='Title',
        body='Body',
        tags=['tag A']
    )

    patch = mocker.patch('path.to.search_posts', return_value=[post])
    
    return [post, patch]


@pytest.fixture
def mocker_create_post(mocker):
    post = Post(
        id=0,
        title='New Post Title',
        body='Body',
        tags=['tag A']
    )

    return mocker.patch('path.to.create_post', return_value=post)

@pytest.fixture
def mocker_search_comments(mocker):
    comment = Comment(
        id=1,
        content='Comment content',
        moderation_date: datetime.now(pytz.utc)
    )

    patch = mocker.patch('path.to.search_comments', return_value=[comment])
    
    return [comment, patch]
    

@pytest.mark.usefixtures("mocker_search_comments")
def test_row_creation(mocker_search_posts, mocker_create_post):
    post, _ = mocker_search_posts
    create_posts()

    _, kwargs = mocker_create_post.call_args_list[0]
    #a few asserts

Don't worry to much about the example, it's not my real code, it is just structure like mine.

I need to define two parameters: a list of tags and a date. The idea is to generate multiple scenarios in a single test, like this:

@pytest.mark.parametrize("date", [datetime.now(pytz.utc), datetime.now(pytz.utc) - timedelta(minutes=26)])
@pytest.mark.parametrize("tags", [[TAG_A],
                                  [TAG_A, TAG_B]])

This would create the following scenarios:

  • current date, [TAG_A]
  • current date, [TAG_A, TAB_B]
  • current date - 26 minutes, [TAG_A]
  • current date - 26 minutes, [TAG_A, TAB_B]

How can I do this and parametrize the expected results (number of items returned for each of the combinations mentioned above)?

0 Answers
Related