Different @patch behavior between Python 2.7 and 3.6 (using mock)











up vote
2
down vote

favorite












@patch does not seem to produce the same behavior under 2.7 and 3.6.



Here is my project structure:



project/
foo.py
bar.py
lol.py
tests/
test_project.py


foo.py:



class Foo:
pass


bar.py (imports Foo):



from project.foo import Foo

class Bar:
def __init__(self):
f = Foo()


lol.py (imports Bar):



from bar import Bar

class Lol:
def __init__(self):
b = Bar()


Since bar.py imports Foo using from project.foo import Foo, I am patching bar.Foo (according to where to patch docs):



test_bar.py:



from project import lol
from project import bar

@patch('bar.Foo') # Works in 3.6, fails with 2.7
def test_lol(mock_Foo):
l = lol.Lol()
mock_Foo.assert_called()


This setup runs correctly in Python 3.6 but fails in 2.7 (Foo does not get patched).



However, if I switch my setup to:



test_bar.py:



from project import lol
# from project import bar # No need to import bar anymore

@patch('project.bar.Foo') # Works in 2.7, fails with 3.6
def test_lol(mock_Foo):
l = lol.Lol()
mock_Foo.assert_called()


It works in 2.7 but fails in 3.6.



What is a recommended way to use @patch to make it produce results consistent between python versions?



Note: This problem only appears when I test lol.py. If I call bar.py from the unit test, I get consistent results using second setup @patch('cookie_test.bar.Foo') and it works in both 2.7 and 3.6.










share|improve this question









New contributor




