mock botocore.vendored requests with requests_mock and pytest












0















I am trying to mock a get request with requests_mock, but it doesn't seem to get it right.



My function calling a third-party API defined in a file lookup.py:



from botocore.vendored import requests

def get_data():
url = 'https://abc.something.com/datapackage'
url_params={
'v': 2,
'auth_apikey':'xyz'
}
resp = requests.get(url, params=url_params)
return resp.json()


I am using py.test to run my tests and in my test file. I have a fixture:



  import requests_mock
import requests, pytest
from lookup import get_data

@pytest.fixture
def req_mock(request):
m = requests_mock.Mocker()
m.start()
request.addfinalizer(m.stop)
return m

def test_api_gets_data(req_mock):
sample={
'key1':123
}
lookup_url = 'https://abc.something.com/datapackage'
query_params = {
'v': 2,
'auth_apikey':'xyz'
}
req_mock.get(lookup_url, json=sample)
resp = get_data()


Apparently, requests_mock isn't able to use the same session as the requests in the get function, so it isn't getting mocked.



Is there a better way to do this?



I'm using Python 3.6, requests 2.18, requests-mock 1.52 and pytest 3.0.7.










share|improve this question

























  • this was working for me

    – user1329187
    Nov 14 '18 at 20:48











  • Sorry I just realised while creating this post I used the wrong import. I am trying to use requests from botocore, which is AWS package and core of boto3

    – Mateen-Hussain
    Nov 15 '18 at 8:58
















0















I am trying to mock a get request with requests_mock, but it doesn't seem to get it right.



My function calling a third-party API defined in a file lookup.py:



from botocore.vendored import requests

def get_data():
url = 'https://abc.something.com/datapackage'
url_params={
'v': 2,
'auth_apikey':'xyz'
}
resp = requests.get(url, params=url_params)
return resp.json()


I am using py.test to run my tests and in my test file. I have a fixture:



  import requests_mock
import requests, pytest
from lookup import get_data

@pytest.fixture
def req_mock(request):
m = requests_mock.Mocker()
m.start()
request.addfinalizer(m.stop)
return m

def test_api_gets_data(req_mock):
sample={
'key1':123
}
lookup_url = 'https://abc.something.com/datapackage'
query_params = {
'v': 2,
'auth_apikey':'xyz'
}
req_mock.get(lookup_url, json=sample)
resp = get_data()


Apparently, requests_mock isn't able to use the same session as the requests in the get function, so it isn't getting mocked.



Is there a better way to do this?



I'm using Python 3.6, requests 2.18, requests-mock 1.52 and pytest 3.0.7.










share|improve this question

























  • this was working for me

    – user1329187
    Nov 14 '18 at 20:48











  • Sorry I just realised while creating this post I used the wrong import. I am trying to use requests from botocore, which is AWS package and core of boto3

    – Mateen-Hussain
    Nov 15 '18 at 8:58














0












0








0








I am trying to mock a get request with requests_mock, but it doesn't seem to get it right.



My function calling a third-party API defined in a file lookup.py:



from botocore.vendored import requests

def get_data():
url = 'https://abc.something.com/datapackage'
url_params={
'v': 2,
'auth_apikey':'xyz'
}
resp = requests.get(url, params=url_params)
return resp.json()


I am using py.test to run my tests and in my test file. I have a fixture:



  import requests_mock
import requests, pytest
from lookup import get_data

@pytest.fixture
def req_mock(request):
m = requests_mock.Mocker()
m.start()
request.addfinalizer(m.stop)
return m

def test_api_gets_data(req_mock):
sample={
'key1':123
}
lookup_url = 'https://abc.something.com/datapackage'
query_params = {
'v': 2,
'auth_apikey':'xyz'
}
req_mock.get(lookup_url, json=sample)
resp = get_data()


Apparently, requests_mock isn't able to use the same session as the requests in the get function, so it isn't getting mocked.



Is there a better way to do this?



I'm using Python 3.6, requests 2.18, requests-mock 1.52 and pytest 3.0.7.










share|improve this question
















I am trying to mock a get request with requests_mock, but it doesn't seem to get it right.



My function calling a third-party API defined in a file lookup.py:



from botocore.vendored import requests

def get_data():
url = 'https://abc.something.com/datapackage'
url_params={
'v': 2,
'auth_apikey':'xyz'
}
resp = requests.get(url, params=url_params)
return resp.json()


I am using py.test to run my tests and in my test file. I have a fixture:



  import requests_mock
import requests, pytest
from lookup import get_data

@pytest.fixture
def req_mock(request):
m = requests_mock.Mocker()
m.start()
request.addfinalizer(m.stop)
return m

