.. _`tmpdir handling`: ä¸æãã£ã¬ã¯ããªã¨ãã¡ã¤ã« ========================== .. Temporary directories and files ================================================ .. The 'tmpdir' test function argument ----------------------------------- ãã¹ãé¢æ°ã®å¼æ° 'tmpdir' ------------------------- .. You can use the ``tmpdir`` function argument which will provide a temporary directory unique to the test invocation, created in the `base temporary directory`_. ``tmpdir`` ã¨ããé¢æ°ã®å¼æ°ãä¸æãªä¸æãã£ã¬ã¯ããªãæä¾ããã®ã«ä½¿ãã¾ãããã㯠:ref:`base temporary directory` ã«ä½æããã¾ãã .. ``tmpdir`` is a `py.path.local`_ object which offers ``os.path`` methods and more. Here is an example test usage:: ``tmpdir`` 㯠``os.path`` ã¡ã½ãããããã«ä»ã®ã¡ã½ãããæä¾ãã `py.path.local`_ ãªãã¸ã§ã¯ãã§ãã次ã«ãã¹ãã§ã®ä½¿ç¨ä¾ãç´¹ä»ãã¾ã:: # test_tmpdir.py ã®å 容 import os def test_create_file(tmpdir): p = tmpdir.mkdir("sub").join("hello.txt") p.write("content") assert p.read() == "content" assert len(tmpdir.listdir()) == 1 assert 0 .. Running this would result in a passed test except for the last ``assert 0`` line which we use to look at values:: ãã®ãã¹ããå®è¡ããã¨ãæçµè¡ã® ``assert 0`` ã失æã㦠``tmpdir`` ã®å¤ãè¦ãã¾ã:: $ py.test test_tmpdir.py =========================== test session starts ============================ platform linux2 -- Python 2.7.1 -- pytest-2.2.4 collecting ... collected 1 items test_tmpdir.py F ================================= FAILURES ================================= _____________________________ test_create_file _____________________________ tmpdir = local('/tmp/pytest-23/test_create_file0') def test_create_file(tmpdir): p = tmpdir.mkdir("sub").join("hello.txt") p.write("content") assert p.read() == "content" assert len(tmpdir.listdir()) == 1 > assert 0 E assert 0 test_tmpdir.py:7: AssertionError ========================= 1 failed in 0.02 seconds ========================= .. _`base temporary directory`: ããã©ã«ãã®ä¸æãã£ã¬ã¯ã㪠---------------------------- .. The default base temporary directory ----------------------------------------------- .. Temporary directories are by default created as sub-directories of the system temporary directory. The base name will be ``pytest-NUM`` where ``NUM`` will be incremented with each test run. Moreover, entries older than 3 temporary directories will be removed. ããã©ã«ãã§ã¯ããã¹ãåãã®ä¸æãã£ã¬ã¯ããªã¯ãã·ã¹ãã ã®ä¸æãã£ã¬ã¯ããªã®ãµããã£ã¬ã¯ããªã¨ãã¦ä½æããã¾ããåºæ¬ã¨ãªãåå㯠``pytest-NUM`` ã¨ãªã ``NUM`` ã¯ãã¹ããå®è¡ããã度ã«æ°åãå¢ãã¾ããã¾ãã3ä¸ä»£ããå¤ãä¸æãã£ã¬ã¯ããªã¯åé¤ããã¾ãã .. You can override the default temporary directory setting like this:: ããã©ã«ãã®ä¸æãã£ã¬ã¯ããªã®è¨å®ã¯æ¬¡ã®ããã«æ¸ãæãããã¾ã:: py.test --basetemp=mydir .. When distributing tests on the local machine, ``py.test`` takes care to configure a basetemp directory for the sub processes such that all temporary data lands below a single per-test run basetemp directory. ``py.test`` ã¯ããã¼ã«ã«ãã·ã³ä¸ã§åæ£ãã¹ããè¡ãã¨ããå ¨ã¦ã®ä¸æãã¼ã¿ã basetemp ãã£ã¬ã¯ããªã®é ä¸ã§å®è¡ããã¦ãã¹ãæ¯ã«ä¸æã«ãªãããããµãããã»ã¹ã«å¯¾ãã¦ã basetemp ãã£ã¬ã¯ããªãã¡ããã¨è¨å®ãã¾ãã .. _`py.path.local`: http://py.rtfd.org/path.html