'Unittest Check behavior of TrackedTask method with requests

I have a class Foo(TrackedTask) that receive and argument (data) to make decisions of what behavior must do.

task = Foo()
task.run(data)

In my Class, for example:

class Foo(TrackedTask):
    def method_1(self):
        response = self.method_2(data)
        ...

    def method_2(self):
        if data.name == "a":
            response = resquest.post(url=A, ...)
        else:
            response = request.post(url=B, ...)

One way is using responses by GetSentry, and I use for other reasons, but this way will send requests. I must not call this url for testing.

@responses.activate
def test_foo(self):
    responses.add(
        responses.POST,
        Foo.url,
        body=json.dumps(response),
        status=200,
        content_type="application/json",
    )
    task = Foo()
    result = task.run(data)

Other way maybe is using Mock by @patch.

My doubt: How can I test with unittest or pytest Foo.method_2 passing an argument (one or more Python Fixtures, for example). If send data_a, will go to url=A or argument data_b go to url=B without calling the endpoint itself.

Best regards.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source