def test_api_gets_data(req_mock):
sample={
'key1':123
}
lookup_url = 'https://abc.something.com/datapackage'
query_params = {
'v': 2,
'auth_apikey':'xyz'
}
req_mock.get(lookup_url, json=sample)
resp = get_data()


Apparently, requests_mock isn't able to use the same session as the requests in the get function, so it isn't getting mocked.



Is there a better way to do this?



I'm using Python 3.6, requests 2.18, requests-mock 1.52 and pytest 3.0.7.







python python-requests boto3






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 8:39







Mateen-Hussain

















asked Nov 14 '18 at 19:11









Mateen-HussainMateen-Hussain

192114




192114













  • this was working for me

    – user1329187
    Nov 14 '18 at 20:48











  • Sorry I just realised while creating this post I used the wrong import. I am trying to use requests from botocore, which is AWS package and core of boto3

    – Mateen-Hussain
    Nov 15 '18 at 8:58



















  • this was working for me

    – user1329187
    Nov 14 '18 at 20:48











  • Sorry I just realised while creating this post I used the wrong import. I am trying to use requests from botocore, which is AWS package and core of boto3

    – Mateen-Hussain
    Nov 15 '18 at 8:58

















this was working for me

– user1329187
Nov 14 '18 at 20:48





this was working for me

– user1329187
Nov 14 '18 at 20:48













Sorry I just realised while creating this post I used the wrong import. I am trying to use requests from botocore, which is AWS package and core of boto3

– Mateen-Hussain
Nov 15 '18 at 8:58





Sorry I just realised while creating this post I used the wrong import. I am trying to use requests from botocore, which is AWS package and core of boto3

– Mateen-Hussain
Nov 15 '18 at 8:58












2 Answers
2






active

oldest

votes


















0














at lookup.py files, lookup_url will raise NameError cause name lookup_url not defined.



use url and don't forget to change params=url_params, so the code will be something like this:



resp = requests.get(url, params=url_params)






share|improve this answer
























  • thanks for the correction. I hadnt properly edited the code.

    – Mateen-Hussain
    Nov 15 '18 at 8:24



















0














Apparently you can't mock from botocore.vendored import requests with requests_mock.



Instead use unittest.mock to mock the response.



  from unittest import mock

class MockResponse:
def __init__(self, status_code, json_data=None):
self.json_data = json_data
self.status_code = status_code

def json(self):
return self.json_data

def raise_for_status(self):
if self.status_code >= 500:
raise Exception

item_not_found = {
"Response": {
"StatusCode": "ItemNotFound",
}
}

item_not_found_resp = MockResponse(200, item_not_found)


