.. _`captures`: æ¨æºåºå/æ¨æºã¨ã©ã¼ã®ãã£ãã㣠=============================== .. Capturing of the stdout/stderr output ========================================================= .. Default stdout/stderr/stdin capturing behaviour --------------------------------------------------------- ããã©ã«ãã® stdout/stderr/stdin ã®ãã£ããã£å¦ç ------------------------------------------------- .. During test execution any output sent to ``stdout`` and ``stderr`` is captured. If a test or a setup method fails its according captured output will usually be shown along with the failure traceback. ãã¹ãã®å®è¡ä¸ ``stdout`` 㨠``stderr`` ã¸éãããå ¨ã¦ã®åºåå 容ã¯ãã£ããã£ããã¾ãããã¹ãã¾ãã¯ã»ããã¢ããã¡ã½ããã失æããå ´åãããã§ãã£ããã£ãããåºåã¯ãé常ãã¨ã©ã¼ãã¬ã¼ã¹ããã¯ã¨ä¸ç·ã«è¡¨ç¤ºããã¾ãã .. In addition, ``stdin`` is set to a "null" object which will fail on attempts to read from it because it is rarely desired to wait for interactive input when running automated tests. å ã㦠``stdin`` ã¯ããã®èªã¿è¾¼ã¿ã«å¤±æãã "null" ãªãã¸ã§ã¯ããã»ããããã¾ãããã®çç±ã¯èªåãã¹ããå®è¡ããã¨ãã«å¯¾è©±å¼ã®å ¥åãå¾ ã¤ã®ãèæ ®ãããã¨ã¯ã»ã¨ãã©ãªãããã§ãã .. By default capturing is done by intercepting writes to low level file descriptors. This allows to capture output from simple print statements as well as output from a subprocess started by a test. ããã©ã«ãã®ãã£ããã£ã¯ãä½ã¬ãã«ã®ãã¡ã¤ã«ãã£ã¹ã¯ãªãã¿ã¸ã®æ¸ãè¾¼ã¿ã横åããã¾ããåç´ãª print æããã®åºåãããããã¹ããçæãããµãããã»ã¹ããã®åºåãåãããã«ãã£ããã£ã§ãã¾ãã .. Setting capturing methods or disabling capturing ------------------------------------------------- ã¡ã½ããããã£ããã£ãããã¾ãã¯ç¡å¹ã«ããè¨å® ---------------------------------------------- .. There are two ways in which ``py.test`` can perform capturing: ``py.test`` ã§ãã£ããã£ãå®è¡ããæ¹æ³ã2ã¤ããã¾ã: .. * file descriptor (FD) level capturing (default): All writes going to the operating system file descriptors 1 and 2 will be captured. * ãã¡ã¤ã«ãã£ã¹ã¯ãªãã¿ (FD) ã¬ãã«ã®ãã£ãã㣠(ããã©ã«ã): ãªãã¬ã¼ãã£ã³ã°ã·ã¹ãã ã®ãã¡ã¤ã«ãã£ã¹ã¯ãªãã¿1ã¨2ã¸ã®å ¨ã¦ã®æ¸ãè¾¼ã¿ããã£ããã£ãã .. * ``sys`` level capturing: Only writes to Python files ``sys.stdout`` and ``sys.stderr`` will be captured. No capturing of writes to filedescriptors is performed. * ``sys`` ã¬ãã«ã®ãã£ããã£: Python ãã¡ã¤ã« ``sys.stdout`` 㨠``sys.stderr`` ã¸ã®æ¸ãè¾¼ã¿ã®ã¿ãã£ããã£ããããã¡ã¤ã«ãã£ã¹ã¯ãªãã¿ã¸ã®æ¸ãè¾¼ã¿ã¯ãã£ããã£ããªã .. You can influence output capturing mechanisms from the command line:: .. _`disable capturing`: ã³ãã³ãã©ã¤ã³ããåºåå 容ã®ãã£ããã£è¨å®ãå¶å¾¡ã§ãã¾ã:: py.test -s # å ¨ã¦ã®ãã£ããã£ãç¡å¹ã«ãã py.test --capture=sys # sys.stdout/stderr ã in-mem ãã¡ã¤ã«ã«ç½®ãæãã py.test --capture=fd # ãã¡ã¤ã«ãã£ã¹ã¯ãªãã¿1ã¨2ãä¸æãã¡ã¤ã«ã«å·®ãåãã .. _printdebugging: ãããã°ã« print æã使ã ------------------------- .. Using print statements for debugging --------------------------------------------------- .. One primary benefit of the default capturing of stdout/stderr output is that you can use print statements for debugging:: ããã©ã«ã㧠stdout/stderr ã®åºåããã£ããã£ãã主ãªå©ç¹ã®1ã¤ã¨ãã¦ããããã°ã« print æã使ãã¾ã:: # test_module.py ã®å 容 def setup_function(function): print ("setting up %s" % function) def test_func1(): assert True def test_func2(): assert False .. and running this module will show you precisely the output of the failing function and hide the other one:: ãã®ã¢ã¸ã¥ã¼ã«ãå®è¡ããã¨ã失æãããã¹ãé¢æ°ã®åºåãé©åã«è¡¨ç¤ºãã¦ãæåãããã1ã¤ã®ãã¹ããé表示ã«ãã¾ã:: $ py.test =========================== test session starts ============================ platform linux2 -- Python 2.7.1 -- pytest-2.2.4 collecting ... collected 2 items test_module.py .F ================================= FAILURES ================================= ________________________________ test_func2 ________________________________ def test_func2(): > assert False E assert False test_module.py:9: AssertionError ----------------------------- Captured stdout ------------------------------ setting up <function test_func2 at 0x20160c8> ==================== 1 failed, 1 passed in 0.01 seconds ==================== .. Accessing captured output from a test function --------------------------------------------------- ãã¹ãé¢æ°ãããã£ããã£ãããåºåã¸ã®ã¢ã¯ã»ã¹ ---------------------------------------------- .. The :ref:`funcarg mechanism` allows test function a very easy way to access the captured output by simply using the names ``capsys`` or ``capfd`` in the test function signature. Here is an example test function that performs some output related checks:: :ref:`funcarg mechanism` ã«ããããã¹ãé¢æ°ã®ã·ã°ããã£ã« ``capsys`` ã¾ã㯠``capfd`` ã¨ããååã使ãã ãã§ãç°¡åã«ãã£ããã£ãããåºåã¸ã¢ã¯ã»ã¹ã§ãã¾ãã次ã«é¢é£ããå¤ã®ç¢ºèªãè¡ããã¹ãé¢æ°ã®ãµã³ãã«ãç´¹ä»ãã¾ã:: def test_myoutput(capsys): # ã¾ã㯠fd ã¬ãã«ã® "capfd" ã使ã print ("hello") sys.stderr.write("world\n") out, err = capsys.readouterr() assert out == "hello\n" assert err == "world\n" print "next" out, err = capsys.readouterr() assert out == "next\n" .. The ``readouterr()`` call snapshots the output so far - and capturing will be continued. After the test function finishes the original streams will be restored. Using ``capsys`` this way frees your test from having to care about setting/resetting output streams and also interacts well with py.test's own per-test capturing. ``readouterr()`` å¼ã³åºãã¯ããã®æç¹ã§ã®åºåå 容ã®ã¹ãããã·ã§ãããè¿ãããã®å¾ããã£ããã£ãç¶è¡ããã¾ãããã¹ãé¢æ°ãçµäºããå¾ãå ã®ã¹ããªã¼ã ã復å ããã¾ãã ``capsys`` ã使ããã¨ã§ããã¹ãå ã§åºåã¹ããªã¼ã ãã»ãã/ãªã»ãããããã¨ã«æ³¨æãæããªãã¦ãããªãã¾ããã¾ããpytest ãä¿æãããã¹ãåä½ã®ãã£ããã£ãæ±ãã¾ãã .. If you want to capture on ``fd`` level you can use the ``capfd`` function argument which offers the exact same interface. ``fd`` ã¬ãã«ã®ãã£ããã£ãè¡ãå ´åãå ¨ãåãã¤ã³ã¿ã¼ãã§ã¼ã¹ãæä¾ãã ``capfd`` ã¨ããé¢æ°ã®å¼æ°ã使ãã¾ãã .. include:: links.inc