Use function for mocked class' method return value












0














I am new to mock and and trying to work with side_effects.



I am trying to set the return value of a method of a mocked class based on the argument said method was called with. In the below code, I am trying to set the return value of some_function when having mocked MyClass.



# application.py
from my_module.my_submodule import MyClass

def my_function(var1):
instance = MyClass()

instance.some_function(var1)


and my testing file



# test_application.py
import mock
import application

def test_my_function():
with mock.patch('application.MyClass') as MockClass:
MockClass.return_value.my_function.return_value = some_return

application.my_function(var1)


This works such that some_function now returns some_return, but I would like to have a function in place of some_return which takes the argument var1 that the function is called with.



The problem is that I don't know how to define the mock to anticipate the calling argument of some_function.



I have experimented with what is discussed in this post changing the side effect of a mock object's method created with patch, but I can't for the life of me figure out how to format it.



I have tried something like this



# test_application.py
import mock
import application

def test_my_function():
with mock.patch('application.MyClass') as MockClass:
MockClass.return_value.my_function.return_value = some_return

# Breaking very long line, in my code it's actually one line.
MockDataPrep.return_value.extract_preprocessed_citizen_data.
side_effect =
mock.MagicMock(side_effect=my_side_effect)

application.my_function(var1)


where the function my_side_effect looks like this:



def my_side_effect(var1):
return_val = some_manipulation_of_var1(var1)

if something:
return `abc`
else:
raise LookupError


but it doesn't seem that the my_side_effect is ever entered (tried with print statements inside of it). How would I format this?










