.. Installation and Getting Started =================================== ã¤ã³ã¹ãã¼ã«ãã¦å§ããã ======================== **Pythons**: Python 2.4-3.2, Jython, PyPy .. **Platforms**: Unix/Posix and Windows **Platforms** : Unix/Posix 㨠Windows .. **PyPI package name**: `pytest <http://pypi.python.org/pypi/pytest>`_ **PyPI ããã±ã¼ã¸å** : `pytest <http://pypi.python.org/pypi/pytest>`_ .. **documentation as PDF**: `download latest <http://pytest.org/latest/pytest.pdf>`_ **PDF ããã¥ã¡ã³ã** : `ææ°ããã¦ã³ãã¼ã <http://pytest.org/latest/pytest.pdf>`_ .. _`getstarted`: ã¤ã³ã¹ãã¼ã« ------------ .. Installation ---------------------------------------- .. Installation options:: ã¤ã³ã¹ãã¼ã«ãªãã·ã§ã³:: pip install -U pytest # or easy_install -U pytest .. To check your installation has installed the correct version:: ã¤ã³ã¹ãã¼ã«å¾ã«é©åãªãã¼ã¸ã§ã³ãã確èªããã«ã¯ã次ã®ããã«å®è¡ãã¾ã:: $ py.test --version This is py.test version 2.2.4, imported from /home/hpk/p/pytest/pytest.py setuptools registered plugins: pytest-xdist-1.8 at /home/hpk/p/pytest-xdist/xdist/plugin.pyc .. If you get an error checkout :ref:`installation issues`. ã¨ã©ã¼ãçºçããã :ref:`installation issues` ã確èªãã¦ãã ããã .. _`simpletest`: åãã¦ã®ãã¹ãå®è¡ ------------------ .. Our first test run ---------------------------------------------------------- .. Let's create a first test file with a simple test function:: ç°¡åãªãã¹ãé¢æ°ãå«ãæåã®ãã¹ããã¡ã¤ã«ãä½ãã¾ããã:: # test_sample.py ã®å 容 def func(x): return x + 1 def test_answer(): assert func(3) == 5 .. That's it. You can execute the test function now:: ãããªæãã§ããããããã¹ãé¢æ°ãå®è¡ãã¾ããã:: $ py.test =========================== test session starts ============================ platform linux2 -- Python 2.7.1 -- pytest-2.2.4 collecting ... collected 1 items test_sample.py F ================================= FAILURES ================================= _______________________________ test_answer ________________________________ def test_answer(): > assert func(3) == 5 E assert 4 == 5 E + where 4 = func(3) test_sample.py:5: AssertionError ========================= 1 failed in 0.01 seconds ========================= .. py.test found the ``test_answer`` function by following :ref:`standard test discovery rules <test discovery>`, basically detecting the ``test_`` prefixes. We got a failure report because our little ``func(3)`` call did not return ``5``. py.test 㯠:ref:`æ¨æºçãªãã¹ãæ¢ç´¢ã«ã¼ã« <test discovery>` ã«å¾ã ``test_answer`` é¢æ°ãæ¤åºãã¾ããåºæ¬çã«ã¯ ``test_`` ã®æ¥é è¾ããã¤ãã¡ã¤ã«ãé¢æ°ã§ããå ã»ã©ä½æãã ``func(3)`` å¼ã³åºãã ``5`` ãè¿ããªãã£ãã¨ãã失æã¬ãã¼ããåãåãã¾ããã .. You can simply use the ``assert`` statement for asserting test expectations. pytest's :ref:`assert introspection` will intelligently report intermediate values of the assert expression freeing you from the need to learn the many names of `JUnit legacy methods`_. .. note:: ãã¹ãã®æå¾ å¤ãã¢ãµã¼ãããã«ã¯åç´ã« ``assert`` æã使ãã¾ããpytest ã® :ref:`assert introspection` 㯠assert è©ä¾¡æã®ä¸éå¤ãè³¢ãã¬ãã¼ããã¾ããããã«ãããå¤ãã® `JUnit ã¬ã¬ã·ã¼ã¡ã½ãã`_ ã®ååãè¦ããå¿ è¦ããªããªãã¾ãã .. _`JUnit legacy methods`: http://docs.python.org/library/unittest.html#test-cases .. _`JUnit ã¬ã¬ã·ã¼ã¡ã½ãã`: http://docs.python.org/library/unittest.html#test-cases .. _`assert statement`: http://docs.python.org/reference/simple_stmts.html#the-assert-statement ç¹å®ã®ä¾å¤ãçºçãããã¨ãã¢ãµã¼ããã -------------------------------------- .. Asserting that a certain exception is raised -------------------------------------------------------------- .. If you want to assert that some code raises an exception you can use the ``raises`` helper:: ä¾å¤ãçºçãããã³ã¼ãããã¹ãããããªã ``raises`` ãã«ãã¼é¢æ°ã使ãã¾ã:: # test_sysexit.py ã®å 容 import pytest def f(): raise SystemExit(1) def test_mytest(): with pytest.raises(SystemExit): f() .. Running it with, this time in "quiet" reporting mode:: ãã®ã³ã¼ãã "quiet" ã¢ã¼ãã§å®è¡ãã¾ã:: $ py.test -q test_sysexit.py collecting ... collected 1 items . 1 passed in 0.00 seconds .. todo:: For further ways to assert exceptions see the `raises` .. Grouping multiple tests in a class -------------------------------------------------------------- 1ã¤ã®ã¯ã©ã¹ã§è¤æ°ã®ãã¹ããã°ã«ã¼ãåãã ----------------------------------------- .. Once you start to have more than a few tests it often makes sense to group tests logically, in classes and modules. Let's write a class containing two tests:: ãã¹ããæ¸ãå§ãã¦ä½åãä½æããããã¯ã©ã¹ãã¢ã¸ã¥ã¼ã«å ã«ãããã£ããã¹ããã°ã«ã¼ãåããã¨åããããããªãã¾ãã2ã¤ã®ãã¹ããå«ãã¯ã©ã¹ãä½æãã¾ããã:: # test_class.py ã®å 容 class TestClass: def test_one(self): x = "this" assert 'h' in x def test_two(self): x = "hello" assert hasattr(x, 'check') .. The two tests are found because of the standard :ref:`test discovery`. There is no need to subclass anything. We can simply run the module by passing its filename:: :ref:`æ¨æºçãªãã¹ãæ¢ç´¢ã«ã¼ã« <test discovery>` ã«ããã2ã¤ã®ãã¹ããæ¤åºããã¾ããããµãã¯ã©ã¹åããå¿ è¦ã¯ããã¾ãããåç´ã«ãã®ãã¡ã¤ã«åãä¸ãããã¨ã§ã対象ã®ã¢ã¸ã¥ã¼ã«ãå®è¡ã§ãã¾ã:: $ py.test -q test_class.py collecting ... collected 2 items .F ================================= FAILURES ================================= ____________________________ TestClass.test_two ____________________________ self = <test_class.TestClass instance at 0x1a956c8> def test_two(self): x = "hello" > assert hasattr(x, 'check') E assert hasattr('hello', 'check') test_class.py:8: AssertionError 1 failed, 1 passed in 0.01 seconds .. The first test passed, the second failed. Again we can easily see the intermediate values used in the assertion, helping us to understand the reason for the failure. æåã®ãã¹ãã¯æåãã2çªç®ã®ãã¹ãã¯å¤±æãã¾ãããã¾ãã失æããåå ãç解ããããããããã®ã¢ãµã¼ã·ã§ã³ã®ä¸éå¤ãã±ã£ã¨è¦ã¦åããã¾ãã .. Going functional: requesting a unique temporary directory -------------------------------------------------------------- æ©è½ãã¹ã: ä¸æãã£ã¬ã¯ããªã®è¦æ± ---------------------------------- .. For functional tests one often needs to create some files and pass them to application objects. py.test provides the versatile :ref:`funcarg mechanism` which allows to request arbitrary resources, for example a unique temporary directory:: æ©è½ãã¹ãã§ã¯ããã¡ã¤ã«ãä½æãã¦ãã¢ããªã±ã¼ã·ã§ã³ã®ãªãã¸ã§ã¯ãããã®ãã¡ã¤ã«ã«æ¸ãè¾¼ããããªãã¨ãããããã¾ããpy.test ã¯ã1ã¤ã ãåå¨ããä¸æãã£ã¬ã¯ããªã¨ãã£ããä»»æã®ãªã½ã¼ã¹è¦æ±ãæ±ãä¸è½ã® :ref:`funcarg mechanism` ãæä¾ãã¾ã:: # test_tmpdir.py ã®å 容 def test_needsfiles(tmpdir): print tmpdir assert 0 .. We list the name ``tmpdir`` in the test function signature and py.test will lookup and call a factory to create the resource before performing the test function call. Let's just run it:: ãã¹ãé¢æ°ã®ã·ã°ããã£ã« ``tmpdir`` ã¨ããååãå«ãã¾ããpy.test ã¯ãã®ååãè¦ã¤ãããã¹ãé¢æ°ãå¼ã³åºãããåã«ãªã½ã¼ã¹ãä½æãããã¡ã¯ããªã¼é¢æ°ãå¼ã³åºãã¾ããã§ã¯ãå®è¡ãã¦ã¿ã¾ããã:: $ py.test -q test_tmpdir.py collecting ... collected 1 items F ================================= FAILURES ================================= _____________________________ test_needsfiles ______________________________ tmpdir = local('/tmp/pytest-22/test_needsfiles0') def test_needsfiles(tmpdir): print tmpdir > assert 0 E assert 0 test_tmpdir.py:3: AssertionError ----------------------------- Captured stdout ------------------------------ /tmp/pytest-22/test_needsfiles0 1 failed in 0.01 seconds .. Before the test runs, a unique-per-test-invocation temporary directory was created. More info at :ref:`tmpdir handling`. ãã¹ããå®è¡ããæ¯ã«ããã®ãã¹ãé¢æ°ã®å®è¡åã«ä¸æãã£ã¬ã¯ããªãä½æããã¾ãããããã«è©³ç´°ã¯ :ref:`tmpdir handling` ãåç §ãã¦ãã ããã .. You can find out what kind of builtin :ref:`funcargs` exist by typing:: çµã¿è¾¼ã¿ã® :ref:`funcargs` ãææ¡ããã«ã¯ã次ã®ã³ãã³ããå®è¡ãã¾ã:: py.test --fixtures # çµã¿è¾¼ã¿/ã«ã¹ã¿ã ã®é¢æ°ã®å¼æ°ã表示ãã .. Where to go next ------------------------------------- 次ã«å¦ã¶ã㨠------------ .. Here are a few suggestions where to go next: 次ã®ããã¥ã¡ã³ããè¦ã¦ã¾ããã: .. * :ref:`cmdline` for command line invocation examples * :ref:`good practises <goodpractises>` for virtualenv, test layout, genscript support * :ref:`apiref` for documentation and examples on using py.test * :ref:`plugins` managing and writing plugins * :ref:`cmdline`: ã³ãã³ãã©ã¤ã³ã®å®è¡æ¹æ³ã®ãµã³ãã« * :ref:`åªãããã©ã¯ãã£ã¹ <goodpractises>`: virtualenvããã¹ãã¬ã¤ã¢ã¦ããgenscript ã®å¯¾å¿ * :ref:`apiref`: ããã¥ã¡ã³ã㨠py.test ã使ãä¸ã§ã®ãµã³ãã« * :ref:`plugins`: ãã©ã°ã¤ã³ã®ç®¡çã¨ä½æ .. _`installation issues`: ã¤ã³ã¹ãã¼ã«ã«é¢ããæ¢ç¥ã®åé¡ ------------------------------ .. Known Installation issues ------------------------------ .. easy_install or pip not found? ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ easy_install ã pip ãè¦ã¤ããã¾ãã ++++++++++++++++++++++++++++++++++++ .. _`install pip`: http://www.pip-installer.org/en/latest/index.html .. _`pip ãã¤ã³ã¹ãã¼ã«`: http://www.pip-installer.org/en/latest/index.html .. `Install pip`_ for a state of the art python package installer. æå 端㮠Python ããã±ã¼ã¸ã¤ã³ã¹ãã¼ã©ã¼ã§ãã `pip ãã¤ã³ã¹ãã¼ã«`_ ãã¦ãã ããã .. Or consult `distribute docs`_ to install the ``easy_install`` tool on your machine. ããã㯠``easy_install`` ãã¼ã«ãã¤ã³ã¹ãã¼ã«ããããã« `distribute docs`_ ãèªãã§ãã ããã .. You may also use the older `setuptools`_ project but it lacks bug fixes and does not work on Python3. æ§æ¥ã® `setuptools`_ ããã¸ã§ã¯ãã使ãã¾ãããããã¯ãã°ä¿®æ£ãè¡ããã¦ãªã Python 3 ã§ãåä½ãã¾ããã .. py.test not found on Windows despite installation? ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ã¤ã³ã¹ãã¼ã«ããã®ã« Windows ä¸ã§ py.test ãè¦ã¤ããã¾ãã ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ .. _`Python for Windows`: http://www.imladris.com/Scripts/PythonForWindows.html .. - **Windows**: If "easy_install" or "py.test" are not found you need to add the Python script path to your ``PATH``, see here: `Python for Windows`_. You may alternatively use an `ActivePython install`_ which does this for you automatically. - **Windows**: "easy_install" ã¾ã㯠"py.test" ãè¦ã¤ãããªããªãã ``PATH`` ã«ãããã® Python ã¹ã¯ãªããã追å ããå¿ è¦ãããã¾ãã `Python for Windows`_ ãåç §ãã¦ãã ãããå¥ã®æ¹æ³ã¨ãã¦ãèªåçã«ãã¹è¨å®ãè¡ã£ã¦ããã `ActivePython install`_ ã使ããã¨ãã§ãã¾ãã .. _`ActivePython install`: http://www.activestate.com/activepython/downloads .. _`Jython does not create command line launchers`: http://bugs.jython.org/issue1491 .. _`Jython ã¯ã³ãã³ãã©ã¤ã³ã©ã³ãã£ã¼ãä½ããªã`: http://bugs.jython.org/issue1491 .. - **Jython2.5.1 on Windows XP**: `Jython does not create command line launchers`_ so ``py.test`` will not work correctly. You may install py.test on CPython and type ``py.test --genscript=mytest`` and then use :ref:`examples` for more complex examples ``jython mytest`` to run py.test for your tests to run with Jython. - **Windows XP ä¸ã® Jython2.5.1**: `Jython ã¯ã³ãã³ãã©ã¤ã³ã©ã³ãã£ã¼ãä½ããªã`_ ã®ã§ ``py.test`` ã¯æ£å¸¸ã«åä½ãã¾ãããCPython ä¸ã« py.test ãã¤ã³ã¹ãã¼ã«ã㦠``py.test --genscript=mytest`` ãå®è¡ããã¨ãJython ã§è¡ããã¹ãã py.test ã§å®è¡ãã ``jython mytest`` ã使ãã¾ãã è¤éãªä¾ã¯ :ref:`examples` ãåç §ãã¦ãã ããã .. include:: links.inc