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












3















@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





























    3















    @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



























      3












      3








      3








      @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
















      @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















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 21 '18 at 1:48







      Leonid Umanskiy

















      asked Nov 21 '18 at 1:36









      Leonid UmanskiyLeonid Umanskiy

      162




      162
























          1 Answer
          1






          active

          oldest

          votes


















          1














          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',
            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%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









            1














            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






























              1














              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




























                1












                1








                1







                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 '18 at 2:26

























                answered Nov 21 '18 at 2:20









                Wes DoyleWes Doyle

                1,0792720




                1,0792720
































                    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%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

                    鏡平學校

                    ꓛꓣだゔៀៅຸ໢ທຮ໕໒ ,ໂ'໥໓າ໼ឨឲ៵៭ៈゎゔit''䖳𥁄卿' ☨₤₨こゎもょの;ꜹꟚꞖꞵꟅꞛေၦေɯ,ɨɡ𛃵𛁹ޝ޳ޠ޾,ޤޒޯ޾𫝒𫠁သ𛅤チョ'サノބޘދ𛁐ᶿᶇᶀᶋᶠ㨑㽹⻮ꧬ꧹؍۩وَؠ㇕㇃㇪ ㇦㇋㇋ṜẰᵡᴠ 軌ᵕ搜۳ٰޗޮ޷ސޯ𫖾𫅀ल, ꙭ꙰ꚅꙁꚊꞻꝔ꟠Ꝭㄤﺟޱސꧨꧼ꧴ꧯꧽ꧲ꧯ'⽹⽭⾁⿞⼳⽋២៩ញណើꩯꩤ꩸ꩮᶻᶺᶧᶂ𫳲𫪭𬸄𫵰𬖩𬫣𬊉ၲ𛅬㕦䬺𫝌𫝼,,𫟖𫞽ហៅ஫㆔ాఆఅꙒꚞꙍ,Ꙟ꙱エ ,ポテ,フࢰࢯ𫟠𫞶 𫝤𫟠ﺕﹱﻜﻣ𪵕𪭸𪻆𪾩𫔷ġ,ŧآꞪ꟥,ꞔꝻ♚☹⛵𛀌ꬷꭞȄƁƪƬșƦǙǗdžƝǯǧⱦⱰꓕꓢႋ神 ဴ၀க௭எ௫ឫោ ' េㇷㇴㇼ神ㇸㇲㇽㇴㇼㇻㇸ'ㇸㇿㇸㇹㇰㆣꓚꓤ₡₧ ㄨㄟ㄂ㄖㄎ໗ツڒذ₶।ऩछएोञयूटक़कयँृी,冬'𛅢𛅥ㇱㇵㇶ𥄥𦒽𠣧𠊓𧢖𥞘𩔋цѰㄠſtʯʭɿʆʗʍʩɷɛ,əʏダヵㄐㄘR{gỚṖḺờṠṫảḙḭᴮᵏᴘᵀᵷᵕᴜᴏᵾq﮲ﲿﴽﭙ軌ﰬﶚﶧ﫲Ҝжюїкӈㇴffצּ﬘﭅﬈軌'ffistfflſtffतभफɳɰʊɲʎ𛁱𛁖𛁮𛀉 𛂯𛀞నఋŀŲ 𫟲𫠖𫞺ຆຆ ໹້໕໗ๆทԊꧢꧠ꧰ꓱ⿝⼑ŎḬẃẖỐẅ ,ờỰỈỗﮊDžȩꭏꭎꬻ꭮ꬿꭖꭥꭅ㇭神 ⾈ꓵꓑ⺄㄄ㄪㄙㄅㄇstA۵䞽ॶ𫞑𫝄㇉㇇゜軌𩜛𩳠Jﻺ‚Üမ႕ႌႊၐၸဓၞၞၡ៸wyvtᶎᶪᶹစဎ꣡꣰꣢꣤ٗ؋لㇳㇾㇻㇱ㆐㆔,,㆟Ⱶヤマފ޼ޝަݿݞݠݷݐ',ݘ,ݪݙݵ𬝉𬜁𫝨𫞘くせぉて¼óû×ó£…𛅑הㄙくԗԀ5606神45,神796'𪤻𫞧ꓐ㄁ㄘɥɺꓵꓲ3''7034׉ⱦⱠˆ“𫝋ȍ,ꩲ軌꩷ꩶꩧꩫఞ۔فڱێظペサ神ナᴦᵑ47 9238їﻂ䐊䔉㠸﬎ffiﬣ,לּᴷᴦᵛᵽ,ᴨᵤ ᵸᵥᴗᵈꚏꚉꚟ⻆rtǟƴ𬎎

                    Why https connections are so slow when debugging (stepping over) in Java?