share|improve this question



























    0














    I am new to mock and and trying to work with side_effects.



    I am trying to set the return value of a method of a mocked class based on the argument said method was called with. In the below code, I am trying to set the return value of some_function when having mocked MyClass.



    # application.py
    from my_module.my_submodule import MyClass

    def my_function(var1):
    instance = MyClass()

    instance.some_function(var1)


    and my testing file



    # test_application.py
    import mock
    import application

    def test_my_function():
    with mock.patch('application.MyClass') as MockClass:
    MockClass.return_value.my_function.return_value = some_return

    application.my_function(var1)


    This works such that some_function now returns some_return, but I would like to have a function in place of some_return which takes the argument var1 that the function is called with.



    The problem is that I don't know how to define the mock to anticipate the calling argument of some_function.



    I have experimented with what is discussed in this post changing the side effect of a mock object's method created with patch, but I can't for the life of me figure out how to format it.



    I have tried something like this



    # test_application.py
    import mock
    import application

    def test_my_function():
    with mock.patch('application.MyClass') as MockClass:
    MockClass.return_value.my_function.return_value = some_return

    # Breaking very long line, in my code it's actually one line.
    MockDataPrep.return_value.extract_preprocessed_citizen_data.
    side_effect =
    mock.MagicMock(side_effect=my_side_effect)

    application.my_function(var1)


    where the function my_side_effect looks like this:



    def my_side_effect(var1):
    return_val = some_manipulation_of_var1(var1)

    if something:
    return `abc`
    else:
    raise LookupError


    but it doesn't seem that the my_side_effect is ever entered (tried with print statements inside of it). How would I format this?










    share|improve this question

























      0












      0








      0







      I am new to mock and and trying to work with side_effects.



      I am trying to set the return value of a method of a mocked class based on the argument said method was called with. In the below code, I am trying to set the return value of some_function when having mocked MyClass.



      # application.py
      from my_module.my_submodule import MyClass

      def my_function(var1):
      instance = MyClass()

      instance.some_function(var1)


      and my testing file



      # test_application.py
      import mock
      import application

      def test_my_function():
      with mock.patch('application.MyClass') as MockClass:
      MockClass.return_value.my_function.return_value = some_return

      application.my_function(var1)


      This works such that some_function now returns some_return, but I would like to have a function in place of some_return which takes the argument var1 that the function is called with.



      The problem is that I don't know how to define the mock to anticipate the calling argument of some_function.



      I have experimented with what is discussed in this post changing the side effect of a mock object's method created with patch, but I can't for the life of me figure out how to format it.



      I have tried something like this



      # test_application.py
      import mock
      import application

      def test_my_function():
      with mock.patch('application.MyClass') as MockClass:
      MockClass.return_value.my_function.return_value = some_return

      # Breaking very long line, in my code it's actually one line.
      MockDataPrep.return_value.extract_preprocessed_citizen_data.
      side_effect =
      mock.MagicMock(side_effect=my_side_effect)

      application.my_function(var1)


      where the function my_side_effect looks like this:



      def my_side_effect(var1):
      return_val = some_manipulation_of_var1(var1)

      if something:
      return `abc`
      else:
      raise LookupError


      but it doesn't seem that the my_side_effect is ever entered (tried with print statements inside of it). How would I format this?










      share|improve this question













      I am new to mock and and trying to work with side_effects.



      I am trying to set the return value of a method of a mocked class based on the argument said method was called with. In the below code, I am trying to set the return value of some_function when having mocked MyClass.



      # application.py
      from my_module.my_submodule import MyClass

      def my_function(var1):
      instance = MyClass()

      instance.some_function(var1)


      and my testing file



      # test_application.py
      import mock
      import application

      def test_my_function():
      with mock.patch('application.MyClass') as MockClass:
      MockClass.return_value.my_function.return_value = some_return

      application.my_function(var1)


      This works such that some_function now returns some_return, but I would like to have a function in place of some_return which takes the argument var1 that the function is called with.



      The problem is that I don't know how to define the mock to anticipate the calling argument of some_function.



      I have experimented with what is discussed in this post changing the side effect of a mock object's method created with patch, but I can't for the life of me figure out how to format it.



      I have tried something like this



      # test_application.py
      import mock
      import application

      def test_my_function():
      with mock.patch('application.MyClass') as MockClass:
      MockClass.return_value.my_function.return_value = some_return

      # Breaking very long line, in my code it's actually one line.
      MockDataPrep.return_value.extract_preprocessed_citizen_data.
      side_effect =
      mock.MagicMock(side_effect=my_side_effect)

      application.my_function(var1)


      where the function my_side_effect looks like this:



      def my_side_effect(var1):
      return_val = some_manipulation_of_var1(var1)

      if something:
      return `abc`
      else:
      raise LookupError


      but it doesn't seem that the my_side_effect is ever entered (tried with print statements inside of it). How would I format this?







      python testing






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 23 '18 at 9:32









      Kaspar H

      276




      276
























          2 Answers
          2






          active

          oldest

          votes


















          1














          You are mocking wrong method. my_function is a function of application module, not a method of MyClass and hence what you want to mock is some_function instead.



          import mock
          import application


          def my_side_effect(*args, **kwargs):
          print("my_side_effect called")
          print(args, kwargs)


          def test_my_function():
          with mock.patch("application.MyClass") as MockClass:
          MockClass.return_value.some_function.side_effect = my_side_effect

          application.my_function(arg1)


          This way arg1 will be passed to my_side_effect within *args.



          Also, you may want to mock only a specific method, not the entire class.



          def test_my_function():
          with mock.patch("application.MyClass.some_function") as mock_some_function:
          mock_some_function.side_effect = my_side_effect

          application.my_function(arg1)





          share|improve this answer































            0














            It looks you turned to side_effects because you couldn't do it with a return_value.



            But you can use the wraps mock parameter. It wraps an object with a mock. You can only use existing methods in the object (although you can add more) and the returned result is the result of the execution of the real method. return_value is ignored:



            from unittest.mock import Mock

            class Mirror:
            def func(self,x):
            return x

            mirror = Mirror()
            m = Mock(wraps=mirror)
            print(m.func(20))


            If instead you want to test how a mock was called, with what args, how many times and so on, there are inspection methods in the mock objects too.






            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%2f53443935%2fuse-function-for-mocked-class-method-return-value%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









              1














              You are mocking wrong method. my_function is a function of application module, not a method of MyClass and hence what you want to mock is some_function instead.



              import mock
              import application


              def my_side_effect(*args, **kwargs):
              print("my_side_effect called")
              print(args, kwargs)


              def test_my_function():
              with mock.patch("application.MyClass") as MockClass:
              MockClass.return_value.some_function.side_effect = my_side_effect

              application.my_function(arg1)


              This way arg1 will be passed to my_side_effect within *args.



              Also, you may want to mock only a specific method, not the entire class.



              def test_my_function():
              with mock.patch("application.MyClass.some_function") as mock_some_function:
              mock_some_function.side_effect = my_side_effect

              application.my_function(arg1)





              share|improve this answer




























                1














                You are mocking wrong method. my_function is a function of application module, not a method of MyClass and hence what you want to mock is some_function instead.



                import mock
                import application


                def my_side_effect(*args, **kwargs):
                print("my_side_effect called")
                print(args, kwargs)


                def test_my_function():
                with mock.patch("application.MyClass") as MockClass:
                MockClass.return_value.some_function.side_effect = my_side_effect

                application.my_function(arg1)


                This way arg1 will be passed to my_side_effect within *args.



                Also, you may want to mock only a specific method, not the entire class.



                def test_my_function():
                with mock.patch("application.MyClass.some_function") as mock_some_function:
                mock_some_function.side_effect = my_side_effect

                application.my_function(arg1)





                share|improve this answer


























                  1












                  1








                  1






                  You are mocking wrong method. my_function is a function of application module, not a method of MyClass and hence what you want to mock is some_function instead.



                  import mock
                  import application


                  def my_side_effect(*args, **kwargs):
                  print("my_side_effect called")
                  print(args, kwargs)


                  def test_my_function():
                  with mock.patch("application.MyClass") as MockClass:
                  MockClass.return_value.some_function.side_effect = my_side_effect

                  application.my_function(arg1)


                  This way arg1 will be passed to my_side_effect within *args.



                  Also, you may want to mock only a specific method, not the entire class.



                  def test_my_function():
                  with mock.patch("application.MyClass.some_function") as mock_some_function:
                  mock_some_function.side_effect = my_side_effect

                  application.my_function(arg1)





                  share|improve this answer














                  You are mocking wrong method. my_function is a function of application module, not a method of MyClass and hence what you want to mock is some_function instead.



                  import mock
                  import application


                  def my_side_effect(*args, **kwargs):
                  print("my_side_effect called")
                  print(args, kwargs)


                  def test_my_function():
                  with mock.patch("application.MyClass") as MockClass:
                  MockClass.return_value.some_function.side_effect = my_side_effect

                  application.my_function(arg1)


                  This way arg1 will be passed to my_side_effect within *args.



                  Also, you may want to mock only a specific method, not the entire class.



                  def test_my_function():
                  with mock.patch("application.MyClass.some_function") as mock_some_function:
                  mock_some_function.side_effect = my_side_effect

                  application.my_function(arg1)






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 26 '18 at 16:29

























                  answered Nov 26 '18 at 16:19









                  Dušan Maďar

                  4,32741935




                  4,32741935

























                      0














                      It looks you turned to side_effects because you couldn't do it with a return_value.



                      But you can use the wraps mock parameter. It wraps an object with a mock. You can only use existing methods in the object (although you can add more) and the returned result is the result of the execution of the real method. return_value is ignored:



                      from unittest.mock import Mock

                      class Mirror:
                      def func(self,x):
                      return x

                      mirror = Mirror()
                      m = Mock(wraps=mirror)
                      print(m.func(20))


                      If instead you want to test how a mock was called, with what args, how many times and so on, there are inspection methods in the mock objects too.






                      share|improve this answer




























                        0














                        It looks you turned to side_effects because you couldn't do it with a return_value.



                        But you can use the wraps mock parameter. It wraps an object with a mock. You can only use existing methods in the object (although you can add more) and the returned result is the result of the execution of the real method. return_value is ignored:



                        from unittest.mock import Mock

                        class Mirror:
                        def func(self,x):
                        return x

                        mirror = Mirror()
                        m = Mock(wraps=mirror)
                        print(m.func(20))


                        If instead you want to test how a mock was called, with what args, how many times and so on, there are inspection methods in the mock objects too.






                        share|improve this answer


























                          0












                          0








                          0






                          It looks you turned to side_effects because you couldn't do it with a return_value.



                          But you can use the wraps mock parameter. It wraps an object with a mock. You can only use existing methods in the object (although you can add more) and the returned result is the result of the execution of the real method. return_value is ignored:



                          from unittest.mock import Mock

                          class Mirror:
                          def func(self,x):
                          return x

                          mirror = Mirror()
                          m = Mock(wraps=mirror)
                          print(m.func(20))


                          If instead you want to test how a mock was called, with what args, how many times and so on, there are inspection methods in the mock objects too.






                          share|improve this answer














                          It looks you turned to side_effects because you couldn't do it with a return_value.



                          But you can use the wraps mock parameter. It wraps an object with a mock. You can only use existing methods in the object (although you can add more) and the returned result is the result of the execution of the real method. return_value is ignored:



                          from unittest.mock import Mock

                          class Mirror:
                          def func(self,x):
                          return x

                          mirror = Mirror()
                          m = Mock(wraps=mirror)
                          print(m.func(20))


                          If instead you want to test how a mock was called, with what args, how many times and so on, there are inspection methods in the mock objects too.







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Nov 24 '18 at 14:35

























                          answered Nov 24 '18 at 14:29









                          progmatico

                          1,8401512




                          1,8401512






























                              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%2f53443935%2fuse-function-for-mocked-class-method-return-value%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

                              Alexandru Averescu

                              Trompette piccolo