Search code examples
pythonvcr

Can we register multiple custom request matchers while using VCR.py (cassette recording for python)?


If the answer is yes, then an example on how to use them will be great.

Am referring to the below part of documentation: http://vcrpy.readthedocs.io/en/latest/advanced.html#register-your-own-request-matcher


Solution

  • I found the answer myself and the answer is Yes, it is possible to have multiple custom matchers when using VCR.py It's actually similar to how the VCRPY docs show using a single custom matcher.

    Below is a general structure on how it can be done:

    def vcr_matcher_1(r1, r2):
        # logic for matching r1 and r2 requests
    
    def vcr_matcher_2(r1, r2):
        # logic for matching r1 and r2 requests
    

    In your recording code, you can use something like:

    custom_vcr = vcr.VCR()
    custom_vcr.register_matcher('vcr_matcher_1', vcr_matcher_1)
    custom_vcr.register_matcher('vcr_matcher_2', vcr_matcher_2)
    with custom_vcr.use_cassette('cassette_name.yml',
        match_on = ['method', 'scheme', 'host', 'port', 'vcr_matcher_1', 'vcr_matcher_2']):
    

    The above example also shows you can mix your custom matchers with default provided matchers by VCR.py

    Ref:
    http://vcrpy.readthedocs.io/en/latest/advanced.html#register-your-own-request-matcher