Check if all key-value pairs are present in dictionary (pytest)

Question:

I am trying to write a test to check if all key-value pairs from the expected result are present in the actual result

import pytest


def common_pairs(dict1, dict2):
    return {key: dict1[key] for key in dict1 if key in dict2 and dict1[key] == dict2[key]}


@pytest.mark.parametrize(
    "input_data,expected", [({"a": 1, "b": 2, "c": 3}, {"a": 1, "b": 2}),({"a": 5}, {})]
)
def test_output(input_data, expected):
    assert common_pairs(input_data, expected) == expected

Does my test make sense? Is there a more conventional way to test such cases?

Asked By: Nana

||

Answers:

I mean the most natural way to do this imo would be

for key,val in dict1.items():
    assert val == dict2[key]
Answered By: yagod
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.