diff --git a/tests/unit/test_matchers.py b/tests/unit/test_matchers.py index 1640d42..5ddb583 100644 --- a/tests/unit/test_matchers.py +++ b/tests/unit/test_matchers.py @@ -196,3 +196,37 @@ def test_get_assertion_message_with_details(): "----------------------------------------\n".format(assertion_msg) ) assert matchers.get_assertion_message(assertion_msg) == expected + + +@pytest.mark.parametrize( + "r1, r2, expected_successes, expected_failures", + [ + ( + request.Request("GET", "http://host.com/p?a=b", "", {}), + request.Request("GET", "http://host.com/p?a=b", "", {}), + ["method", "path"], + [], + ), + ( + request.Request("GET", "http://host.com/p?a=b", "", {}), + request.Request("POST", "http://host.com/p?a=b", "", {}), + ["path"], + ["method"], + ), + ( + request.Request("GET", "http://host.com/p?a=b", "", {}), + request.Request("POST", "http://host.com/path?a=b", "", {}), + [], + ["method", "path"], + ), + ], +) +def test_get_matchers_results(r1, r2, expected_successes, expected_failures): + successes, failures = matchers.get_matchers_results( + r1, r2, [matchers.method, matchers.path] + ) + assert successes == expected_successes + assert len(failures) == len(expected_failures) + for i, expected_failure in enumerate(expected_failures): + assert failures[i][0] == expected_failure + assert failures[i][1] is not None diff --git a/vcr/matchers.py b/vcr/matchers.py index 5885058..ede73fb 100644 --- a/vcr/matchers.py +++ b/vcr/matchers.py @@ -118,6 +118,24 @@ def _evaluate_matcher(matcher_function, *args): return match, assertion_message +def get_matchers_results(r1, r2, matchers): + """ + Get the comparison results of two requests as two list. + The first returned list represents the matchers names that passed. + The second list is the failed matchers as a string with failed assertion details if any. + """ + matches_success, matches_fails = [], [] + for m in matchers: + matcher_name = m.__name__ + match, assertion_message = _evaluate_matcher(m, r1, r2) + if match: + matches_success.append(matcher_name) + else: + assertion_message = get_assertion_message(assertion_message) + matches_fails.append((matcher_name, assertion_message)) + return matches_success, matches_fails + + def get_assertion_message(assertion_details, **format_options): """ Get a detailed message about the failing matcher.