@mock.patch('botocore.vendored.requests.get', return_value=item_not_found_resp)
def test_api_returns_not_found_when_third_party_api_returns_item_not_found(mc):
resp = get(e1, c)
exp_resp = {
"statusCode": 404,
"body": json.dumps({
'error': 'no item found'
})
}
request_url = mc.call_args[0][0]
request_params = mc.call_args[1]['params']
assert lookup_url == request_url
assert query_params == request_params
assert exp_resp == resp





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53307234%2fmock-botocore-vendored-requests-with-requests-mock-and-pytest%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    at lookup.py files, lookup_url will raise NameError cause name lookup_url not defined.



    use url and don't forget to change params=url_params, so the code will be something like this:



    resp = requests.get(url, params=url_params)






    share|improve this answer
























    • thanks for the correction. I hadnt properly edited the code.

      – Mateen-Hussain
      Nov 15 '18 at 8:24
















    0














    at lookup.py files, lookup_url will raise NameError cause name lookup_url not defined.



    use url and don't forget to change params=url_params, so the code will be something like this:



    resp = requests.get(url, params=url_params)






    share|improve this answer
























    • thanks for the correction. I hadnt properly edited the code.

      – Mateen-Hussain
      Nov 15 '18 at 8:24














    0












    0








    0







    at lookup.py files, lookup_url will raise NameError cause name lookup_url not defined.



    use url and don't forget to change params=url_params, so the code will be something like this:



    resp = requests.get(url, params=url_params)






    share|improve this answer













    at lookup.py files, lookup_url will raise NameError cause name lookup_url not defined.



    use url and don't forget to change params=url_params, so the code will be something like this:



    resp = requests.get(url, params=url_params)







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 14 '18 at 23:50









    didadadida93didadadida93

    113




    113













    • thanks for the correction. I hadnt properly edited the code.

      – Mateen-Hussain
      Nov 15 '18 at 8:24



















    • thanks for the correction. I hadnt properly edited the code.

      – Mateen-Hussain
      Nov 15 '18 at 8:24

















    thanks for the correction. I hadnt properly edited the code.

    – Mateen-Hussain
    Nov 15 '18 at 8:24





    thanks for the correction. I hadnt properly edited the code.

    – Mateen-Hussain
    Nov 15 '18 at 8:24













    0














    Apparently you can't mock from botocore.vendored import requests with requests_mock.



    Instead use unittest.mock to mock the response.



      from unittest import mock

    class MockResponse:
    def __init__(self, status_code, json_data=None):
    self.json_data = json_data
    self.status_code = status_code

    def json(self):
    return self.json_data

    def raise_for_status(self):
    if self.status_code >= 500:
    raise Exception

    item_not_found = {
    "Response": {
    "StatusCode": "ItemNotFound",
    }
    }

    item_not_found_resp = MockResponse(200, item_not_found)


    @mock.patch('botocore.vendored.requests.get', return_value=item_not_found_resp)
    def test_api_returns_not_found_when_third_party_api_returns_item_not_found(mc):
    resp = get(e1, c)
    exp_resp = {
    "statusCode": 404,
    "body": json.dumps({
    'error': 'no item found'
    })
    }
    request_url = mc.call_args[0][0]
    request_params = mc.call_args[1]['params']
    assert lookup_url == request_url
    assert query_params == request_params
    assert exp_resp == resp





    share|improve this answer




























      0














      Apparently you can't mock from botocore.vendored import requests with requests_mock.



      Instead use unittest.mock to mock the response.



        from unittest import mock

      class MockResponse:
      def __init__(self, status_code, json_data=None):
      self.json_data = json_data
      self.status_code = status_code

      def json(self):
      return self.json_data

      def raise_for_status(self):
      if self.status_code >= 500:
      raise Exception

      item_not_found = {
      "Response": {
      "StatusCode": "ItemNotFound",
      }
      }

      item_not_found_resp = MockResponse(200, item_not_found)


      @mock.patch('botocore.vendored.requests.get', return_value=item_not_found_resp)
      def test_api_returns_not_found_when_third_party_api_returns_item_not_found(mc):
      resp = get(e1, c)
      exp_resp = {
      "statusCode": 404,
      "body": json.dumps({
      'error': 'no item found'
      })
      }
      request_url = mc.call_args[0][0]
      request_params = mc.call_args[1]['params']
      assert lookup_url == request_url
      assert query_params == request_params
      assert exp_resp == resp





      share|improve this answer


























        0












        0








        0







        Apparently you can't mock from botocore.vendored import requests with requests_mock.



        Instead use unittest.mock to mock the response.



          from unittest import mock

        class MockResponse:
        def __init__(self, status_code, json_data=None):
        self.json_data = json_data
        self.status_code = status_code

        def json(self):
        return self.json_data

        def raise_for_status(self):
        if self.status_code >= 500:
        raise Exception

        item_not_found = {
        "Response": {
        "StatusCode": "ItemNotFound",
        }
        }

        item_not_found_resp = MockResponse(200, item_not_found)


        @mock.patch('botocore.vendored.requests.get', return_value=item_not_found_resp)
        def test_api_returns_not_found_when_third_party_api_returns_item_not_found(mc):
        resp = get(e1, c)
        exp_resp = {
        "statusCode": 404,
        "body": json.dumps({
        'error': 'no item found'
        })
        }
        request_url = mc.call_args[0][0]
        request_params = mc.call_args[1]['params']
        assert lookup_url == request_url
        assert query_params == request_params
        assert exp_resp == resp





        share|improve this answer













        Apparently you can't mock from botocore.vendored import requests with requests_mock.



        Instead use unittest.mock to mock the response.



          from unittest import mock

        class MockResponse:
        def __init__(self, status_code, json_data=None):
        self.json_data = json_data
        self.status_code = status_code

        def json(self):
        return self.json_data

        def raise_for_status(self):
        if self.status_code >= 500:
        raise Exception

        item_not_found = {
        "Response": {
        "StatusCode": "ItemNotFound",
        }
        }

        item_not_found_resp = MockResponse(200, item_not_found)


        @mock.patch('botocore.vendored.requests.get', return_value=item_not_found_resp)
        def test_api_returns_not_found_when_third_party_api_returns_item_not_found(mc):
        resp = get(e1, c)
        exp_resp = {
        "statusCode": 404,
        "body": json.dumps({
        'error': 'no item found'
        })
        }
        request_url = mc.call_args[0][0]
        request_params = mc.call_args[1]['params']
        assert lookup_url == request_url
        assert query_params == request_params
        assert exp_resp == resp






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 16 '18 at 8:48









        Mateen-HussainMateen-Hussain

        192114




        192114






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53307234%2fmock-botocore-vendored-requests-with-requests-mock-and-pytest%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Bressuire

            Vorschmack

            Quarantine