Leonid Umanskiy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    up vote
    2
    down vote

    favorite












    @patch does not seem to produce the same behavior under 2.7 and 3.6.



    Here is my project structure:



    project/
    foo.py
    bar.py
    lol.py
    tests/
    test_project.py


    foo.py:



    class Foo:
    pass


    bar.py (imports Foo):



    from project.foo import Foo

    class Bar:
    def __init__(self):
    f = Foo()


    lol.py (imports Bar):



    from bar import Bar

    class Lol:
    def __init__(self):
    b = Bar()


    Since bar.py imports Foo using from project.foo import Foo, I am patching bar.Foo (according to where to patch docs):



    test_bar.py:



    from project import lol
    from project import bar

    @patch('bar.Foo') # Works in 3.6, fails with 2.7
    def test_lol(mock_Foo):
    l = lol.Lol()
    mock_Foo.assert_called()


    This setup runs correctly in Python 3.6 but fails in 2.7 (Foo does not get patched).



    However, if I switch my setup to:



    test_bar.py:



    from project import lol
    # from project import bar # No need to import bar anymore

    @patch('project.bar.Foo') # Works in 2.7, fails with 3.6
    def test_lol(mock_Foo):
    l = lol.Lol()
    mock_Foo.assert_called()


    It works in 2.7 but fails in 3.6.



    What is a recommended way to use @patch to make it produce results consistent between python versions?



    Note: This problem only appears when I test lol.py. If I call bar.py from the unit test, I get consistent results using second setup @patch('cookie_test.bar.Foo') and it works in both 2.7 and 3.6.










    share|improve this question









    New contributor




    Leonid Umanskiy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      @patch does not seem to produce the same behavior under 2.7 and 3.6.



      Here is my project structure:



      project/
      foo.py
      bar.py
      lol.py
      tests/
      test_project.py


      foo.py:



      class Foo:
      pass


      bar.py (imports Foo):



      from project.foo import Foo

      class Bar:
      def __init__(self):
      f = Foo()


      lol.py (imports Bar):



      from bar import Bar

      class Lol:
      def __init__(self):
      b = Bar()


      Since bar.py imports Foo using from project.foo import Foo, I am patching bar.Foo (according to where to patch docs):



      test_bar.py:



      from project import lol
      from project import bar

      @patch('bar.Foo') # Works in 3.6, fails with 2.7
      def test_lol(mock_Foo):
      l = lol.Lol()
      mock_Foo.assert_called()


      This setup runs correctly in Python 3.6 but fails in 2.7 (Foo does not get patched).



      However, if I switch my setup to:



      test_bar.py:



      from project import lol
      # from project import bar # No need to import bar anymore

      @patch('project.bar.Foo') # Works in 2.7, fails with 3.6
      def test_lol(mock_Foo):
      l = lol.Lol()
      mock_Foo.assert_called()


      It works in 2.7 but fails in 3.6.



      What is a recommended way to use @patch to make it produce results consistent between python versions?



      Note: This problem only appears when I test lol.py. If I call bar.py from the unit test, I get consistent results using second setup @patch('cookie_test.bar.Foo') and it works in both 2.7 and 3.6.










      share|improve this question









      New contributor




      Leonid Umanskiy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      @patch does not seem to produce the same behavior under 2.7 and 3.6.



      Here is my project structure:



      project/
      foo.py
      bar.py
      lol.py
      tests/
      test_project.py


      foo.py:



      class Foo:
      pass


      bar.py (imports Foo):



      from project.foo import Foo

      class Bar:
      def __init__(self):
      f = Foo()


      lol.py (imports Bar):



      from bar import Bar

      class Lol:
      def __init__(self):
      b = Bar()


      Since bar.py imports Foo using from project.foo import Foo, I am patching bar.Foo (according to where to patch docs):



      test_bar.py:



      from project import lol
      from project import bar

      @patch('bar.Foo') # Works in 3.6, fails with 2.7
      def test_lol(mock_Foo):
      l = lol.Lol()
      mock_Foo.assert_called()


      This setup runs correctly in Python 3.6 but fails in 2.7 (Foo does not get patched).



      However, if I switch my setup to:



      test_bar.py:



      from project import lol
      # from project import bar # No need to import bar anymore

      @patch('project.bar.Foo') # Works in 2.7, fails with 3.6
      def test_lol(mock_Foo):
      l = lol.Lol()
      mock_Foo.assert_called()


      It works in 2.7 but fails in 3.6.



      What is a recommended way to use @patch to make it produce results consistent between python versions?



      Note: This problem only appears when I test lol.py. If I call bar.py from the unit test, I get consistent results using second setup @patch('cookie_test.bar.Foo') and it works in both 2.7 and 3.6.







      python python-3.x python-2.7 mocking patch






      share|improve this question









      New contributor




      Leonid Umanskiy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      Leonid Umanskiy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited Nov 21 at 1:48





















      New contributor




      Leonid Umanskiy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked Nov 21 at 1:36









      Leonid Umanskiy

      112




      112




      New contributor




      Leonid Umanskiy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Leonid Umanskiy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Leonid Umanskiy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote













          I cannot replicate the difference using 2.7 vs 3.6 upon adding __init__.py files to your project directory, and changing the import of Bar in lol.py:



          from project.bar import Bar


          In either case, you should not need to import bar in your test - mock is taking care of finding bar by parsing the string passed to the mock decorator.



          I suspect the error you're seeing is due to the fact that Python 3 uses absolute imports (https://www.python.org/dev/peps/pep-0328/)






          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
            });


            }
            });






            Leonid Umanskiy is a new contributor. Be nice, and check out our Code of Conduct.










             

            draft saved


            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53404113%2fdifferent-patch-behavior-between-python-2-7-and-3-6-using-mock%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













            I cannot replicate the difference using 2.7 vs 3.6 upon adding __init__.py files to your project directory, and changing the import of Bar in lol.py:



            from project.bar import Bar


            In either case, you should not need to import bar in your test - mock is taking care of finding bar by parsing the string passed to the mock decorator.



            I suspect the error you're seeing is due to the fact that Python 3 uses absolute imports (https://www.python.org/dev/peps/pep-0328/)






            share|improve this answer



























              up vote
              1
              down vote













              I cannot replicate the difference using 2.7 vs 3.6 upon adding __init__.py files to your project directory, and changing the import of Bar in lol.py:



              from project.bar import Bar


              In either case, you should not need to import bar in your test - mock is taking care of finding bar by parsing the string passed to the mock decorator.



              I suspect the error you're seeing is due to the fact that Python 3 uses absolute imports (https://www.python.org/dev/peps/pep-0328/)






              share|improve this answer

























                up vote
                1
                down vote










                up vote
                1
                down vote









                I cannot replicate the difference using 2.7 vs 3.6 upon adding __init__.py files to your project directory, and changing the import of Bar in lol.py:



                from project.bar import Bar


                In either case, you should not need to import bar in your test - mock is taking care of finding bar by parsing the string passed to the mock decorator.



                I suspect the error you're seeing is due to the fact that Python 3 uses absolute imports (https://www.python.org/dev/peps/pep-0328/)






                share|improve this answer














                I cannot replicate the difference using 2.7 vs 3.6 upon adding __init__.py files to your project directory, and changing the import of Bar in lol.py:



                from project.bar import Bar


                In either case, you should not need to import bar in your test - mock is taking care of finding bar by parsing the string passed to the mock decorator.



                I suspect the error you're seeing is due to the fact that Python 3 uses absolute imports (https://www.python.org/dev/peps/pep-0328/)







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 21 at 2:26

























                answered Nov 21 at 2:20









                Wes Doyle

                6051619




                6051619






















                    Leonid Umanskiy is a new contributor. Be nice, and check out our Code of Conduct.










                     

                    draft saved


                    draft discarded


















                    Leonid Umanskiy is a new contributor. Be nice, and check out our Code of Conduct.













                    Leonid Umanskiy is a new contributor. Be nice, and check out our Code of Conduct.












                    Leonid Umanskiy is a new contributor. Be nice, and check out our Code of Conduct.















                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53404113%2fdifferent-patch-behavior-between-python-2-7-and-3-6-using-mock%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

                    Berounka

                    Sphinx de Gizeh

                    Different font size/position of beamer's navigation symbols template's content depending on regular/plain...