.. highlightlang:: python .. _`goodpractises`: åªããã¤ã³ãã°ã¬ã¼ã·ã§ã³ãã©ã¯ãã£ã¹ ==================================== .. Good Integration Practises ================================================= .. Work with virtual environments ----------------------------------------------------------- ä»®æ³ç°å¢ã§ã®ä½æ¥ ---------------- .. We recommend to use virtualenv_ environments and use easy_install_ (or pip_) for installing your application dependencies as well as the ``pytest`` package itself. This way you will get a much more reproducible environment. A good tool to help you automate test runs against multiple dependency configurations or Python interpreters is `tox`_. virtualenv_ ç°å¢ãæ§ç¯ãã¦ã ``pytest`` ããã±ã¼ã¸ã¨ãã®ä»ã«ä¾åããã¢ããªã±ã¼ã·ã§ã³ãã¤ã³ã¹ãã¼ã«ããã®ã« easy_install_ (ã¾ã㯠pip_) ã使ããã¨ãã奨ããã¾ãã `tox`_ ã¨ãããè¤æ°ã®ä¾åè¨å®ã Python ã¤ã³ã¿ã¼ããªã¿ã¼ã«å¯¾ãã¦èªåçã«ãã¹ããå®è¡ãã便å©ãªãã¼ã«ãããã¾ãã .. _`virtualenv`: http://pypi.python.org/pypi/virtualenv .. _`buildout`: http://www.buildout.org/ .. _pip: http://pypi.python.org/pypi/pip .. Use tox and Continuous Integration servers ------------------------------------------------- tox ã¨ç¶ç¶çã¤ã³ãã°ã¬ã¼ã·ã§ã³ãµã¼ãã¼ã®å©ç¨ -------------------------------------------- .. If you frequently release code to the public you may want to look into `tox`_, the virtualenv test automation tool and its `pytest support <http://codespeak.net/tox/example/pytest.html>`_. The basic idea is to generate a JUnitXML file through the ``--junitxml=PATH`` option and have a continuous integration server like Jenkins_ pick it up and generate reports. ããé »ç¹ã«ã³ã¼ããä¸è¬åãã«ãªãªã¼ã¹ãããªããvirtualenv ã®ãã¹ãèªååã¨ãã® `pytest ãµãã¼ã <http://codespeak.net/tox/example/pytest.html>`_ ãè¡ã `tox`_ ã調ã¹ã¦ã¿ãããªãã§ããããåºæ¬çãªèãæ¹ã¯ã ``--junitxml=PATH`` ãªãã·ã§ã³ã«ãã JUnitXML ãã¡ã¤ã«ãçæãã¾ãããã㦠Jenkins_ ã®ãããªç¶ç¶çã¤ã³ãã°ã¬ã¼ã·ã§ã³ãµã¼ãã¼ããã®ãã¡ã¤ã«ãåå¾ãã¦ã¬ãã¼ããçæãã¾ãã .. _standalone: .. _`genscript method`: åç¬å®è¡ã§ãã py.test ã¹ã¯ãªããã®ä½æ --------------------------------------- .. Create a py.test standalone script ------------------------------------------- .. If you are a maintainer or application developer and want others to easily run tests you can generate a completely standalone "py.test" script:: ããªããã¡ã³ããã¼ã¾ãã¯ã¢ããªã±ã¼ã·ã§ã³éçºè ã§ãä»ã®äººã«ãç°¡åã«ãã¹ããå®è¡ãããããªããåç¬ã§å®è¡ã§ãã "py.test" ã¹ã¯ãªãããä½æã§ãã¾ã:: py.test --genscript=runtests.py .. generates a ``runtests.py`` script which is a fully functional basic ``py.test`` script, running unchanged under Python2 and Python3. You can tell people to download the script and then e.g. run it like this:: åºæ¬çã« ``py.test`` ã¹ã¯ãªããã¨å®å ¨ã«åæ©è½ãã㤠``runtests.py`` ã¹ã¯ãªãããçæãã¾ãããã®ã¹ã¯ãªãã㯠Python2 㨠Python3 ã«ããã¦ãä¿®æ£ããå®è¡ã§ãã¾ãããã®ã¹ã¯ãªããããã¦ã³ãã¼ããã¦ãä¾ãã°ã次ã®ããã«å®è¡ãã¦ãã ããã¨ä¼ããã°è¯ãã§ã:: python runtests.py .. _`Distribute for installation`: http://pypi.python.org/pypi/distribute#installation-instructions .. _`distribute installation`: http://pypi.python.org/pypi/distribute ``python setup.py test`` ã«ãã distutils ã¨ã®é£æº -------------------------------------------------- .. Integrating with distutils / ``python setup.py test`` -------------------------------------------------------- .. You can integrate test runs into your distutils or setuptools based project. Use the `genscript method`_ to generate a standalone py.test script:: ããã¸ã§ã¯ããã¼ã¹ã® distutils ã¾ã㯠setuptools ã§ãã¹ãå®è¡ãé£æºã§ãã¾ããåç¬ã§å®è¡ã§ãã py.test ã¹ã¯ãªãããçæããã«ã¯ :ref:`genscript ã¡ã½ãã <genscript method>` ã使ã£ã¦ãã ãã:: py.test --genscript=runtests.py .. and make this script part of your distribution and then add this to your ``setup.py`` file:: ãã®ã¹ã¯ãªãããé å¸ç©ã®ä¸é¨ã«ã㦠``setup.py`` ãã¡ã¤ã«ã«æ¬¡ã®ã³ã¼ãã追å ãã¾ã:: from distutils.core import setup, Command # setuptools ãããã¤ã³ãã¼ãã§ãã¾ã class PyTest(Command): user_options = [] def initialize_options(self): pass def finalize_options(self): pass def run(self): import sys,subprocess errno = subprocess.call([sys.executable, 'runtest.py']) raise SystemExit(errno) setup( #..., cmdclass = {'test': PyTest}, #..., ) .. If you now type:: ããã§æ¬¡ã®ããã«å®è¡ãã¾ã:: python setup.py test .. this will execute your tests using ``runtest.py``. As this is a standalone version of ``py.test`` no prior installation whatsoever is required for calling the test command. You can also pass additional arguments to the subprocess-calls such as your test directory or other options. ãã㯠``runtest.py`` ã使ã£ã¦ãã¹ããå®è¡ãã¾ãããã®ããã«ãåç¬ã§å®è¡ã§ãã ``py.test`` ã¹ã¯ãªããã¯ããã®ãã¹ãã³ãã³ããå¼ã³åºãããã«ä¾åããã±ã¼ã¸ãã¤ã³ã¹ãã¼ã«ããå¿ è¦ãããã¾ãããããã«ãã¹ããã£ã¬ã¯ããªããã®ä»ã®ãªãã·ã§ã³ãªã©ãsubprocess.call ã«è¿½å ã®å¼æ°ã¨ãã¦æ¸¡ãã¾ãã .. _`test discovery`: .. _`Python test discovery`: setuptools/distribute ã®ãã¹ãã³ãã³ãã¨ã®çµã¿åãã ---------------------------------------------------- .. Integration with setuptools/distribute test commands ---------------------------------------------------- .. Distribute/Setuptools support test requirements, which means its really easy to extend its test command to support running a pytest from test requirements:: setuptools/distribute ã¯ããã¹ãã«å¿ è¦ãªããã±ã¼ã¸è¦ä»¶ãã pytest ãå®è¡ãããã¹ãã³ãã³ããã¨ã¦ãç°¡åã«æ¡å¼µã§ãã tests_require ã«å¯¾å¿ãã¦ãã¾ã:: from setuptools.command.test import test as TestCommand class PyTest(TestCommand): def finalize_options(self): TestCommand.finalize_options(self) self.test_args = [] self.test_suite = True def run_tests(self): # å¤é¨ã§ egg ãèªã¿è¾¼ã¾ããããªããªãããã§ã¤ã³ãã¼ããã¦ãã ãã import pytest pytest.main(self.test_args) setup( #..., tests_require=['pytest'], cmdclass = {'test': pytest}, ) .. Now if you run:: ããã§æ¬¡ã®ããã«å®è¡ãã¾ã:: python setup.py test .. this will download py.test if needed and then run py.test as you would expect it to. å¿ è¦ã«å¿ã㦠py.test ããã¦ã³ãã¼ããã¦ãããæå¾ ããéãã« py.test ãå®è¡ãã¾ãã .. Conventions for Python test discovery ------------------------------------------------- Python ãã¹ãæ¢ç´¢ã®è¦ç´ ----------------------- .. ``py.test`` implements the following standard test discovery: ``py.test`` ã¯æ¬¡ã®ãã¹ãæ¢ç´¢æ¨æºãå®è£ ãã¾ã: .. * collection starts from the initial command line arguments which may be directories, filenames or test ids. * recurse into directories, unless they match :confval:`norecursedirs` * ``test_*.py`` or ``*_test.py`` files, imported by their `package name`_. * ``Test`` prefixed test classes (without an ``__init__`` method) * ``test_`` prefixed test functions or methods are test items * ã³ã¬ã¯ã·ã§ã³ã¯ããã£ã¬ã¯ããªããã¡ã¤ã«åããã¹ã ID ã¨ãã£ãæåã«ä¸ããã³ãã³ãã©ã¤ã³å¼æ°ããéå§ãã * :confval:`norecursedirs` ã«ä¸è´ããªãéãããã£ã¬ã¯ããªãå帰çã«æ¢ç´¢ãã * `package name`_ ã§ã¤ã³ãã¼ãããã ``test_*.py`` ã¾ã㯠``*_test.py`` ãã¡ã¤ã« * ``Test`` ã¨ããæ¥é è¾ããã¤ãã¹ãã¯ã©ã¹ (``__init__`` ã¡ã½ããããããªã) * ``test_`` ã¨ããæ¥é è¾ããã¤ãã¹ãé¢æ°ãã¡ã½ããããã¹ãé ç®ã«ãªã .. For examples of how to customize your test discovery :doc:`example/pythoncollection`. ãã¹ãæ¢ç´¢ãã«ã¹ã¿ãã¤ãºããæ¹æ³ã®ä¾ã¯ :doc:`example/pythoncollection` ãåç §ãã¦ãã ããã .. Within Python modules, py.test also discovers tests using the standard :ref:`unittest.TestCase <unittest.TestCase>` subclassing technique. Python ã¢ã¸ã¥ã¼ã«å ã§ã¯ãpy.test ãæ¨æºã©ã¤ãã©ãªã® :ref:`unittest.TestCase <unittest.TestCase>` ã®ãµãã¯ã©ã¹åã使ã£ã¦ãã¹ããæ¢ç´¢ãã¾ãã .. Choosing a test layout / import rules ------------------------------------------ ãã¹ãã¬ã¤ã¢ã¦ãé¸æã¨ã¤ã³ãã¼ãã«ã¼ã« -------------------------------------- .. py.test supports common test layouts: py.test ã¯ä¸è¬çãªãã¹ãã¬ã¤ã¢ã¦ãã«å¯¾å¿ãã¦ãã¾ã: .. * inlining test directories into your application package, useful if you want to keep (unit) tests and actually tested code close together:: * ã¢ããªã±ã¼ã·ã§ã³å ã« test ãã£ã¬ã¯ããªãé ç½®ãã¦ãã¾ãã(ã¦ããã) ãã¹ããä¿æãã¦å®éã«ãã¹ããããã³ã¼ããä¸ç·ã«ãã¦ããã®ã«å½¹ç«ã¡ã¾ã:: mypkg/ __init__.py appmodule.py ... test/ test_app.py ... .. * putting tests into an extra directory outside your actual application code, useful if you have many functional tests or want to keep tests separate from actual application code:: * ãã¹ããã¢ããªã±ã¼ã·ã§ã³ã³ã¼ãã®å¤é¨ã«é ç½®ãã¦ãã¾ããå¤ãã®æ©è½ãã¹ãããããã¾ãã¯å®éã®ã¢ããªã±ã¼ã·ã§ã³ã³ã¼ããããã¹ããåé¢ãã¦ä¿æãããã¨ãã«å½¹ç«ã¡ã¾ã:: mypkg/ __init__.py appmodule.py tests/ test_app.py ... .. In both cases you usually need to make sure that ``mypkg`` is importable, for example by using the setuptools ``python setup.py develop`` method. ã©ã¡ãã®å ´åããæ®éã« ``mypkg`` ãã¤ã³ãã¼ãã§ãããã¨ãä¿è¨¼ããå¿ è¦ãããã¾ããä¾ãã°ãsetuptools ã® ``python setup.py develop`` ã¡ã½ããã使ãã¾ãã .. You can run your tests by pointing to it:: 次ã®ããã«ãã¹ããå®è¡ã§ãã¾ã:: py.test tests/test_app.py # å¤é¨ã®ãã¹ããã£ã¬ã¯ã㪠py.test mypkg/test/test_app.py # å é¨ã®ãã¹ããã£ã¬ã¯ã㪠py.test mypkg # ãã¹ããã£ã¬ã¯ããªé ä¸ã«ããå ¨ã¦ã®ãã¹ããå®è¡ py.test # ã«ã¬ã³ããã£ãã¯ãªé ä¸ã«ããå ¨ã¦ã®ãã¹ããå®è¡ ... .. _`package name`: .. note:: py.test ããã¡ã¤ã«ã·ã¹ãã ãå帰çã«è¾¿ã£ã¦ "a/b/test_module.py" ãã¹ããã¡ã¤ã«ãæ¤åºããå ´åãã¤ã³ãã¼ãåã次ã®ããã«ãã¦æ±ºå®ãã¾ãã * ``basedir`` ãæ¤åºãã -- ãã㯠``__init__.py`` ãå«ã¾ãªãæåã® "upward" (ã«ã¼ãã«åãã) ãã£ã¬ã¯ããªã§ãã ``a`` 㨠``b`` ã®ãã£ã¬ã¯ããªä¸¡æ¹ã« ``__init__.py`` ãå«ãå ´åãbasedir 㯠``a`` ã®è¦ªãã£ã¬ã¯ããªã«ãªãã¾ã * ãã¹ãã¢ã¸ã¥ã¼ã«ãå®å ¨ä¿®é£¾ã¤ã³ãã¼ãåã§ã¤ã³ãã¼ãã§ããããã«ããããã« ``sys.path.insert(0, basedir)`` ãå®è¡ãã¾ã * ãã¹åºåãæå ``/`` ã "." ã«å¤æãããã¨ã§æ±ºã¾ã ``import a.b.test_module`` ãè¡ããã¤ã¾ãã¤ã³ãã¼ãåã«ç´æ¥ãã£ã¬ã¯ããªããã¡ã¤ã«åã対å¿ä»ããè¦ç´ã«å¾ããªãã¨ããã¾ãã ãã®å°ãé²åããã¤ã³ãã¼ããã¯ããã¯ã使ãçç±ã¯ã巨大ãªããã¸ã§ã¯ãã§ã¯è¤æ°ã®ãã¹ãã¢ã¸ã¥ã¼ã«ããäºãã«ã¤ã³ãã¼ãããå¯è½æ§ãããããã§ããããã¦ããã®ããã«å°åºãããã¤ã³ãã¼ãåã®æ¨æºåã¯ããã¹ãã¢ã¸ã¥ã¼ã«ã2åã¤ã³ãã¼ããã¦ãã¾ã£ã¦é©ããªãããã«ããã®ã«å½¹ç«ã¡ã¾ãã .. If py.test finds a "a/b/test_module.py" test file while recursing into the filesystem it determines the import name as follows: * find ``basedir`` -- this is the first "upward" (towards the root) directory not containing an ``__init__.py``. If both the ``a`` and ``b`` directories contain an ``__init__.py`` the basedir will be the parent dir of ``a``. * perform ``sys.path.insert(0, basedir)`` to make the test module importable under the fully qualified import name. * ``import a.b.test_module`` where the path is determined by converting path separators ``/`` into "." characters. This means you must follow the convention of having directory and file names map directly to the import names. The reason for this somewhat evolved importing technique is that in larger projects multiple test modules might import from each other and thus deriving a canonical import name helps to avoid surprises such as a test modules getting imported twice. .. include:: links.inc