Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add additional kwargs to allow additional_matcher function to take in additional parameters #207

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

xlanor
Copy link

@xlanor xlanor commented Jun 28, 2022

This can be useful when looping over a testing piece of code, just changing the test cases that is provided to it.

Would like to get approval for CI please as local tox is hanging for me.

@jamielennox
Copy link
Owner

Hey, sorry about CI, there was a discussion about the way a lock worked that took a long time to get fixed. It's merged now.

I'm not sure i see your use case. Why not just build a function that returns a function with the kwargs embedded?

@jamielennox
Copy link
Owner

your test case just rewritten a little to be inline in a script.

import requests
import requests_mock


with requests_mock.mock() as m:
    url = 'http://www.example.com'
    good_text = 'success'

    def cb_factory(match_text):
        def cb(req):
            return match_text in req.text

        return cb

    m.post(url, additional_matcher=cb_factory("hello"), text=good_text)

    assert good_text == requests.post(url, data='hello world').text

    try:
        requests.post(url, data="goodbye world")
    except requests_mock.NoMockAddress as e:
        pass
    else:
        raise Exception("should have raised")

@xlanor
Copy link
Author

xlanor commented Aug 30, 2022

Hey, sorry about CI, there was a discussion about the way a lock worked that took a long time to get fixed. It's merged now.

I'm not sure i see your use case. Why not just build a function that returns a function with the kwargs embedded?

Sorry - do you have an example of what you mean?

Primarily my use case of this was to write tests in a table driven style using dataclasses, which allows me to define the expected payload at each test case and pass it on to request_mock

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants