.. _`xdist`: xdist: pytest ã®åæ£ãã¹ããã©ã°ã¤ã³ ==================================== .. xdist: pytest distributed testing plugin =============================================================== .. The `pytest-xdist`_ plugin extends py.test with some unique test execution modes: `pytest-xdist`_ ãã©ã°ã¤ã³ã¯ãç¬èªã®ãã¹ãã¢ã¼ã㧠py.test ãæ¡å¼µãã¾ã: .. * Looponfail: run your tests repeatedly in a subprocess. After each run, py.test waits until a file in your project changes and then re-runs the previously failing tests. This is repeated until all tests pass. At this point a full run is again performed. * Looponfail: ãµãããã»ã¹ã§ãã¹ããç¹°ãè¿ãå®è¡ãã¾ããåãã¹ããå®è¡ãã¦ãã py.test ã¯ããã¸ã§ã¯ãå ã®ãã¡ã¤ã«ãå¤æ´ãããã¾ã§å¾ ã¡ãäºåã«å¤±æãããã¹ãã®ã¿ãåå®è¡ãã¾ãããã®åä½ã¯å ¨ã¦ã®ãã¹ããæåããã¾ã§ç¹°ãè¿ããã¾ãããã®æ®µéã«ãªã£ã¦å度ãå ¨ã¦ã®ãã¹ããå®è¡ãã¾ãã .. * multiprocess Load-balancing: if you have multiple CPUs or hosts you can use them for a combined test run. This allows to speed up development or to use special resources of remote machines. * ãã«ãããã»ã¹è² è·åæ£: è¤æ° CPU ã¾ãã¯è¤æ°ãã¹ããããå ´åãããããçµåãã¹ãã®å®è¡ã«ä½¿ãã¾ããããã«ããéçºãé«éåãããããªã¢ã¼ããã·ã³ã®ç¹å¥ãªãªã½ã¼ã¹ã使ã£ãããã¾ãã .. * Multi-Platform coverage: you can specify different Python interpreters or different platforms and run tests in parallel on all of them. * ãã«ããã©ãããã©ã¼ã ã®ã«ãã¬ãã¸: ç°ãªã Python ã¤ã³ã¿ã¼ããªã¿ã¼ãã¾ãã¯ç°ãªããã©ãããã©ã¼ã ãæå®ãã¦ãå ¨ã¦ã®ç°å¢ã§ä¸¦è¡ãã¦ãã¹ããå®è¡ãã¾ãã .. Before running tests remotely, ``py.test`` efficiently "rsyncs" your program source code to the remote place. All test results are reported back and displayed to your local terminal. You may specify different Python versions and interpreters. ãªã¢ã¼ãç°å¢ã§ãã¹ããå®è¡ããåã«ã ``py.test`` ã¯ããã°ã©ã ã®ã½ã¼ã¹ã³ã¼ãããªã¢ã¼ããã·ã³ã¸å¹ççã« "rsync" ãã¾ããå ¨ã¦ã®ãã¹ãçµæã¯ããã¼ã«ã«ãã·ã³ã®ã¿ã¼ããã«ä¸ã§è¡¨ç¤ºããã¾ããç°ãªã Python ãã¼ã¸ã§ã³ãã¤ã³ã¿ã¼ããªã¿ã¼ãæå®ã§ãã¾ãã .. Installation of xdist plugin ------------------------------ xdist ãã©ã°ã¤ã³ã®ã¤ã³ã¹ãã¼ã« ------------------------------ .. Install the plugin with:: ãã©ã°ã¤ã³ãã¤ã³ã¹ãã¼ã«ãã¾ã:: easy_install pytest-xdist # ã¾ã㯠pip install pytest-xdist .. or use the package in develop/in-place mode with a checkout of the `pytest-xdist repository`_ :: ããã㯠`pytest-xdist ãªãã¸ããª`_ ãããã§ãã¯ã¢ã¦ãã㦠develop ã³ãã³ãã§ããã±ã¼ã¸ã使ãã¾ã:: python setup.py develop .. Usage examples --------------------- 使ç¨ä¾ ------ .. _`xdistcpu`: è¤æ°ã® CPU ã使ã£ããã¹ãå®è¡ã®é«éå +++++++++++++++++++++++++++++++++++++ .. Speed up test runs by sending tests to multiple CPUs +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ .. To send tests to multiple CPUs, type:: è¤æ°ã® CPU ã§ãã¹ããè¡ãã«ã¯ã次ã®ããã«å®è¡ãã¾ã:: py.test -n NUM .. Especially for longer running tests or tests requiring a lot of I/O this can lead to considerable speed ups. ç¹ã«é·æéå®è¡ããããã¹ããã¾ãã¯ããããã® I/O ãå¿ è¦ã¨ãããã¹ãã®é度åä¸ãè¦è¾¼ãã¾ãã .. Running tests in a Python subprocess +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Python ã®ãµãããã»ã¹ã§ãã¹ãå®è¡ +++++++++++++++++++++++++++++++++ .. To instantiate a Python-2.4 subprocess and send tests to it, you may type:: Python 2.4 ã® subprocess ãã¤ã³ã¹ã¿ã³ã¹åããã«ã¯ãããã«ãã¹ããéãã次ã®ããã«å®è¡ãã¾ã:: py.test -d --tx popen//python=python2.4 .. This will start a subprocess which is run with the "python2.4" Python interpreter, found in your system binary lookup path. ã·ã¹ãã ã®ãã¤ããªæ¤ç´¢ãã¹ã§è¦ã¤ãã "Python 2.4" ã¤ã³ã¿ã¼ããªã¿ã¼ã§å®è¡ãããµãããã»ã¹ãéå§ãã¾ãã .. If you prefix the --tx option value like this:: 次ã®ããã« --tx ãªãã·ã§ã³ã«å¤ãæå®ããå ´å:: py.test -d --tx 3*popen//python=python2.4 .. then three subprocesses would be created and the tests will be distributed to three subprocesses and run simultanously. 3ã¤ã®ãµãããã»ã¹ãä½æããããã®3ã¤ã®ãµãããã»ã¹ã«ãã¹ããåæ£ããã¦ä¸¦è¡ã§å®è¡ããã¾ãã .. _looponfailing: looponfailing ã¢ã¼ãã§ã®ãã¹ãå®è¡ ++++++++++++++++++++++++++++++++++ .. Running tests in looponfailing mode +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ .. For refactoring a project with a medium or large test suite you can use the looponfailing mode. Simply add the ``--f`` option:: ä¸è¦æ¨¡ã¾ãã¯å¤§è¦æ¨¡ã®ãã¹ãã¹ã¤ã¼ãããã¤ããã¸ã§ã¯ãããªãã¡ã¯ã¿ãªã³ã°ããããã«ãlooponfailing ã¢ã¼ãã便å©ã§ããåç´ã« ``--f`` ãªãã·ã§ã³ã追å ãã¾ã:: py.test -f .. and py.test will run your tests. Assuming you have failures it will then wait for file changes and re-run the failing test set. File changes are detected by looking at ``looponfailingroots`` root directories and all of their contents (recursively). If the default for this value does not work for you you can change it in your project by setting a configuration option:: py.test ã¯ãã¹ããå®è¡ãã¾ããããã§å®è¡ãããã¹ãã«å¤±æããã¨ä»®å®ãã¦ããã¡ã¤ã«ã®æ´æ°ãå¾ ã£ã¦ãããã®å¤±æãããã¹ããåå®è¡ãã¾ãããã¡ã¤ã«ã®æ´æ°ã¯ ``looponfailingroots`` ã¨ããã«ã¼ããã£ã¬ã¯ããªã¨ãã®å ¨ã¦ã®ã³ã³ãã³ãã (å帰çã«) 調ã¹ããã¨ã§æ¤åºããã¾ãããã®ããã©ã«ãå¤ãæå³ããããã«ä½ç¨ããªããªããè¨å®ãªãã·ã§ã³ã追å ãããã¨ã§èªåã®ããã¸ã§ã¯ãåãã®å¤ãå¤æ´ã§ãã¾ã:: # pytest.ini, setup.cfg ã¾ã㯠tox.ini ãã¡ã¤ã«ã®å 容 [pytest] looponfailroots = mypkg testdir .. This would lead to only looking for file changes in the respective directories, specified relatively to the ini-file's directory. ãã®è¨å®ã¯ ini ãã¡ã¤ã«ããããã£ã¬ã¯ããªããã¿ã¦ãç¸å¯¾ãã¹ã§æå®ãããã£ã¬ã¯ããªã®ãã¡ã¤ã«ã®æ´æ°ã®ã¿ã調ã¹ãããã«ãã¾ãã .. Sending tests to remote SSH accounts +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ãªã¢ã¼ãã® SSH ã¢ã«ã¦ã³ãã¸ãã¹ããéä¿¡ +++++++++++++++++++++++++++++++++++++++ .. Suppose you have a package ``mypkg`` which contains some tests that you can successfully run locally. And you also have a ssh-reachable machine ``myhost``. Then you can ad-hoc distribute your tests by typing:: ãã¼ã«ã«ã§æ£å¸¸ã«å®è¡ã§ãããã¹ããå«ã ``mypkg`` ã¨ãssh ã§ãã°ã¤ã³ã§ãããã·ã³ ``myhost`` ãããã¨ä»®å®ãã¾ãã次ã®ããã«å®è¡ãã¦ã¢ãããã¯ã«åæ£ãã¹ããå®è¡ã§ãã¾ã:: py.test -d --tx ssh=myhostpopen --rsyncdir mypkg mypkg .. This will synchronize your ``mypkg`` package directory with a remote ssh account and then collect and run your tests at the remote side. ããã¯ãªã¢ã¼ãã® ssh ã¢ã«ã¦ã³ã㧠``mypkg`` ããã±ã¼ã¸ã®ãã£ã¬ã¯ããªãåæããããªã¢ã¼ããã·ã³ä¸ã§ãã¹ããå®è¡ãã¾ãã .. You can specify multiple ``--rsyncdir`` directories to be sent to the remote side. ãªã¢ã¼ããã·ã³ã¸éãããã« è¤æ°ã® ``--rsyncdir`` ãã£ã¬ã¯ããªãæå®ã§ãã¾ãã .. XXX CHECK **NOTE:** For py.test to collect and send tests correctly you not only need to make sure all code and tests directories are rsynced, but that any test (sub) directory also has an ``__init__.py`` file because internally py.test references tests as a fully qualified python module path. **You will otherwise get strange errors** during setup of the remote side. .. Sending tests to remote Socket Servers +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ãªã¢ã¼ãã®ã½ã±ãããµã¼ãã¼ã¸ãã¹ããéä¿¡ ++++++++++++++++++++++++++++++++++++++++ .. Download the single-module `socketserver.py`_ Python program and run it like this:: `socketserver.py`_ ã¨ããåä¸ã¢ã¸ã¥ã¼ã«ã® Python ããã°ã©ã ããã¦ã³ãã¼ããã次ã®ããã«å®è¡ãã¦ãã ãã:: python socketserver.py .. It will tell you that it starts listening on the default port. You can now on your home machine specify this new socket host with something like this:: ãã®ããã°ã©ã ã¯ããã©ã«ãã®ãã¼ãã§å¾ æ©ç¶æ ãèµ·åãã¾ããèªå® ã®ãã·ã³ä¸ã§ãä½ãã®ããã±ã¼ã¸ã¨ä¸ç·ã«ãã®æ°ããªã½ã±ãããã¹ããæå®ã§ãã¾ã:: py.test -d --tx socket=192.168.1.102:8888 --rsyncdir mypkg mypkg .. _`atonce`: å¤ãã®ãã©ãããã©ã¼ã ä¸ã§åæã«ãã¹ããå®è¡ ++++++++++++++++++++++++++++++++++++++++++++ .. Running tests on many platforms at once +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ .. The basic command to run tests on multiple platforms is:: è¤æ°ã®ãã©ãããã©ã¼ã ã§ãã¹ããå®è¡ããåºæ¬çãªã³ãã³ãã§ã:: py.test --dist=each --tx=spec1 --tx=spec2 .. If you specify a windows host, an OSX host and a Linux environment this command will send each tests to all platforms - and report back failures from all platforms at once. The specifications strings use the `xspec syntax`_. Windows ãã¹ããOSX ãã¹ããLinux ç°å¢ãæå®ããå ´åããã®ã³ãã³ãã¯ããããã®ãã¹ããå ¨ã¦ã®ãã©ãããã©ã¼ã ã«éä¿¡ãã¾ããããã¦ãå ¨ã¦ã®ãã©ãããã©ã¼ã ããåæã«å¤±æã¬ãã¼ããåãåãã¾ãããã®è¨å®ã«ã¯ `xspec æ§æ`_ ã使ãã¾ãã .. _`xspec syntax`: http://codespeak.net/execnet/basics.html#xspec .. _`xspec æ§æ`: http://codespeak.net/execnet/basics.html#xspec .. _`socketserver.py`: http://bitbucket.org/hpk42/execnet/raw/2af991418160/execnet/script/socketserver.py .. _`execnet`: http://codespeak.net/execnet .. Specifying test exec environments in an ini file +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Ini ãã¡ã¤ã«ã§ãã¹ãå®è¡ç°å¢ã®æå® ++++++++++++++++++++++++++++++++++ .. pytest (since version 2.0) supports ini-style configuration. For example, you could make running with three subprocesses your default:: pytest (ãã¼ã¸ã§ã³ 2.0) 㯠ini ã¹ã¿ã¤ã«ã®è¨å®ãã¡ã¤ã«ã«å¯¾å¿ãã¦ãã¾ããä¾ãã°ãããã©ã«ãã§3ã¤ã®ãµãããã»ã¹ã使ã£ã¦ãã¹ããå®è¡ããã¾ã:: [pytest] addopts = -n3 .. You can also add default environments like this:: 次ã®ããã«ãã¦ããã©ã«ãç°å¢ã追å ã§ãã¾ã:: [pytest] addopts = --tx ssh=myhost//python=python2.5 --tx ssh=myhost//python=python2.6 .. and then just type:: ããã¦ã次ã®ããã«å ¥åãã¦å®è¡ããã¨:: py.test --dist=each .. to run tests in each of the environments. ããããã®ç°å¢ã§ãã¹ããå®è¡ããã¾ãã .. Specifying "rsync" dirs in an ini-file +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ini ãã¡ã¤ã«ã§ "rsync" ãã£ã¬ã¯ããªã®æå® +++++++++++++++++++++++++++++++++++++++++ .. In a ``tox.ini`` or ``setup.cfg`` file in your root project directory you may specify directories to include or to exclude in synchronisation:: ããã¸ã§ã¯ãã®ã«ã¼ããã£ã¬ã¯ããªã« ``tox.ini`` ã ``setup.cfg`` ãã¡ã¤ã«ãç½®ããåæã«å«ãããã¾ãã¯é¤å¤ãããã£ã¬ã¯ããªãæå®ãããã¨ãã§ãã¾ã:: [pytest] rsyncdirs = . mypkg helperpkg rsyncignore = .hg .. These directory specifications are relative to the directory where the configuration file was found. ããã§è¨å®ããããã£ã¬ã¯ããªã¯ãè¨å®ãã¡ã¤ã«ã®å ´æããç¸å¯¾ãã¹ã§ãã£ã¬ã¯ããªãæå®ãã¾ãã .. _`pytest-xdist`: http://pypi.python.org/pypi/pytest-xdist .. _`pytest-xdist repository`: http://bitbucket.org/hpk42/pytest-xdist .. _`pytest-xdist ãªãã¸ããª`: http://bitbucket.org/hpk42/pytest-xdist .. _`pytest`: http://pytest.org