.. Monkeypatching/mocking modules and environments ================================================================ ã¢ã³ãã¼ãããï¼ã¢ãã¯ã®ã¢ã¸ã¥ã¼ã«ã¨ç°å¢ ======================================== .. currentmodule:: _pytest.monkeypatch .. Sometimes tests need to invoke functionality which depends on global settings or which invokes code which cannot be easily tested such as network access. The ``monkeypatch`` function argument helps you to safely set/delete an attribute, dictionary item or environment variable or to modify ``sys.path`` for importing. See the `monkeypatch blog post`_ for some introduction material and a discussion of its motivation. æã ãã°ãã¼ãã«è¨å®ã«ä¾åããæ©è½ã®ãã¹ããå®è¡ãããã¾ãã¯ãããã¯ã¼ã¯ã¢ã¯ã»ã¹ãä¼´ããããªç°¡åã«ãã¹ãã§ããªãã³ã¼ããå®è¡ããå¿ è¦ãããã¾ãã ``monkeypatch`` ã¨ããé¢æ°ã®å¼æ°ã使ããã¨ã§ãå±æ§ããã£ã¯ã·ã§ããªã®é ç®ãç°å¢å¤æ°ãã¤ã³ãã¼ãã®ããã® ``sys.path`` ã®å¤æ´ãå®å ¨ã«è¿½å ï¼åé¤ããã®ãæ¯æ´ãã¾ããå ¥éè¨äºã¨ãã®åæ©ä»ãã®è°è«ã¯ `monkeypatch ã®ããã°è¨äº`_ ãåç §ãã¦ãã ããã .. _`monkeypatch blog post`: http://tetamap.wordpress.com/2009/03/03/monkeypatching-in-unit-tests-done-right/ .. _`monkeypatch ã®ããã°è¨äº`: http://tetamap.wordpress.com/2009/03/03/monkeypatching-in-unit-tests-done-right/ .. Simple example: monkeypatching functions --------------------------------------------------- ç°¡åãªä¾: ã¢ã³ãã¼ãããæ©è½ ---------------------------- .. If you want to pretend that ``os.expanduser`` returns a certain directory, you can use the :py:meth:`monkeypatch.setattr` method to patch this function before calling into a function which uses it:: ``os.expanduser`` ãç¹å®ã®ãã£ã¬ã¯ããªãè¿ãããã«ããããå ´åãé¢æ°å 㧠``os.expanduser`` ãå¼ã°ããåã«ãã®é¢æ°ã¸ããããå½ã¦ãããã« :py:meth:`monkeypatch.setattr` ã¡ã½ããã使ãã¾ã:: # test_module.py ã®å 容 import os.path def getssh(): # çä¼¼ã¢ããªã±ã¼ã·ã§ã³ã³ã¼ã return os.path.join(os.path.expanduser("~admin"), '.ssh') def test_mytest(monkeypatch): def mockreturn(path): return '/abc' monkeypatch.setattr(os.path, 'expanduser', mockreturn) x = getssh() assert x == '/abc/.ssh' .. Here our test function monkeypatches ``os.path.expanduser`` and then calls into an function that calls it. After the test function finishes the ``os.path.expanduser`` modification will be undone. ãã®ãã¹ãé¢æ°ã¯ ``os.path.expanduser`` ã«ã¢ã³ãã¼ããããå½ã¦ãå¾ã§ãããé¢æ°å ãããã®é¢æ°ãå¼ã°ãã¾ãããã®ãã¹ãé¢æ°ãçµäºããå¾ã§ ``os.path.expanduser`` ã«å¯¾ããå¤æ´ã¯å ã«æ»ãã¾ãã .. Method reference of the monkeypatch function argument ----------------------------------------------------- é¢æ°ã®å¼æ° monkeypatch ã®ã¡ã½ãããªãã¡ã¬ã³ã¹ --------------------------------------------- .. autoclass:: monkeypatch :members: setattr, delattr, setitem, delitem, setenv, delenv, syspath_prepend, chdir, undo .. ``monkeypatch.setattr/delattr/delitem/delenv()`` all by default raise an Exception if the target does not exist. Pass ``raising=False`` if you want to skip this check. ``monkeypatch.setattr/delattr/delitem/delenv()`` ã®å ¨ã¦ã®é¢æ°ã«ããã¦ãå¤æ´å¯¾è±¡ãåå¨ããªãå ´åã«ããã©ã«ãã§ä¾å¤ãçºçããã¾ãããã®ãã§ãã¯å¦çãã¹ãããããããªã ``raising=False`` ã渡ãã¦ãã ããã