using mocker to patch with pytest











up vote
0
down vote

favorite












I installed pytest-mock and am trying to try using mocker to function as patch does, but I get "Type Error: Need a valid target to patch. You supplied 'return a + b'"



   # test_capitalize.py
import time


def sum( a, b):
time.sleep(10)
return a + b

def test_sum(mocker):
mocker.patch('return a + b');
assertEqual(sum(2,3), 9)









share|improve this question
























  • You can't patch a string. I assume you want to patch the sum function? mocker.patch(__name__ + '.sum', return_value=9) or, if you want to patch with a custom function, use side_effect: mocker.patch(__name__ + '.sum', side_effect=lambda a, b: a - b) etc.
    – hoefling
    Nov 22 at 16:47















up vote
0
down vote

favorite












I installed pytest-mock and am trying to try using mocker to function as patch does, but I get "Type Error: Need a valid target to patch. You supplied 'return a + b'"



   # test_capitalize.py
import time


def sum( a, b):
time.sleep(10)
return a + b

def test_sum(mocker):
mocker.patch('return a + b');
assertEqual(sum(2,3), 9)









share|improve this question
























  • You can't patch a string. I assume you want to patch the sum function? mocker.patch(__name__ + '.sum', return_value=9) or, if you want to patch with a custom function, use side_effect: mocker.patch(__name__ + '.sum', side_effect=lambda a, b: a - b) etc.
    – hoefling
    Nov 22 at 16:47













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I installed pytest-mock and am trying to try using mocker to function as patch does, but I get "Type Error: Need a valid target to patch. You supplied 'return a + b'"



   # test_capitalize.py
import time


def sum( a, b):
time.sleep(10)
return a + b

def test_sum(mocker):
mocker.patch('return a + b');
assertEqual(sum(2,3), 9)









share|improve this question















I installed pytest-mock and am trying to try using mocker to function as patch does, but I get "Type Error: Need a valid target to patch. You supplied 'return a + b'"



   # test_capitalize.py
import time


def sum( a, b):
time.sleep(10)
return a + b

def test_sum(mocker):
mocker.patch('return a + b');
assertEqual(sum(2,3), 9)






python mocking pytest






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 at 15:54









seron

1,93632036




1,93632036










asked Nov 22 at 16:25









Kevin

5918




5918












  • You can't patch a string. I assume you want to patch the sum function? mocker.patch(__name__ + '.sum', return_value=9) or, if you want to patch with a custom function, use side_effect: mocker.patch(__name__ + '.sum', side_effect=lambda a, b: a - b) etc.
    – hoefling
    Nov 22 at 16:47


















  • You can't patch a string. I assume you want to patch the sum function? mocker.patch(__name__ + '.sum', return_value=9) or, if you want to patch with a custom function, use side_effect: mocker.patch(__name__ + '.sum', side_effect=lambda a, b: a - b) etc.
    – hoefling
    Nov 22 at 16:47
















You can't patch a string. I assume you want to patch the sum function? mocker.patch(__name__ + '.sum', return_value=9) or, if you want to patch with a custom function, use side_effect: mocker.patch(__name__ + '.sum', side_effect=lambda a, b: a - b) etc.
– hoefling
Nov 22 at 16:47




You can't patch a string. I assume you want to patch the sum function? mocker.patch(__name__ + '.sum', return_value=9) or, if you want to patch with a custom function, use side_effect: mocker.patch(__name__ + '.sum', side_effect=lambda a, b: a - b) etc.
– hoefling
Nov 22 at 16:47












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










patch requires a path to the function being patched. You could do something like this:



import pytest


def sum(a, b):
return a + b


def test_sum1(mocker):
mocker.patch(__name__ + ".sum", return_value=9)
assert sum(2, 3) == 9


def test_sum2(mocker):
def crazy_sum(a, b):
return b + b

mocker.patch(__name__ + ".sum", side_effect=crazy_sum)
assert sum(2, 3) == 6


Result:



$ pytest -v patch_test.py
============= test session starts ==============
platform cygwin -- Python 3.6.4, pytest-3.10.1, py-1.7.0, pluggy-0.8.0 -- /usr/bin/python3
cachedir: .pytest_cache
rootdir: /home/xyz/temp, inifile:
plugins: mock-1.10.0, cov-2.6.0
collected 2 items

patch_test.py::test_sum1 PASSED [ 50%]
patch_test.py::test_sum2 PASSED [100%]

