1

I'm trying to use monkeypatch to mock one public function but it doesn't seem to work for me.

Here's my file structure

myproject
  |-mrss
     |- feed_burner.py <- has get_feed()
     |- version_controller.py <- has get_version()
  |-tests
     |-feed_burner_tests.py

Here's my test

from mrss.feed_burner import get_feed
from _pytest.monkeypatch import monkeypatch


 def test_first_feed(self):
        mp = monkeypatch()
        mp.setattr(mrss.version_controller, 'get_version', lambda env: 7)
        mrss_feed = get_feed(env=get_config())
        root = ET.fromstring(mrss_feed)

        self.assertEquals(21, len(programmes))

And this is my get_feed function

from mrss.version_controller import get_version

def get_feed(env=os.environ):
    uploader = Uploader(env=env)
    folder = env.get('S3_FOLDER')

    version = get_version(env)
    print version.isdigit()
    print 'version is {v}'.format(v=str(version))
    if not version or not version.isdigit():
        return ''

And all I got for get_version is empty string

False
version is

I tried with this but no luck

with patch('mrss.feed_burner.get_feed.mrss.version_controller.get_version', new=lambda env: 7):
    mrss_feed = get_feed(env=get_config())
    root = ET.fromstring(mrss_feed)

    programmes = root.findall('programme')

    self.assertEquals(21, len(programmes))
4
  • Is the test in the same file as the definition of get_feed? Commented Jul 2, 2015 at 15:25
  • No. They are not in the same file. I'll update my full test Commented Jul 2, 2015 at 15:30
  • You are probably patching the wrong function. If get_feed is in a file mycode.py, then you need to patch mycode.mrss.versioncontroller, not mrss.versioncontroller imported into your test module. The mock module describes what to patch, and I suspect you are experiencing the same problem (and the solution is the same, although I'm not sure enough to actually post an answer). Commented Jul 2, 2015 at 15:39
  • I tried with different location but nothing seems to work. I'll update my file structure and module. Commented Jul 2, 2015 at 15:45

1 Answer 1

2

In your test function,

from mrss.feed_burner import get_feed
from _pytest.monkeypatch import monkeypatch


def test_first_feed(self):
    mp = monkeypatch()
    mp.setattr(mrss.version_controller, 'get_version', lambda env: 7)
    mrss_feed = get_feed(env=get_config())
    root = ET.fromstring(mrss_feed)

    self.assertEquals(21, len(programmes))

the function object referenced by get_feed doesn't call mrss.version_controller.get_version, which is the reference used by your test module. It really calls mrss.feed_burner.mrss.version_controller.get_version, which is what you need to monkey patch. (get_feed has its own reference to its module scope, which is distinct from the reference in the test module.)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.