=========== 2 passed in 0.02 seconds ===========





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',
    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%2f53434986%2fusing-mocker-to-patch-with-pytest%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    patch requires a path to the function being patched. You could do something like this:



    import pytest


    def sum(a, b):
    return a + b


    def test_sum1(mocker):
    mocker.patch(__name__ + ".sum", return_value=9)
    assert sum(2, 3) == 9


    def test_sum2(mocker):
    def crazy_sum(a, b):
    return b + b

    mocker.patch(__name__ + ".sum", side_effect=crazy_sum)
    assert sum(2, 3) == 6


    Result:



    $ pytest -v patch_test.py
    ============= test session starts ==============
    platform cygwin -- Python 3.6.4, pytest-3.10.1, py-1.7.0, pluggy-0.8.0 -- /usr/bin/python3
    cachedir: .pytest_cache
    rootdir: /home/xyz/temp, inifile:
    plugins: mock-1.10.0, cov-2.6.0
    collected 2 items

    patch_test.py::test_sum1 PASSED [ 50%]
    patch_test.py::test_sum2 PASSED [100%]

    =========== 2 passed in 0.02 seconds ===========





    share|improve this answer



























      up vote
      1
      down vote



      accepted










      patch requires a path to the function being patched. You could do something like this:



      import pytest


      def sum(a, b):
      return a + b


      def test_sum1(mocker):
      mocker.patch(__name__ + ".sum", return_value=9)
      assert sum(2, 3) == 9


      def test_sum2(mocker):
      def crazy_sum(a, b):
      return b + b

      mocker.patch(__name__ + ".sum", side_effect=crazy_sum)
      assert sum(2, 3) == 6


      Result:



      $ pytest -v patch_test.py
      ============= test session starts ==============
      platform cygwin -- Python 3.6.4, pytest-3.10.1, py-1.7.0, pluggy-0.8.0 -- /usr/bin/python3
      cachedir: .pytest_cache
      rootdir: /home/xyz/temp, inifile:
      plugins: mock-1.10.0, cov-2.6.0
      collected 2 items

      patch_test.py::test_sum1 PASSED [ 50%]
      patch_test.py::test_sum2 PASSED [100%]

      =========== 2 passed in 0.02 seconds ===========





      share|improve this answer

























        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted






        patch requires a path to the function being patched. You could do something like this:



        import pytest


        def sum(a, b):
        return a + b


        def test_sum1(mocker):
        mocker.patch(__name__ + ".sum", return_value=9)
        assert sum(2, 3) == 9


        def test_sum2(mocker):
        def crazy_sum(a, b):
        return b + b

        mocker.patch(__name__ + ".sum", side_effect=crazy_sum)
        assert sum(2, 3) == 6


        Result:



        $ pytest -v patch_test.py
        ============= test session starts ==============
        platform cygwin -- Python 3.6.4, pytest-3.10.1, py-1.7.0, pluggy-0.8.0 -- /usr/bin/python3
        cachedir: .pytest_cache
        rootdir: /home/xyz/temp, inifile:
        plugins: mock-1.10.0, cov-2.6.0
        collected 2 items

        patch_test.py::test_sum1 PASSED [ 50%]
        patch_test.py::test_sum2 PASSED [100%]

        =========== 2 passed in 0.02 seconds ===========





        share|improve this answer














        patch requires a path to the function being patched. You could do something like this:



        import pytest


        def sum(a, b):
        return a + b


        def test_sum1(mocker):
        mocker.patch(__name__ + ".sum", return_value=9)
        assert sum(2, 3) == 9


        def test_sum2(mocker):
        def crazy_sum(a, b):
        return b + b

        mocker.patch(__name__ + ".sum", side_effect=crazy_sum)
        assert sum(2, 3) == 6


        Result:



        $ pytest -v patch_test.py
        ============= test session starts ==============
        platform cygwin -- Python 3.6.4, pytest-3.10.1, py-1.7.0, pluggy-0.8.0 -- /usr/bin/python3
        cachedir: .pytest_cache
        rootdir: /home/xyz/temp, inifile:
        plugins: mock-1.10.0, cov-2.6.0
        collected 2 items

        patch_test.py::test_sum1 PASSED [ 50%]
        patch_test.py::test_sum2 PASSED [100%]

        =========== 2 passed in 0.02 seconds ===========






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 23 at 14:22

























        answered Nov 23 at 14:16









        seron

        1,93632036




        1,93632036






























            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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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%2f53434986%2fusing-mocker-to-patch-with-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

            What visual should I use to simply compare current year value vs last year in Power BI desktop

            How to ignore python UserWarning in pytest?

            Alexandru Averescu