<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>xdist: pytest distributed testing plugin</title> <link rel="stylesheet" href="_static/sphinxdoc.css" type="text/css" /> <link rel="stylesheet" href="_static/pygments.css" type="text/css" /> <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: '', VERSION: '2.3.4.1', COLLAPSE_INDEX: false, FILE_SUFFIX: '.html', HAS_SOURCE: true }; </script> <script type="text/javascript" src="_static/jquery.js"></script> <script type="text/javascript" src="_static/underscore.js"></script> <script type="text/javascript" src="_static/doctools.js"></script> <link rel="top" title="None" href="index.html" /> <link rel="up" title="py.test reference documentation" href="apiref.html" /> <link rel="next" title="Temporary directories and files" href="tmpdir.html" /> <link rel="prev" title="Monkeypatching/mocking modules and environments" href="monkeypatch.html" /> </head> <body> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="tmpdir.html" title="Temporary directories and files" accesskey="N">next</a></li> <li class="right" > <a href="monkeypatch.html" title="Monkeypatching/mocking modules and environments" accesskey="P">previous</a> |</li> <li><a href="contents.html">pytest-2.3.4.1</a> »</li> <li><a href="apiref.html" accesskey="U">py.test reference documentation</a> »</li> <g:plusone></g:plusone> </ul> </div> <div class="sphinxsidebar"> <div class="sphinxsidebarwrapper"> <div id="searchbox" style="display: none"> <form class="search" action="search.html" method="get"> <input type="text" name="q" size="18" /> <input type="submit" value="Search" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> </div> <script type="text/javascript">$('#searchbox').show(0);</script> <h3>quicklinks</h3> <div style="text-align: left; font-size: 100%; vertical-align: middle;"> <table> <tr> <td> <a href="index.html">home</a> </td><td> <a href="contents.html">TOC/contents</a> </td></tr><tr><td> <a href="getting-started.html">install</a> </td><td> <a href="changelog.html">changelog</a> </td></tr><tr><td> <a href="example/index.html">examples</a> </td><td> <a href="customize.html">customize</a> </td></tr><tr><td> <a href="https://bitbucket.org/hpk42/pytest/issues?status=new&status=open">issues[bb]</a> </td><td> <a href="contact.html">contact</a> </td></tr></table> </div> <h3><a href="contents.html">Table Of Contents</a></h3> <ul> <li><a class="reference internal" href="#">xdist: pytest distributed testing plugin</a><ul> <li><a class="reference internal" href="#installation-of-xdist-plugin">Installation of xdist plugin</a></li> <li><a class="reference internal" href="#usage-examples">Usage examples</a><ul> <li><a class="reference internal" href="#speed-up-test-runs-by-sending-tests-to-multiple-cpus">Speed up test runs by sending tests to multiple CPUs</a></li> <li><a class="reference internal" href="#running-tests-in-a-python-subprocess">Running tests in a Python subprocess</a></li> <li><a class="reference internal" href="#running-tests-in-looponfailing-mode">Running tests in looponfailing mode</a></li> <li><a class="reference internal" href="#sending-tests-to-remote-ssh-accounts">Sending tests to remote SSH accounts</a></li> <li><a class="reference internal" href="#sending-tests-to-remote-socket-servers">Sending tests to remote Socket Servers</a></li> <li><a class="reference internal" href="#running-tests-on-many-platforms-at-once">Running tests on many platforms at once</a></li> <li><a class="reference internal" href="#specifying-test-exec-environments-in-an-ini-file">Specifying test exec environments in an ini file</a></li> <li><a class="reference internal" href="#specifying-rsync-dirs-in-an-ini-file">Specifying “rsync” dirs in an ini-file</a></li> </ul> </li> </ul> </li> </ul> <h4>Previous topic</h4> <p class="topless"><a href="monkeypatch.html" title="previous chapter">Monkeypatching/mocking modules and environments</a></p> <h4>Next topic</h4> <p class="topless"><a href="tmpdir.html" title="next chapter">Temporary directories and files</a></p> </div> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body"> <div class="section" id="xdist-pytest-distributed-testing-plugin"> <span id="xdist"></span><h1>xdist: pytest distributed testing plugin<a class="headerlink" href="#xdist-pytest-distributed-testing-plugin" title="Permalink to this headline">¶</a></h1> <p>The <a class="reference external" href="http://pypi.python.org/pypi/pytest-xdist">pytest-xdist</a> plugin extends py.test with some unique test execution modes:</p> <ul class="simple"> <li>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.</li> <li>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.</li> <li>Multi-Platform coverage: you can specify different Python interpreters or different platforms and run tests in parallel on all of them.</li> </ul> <p>Before running tests remotely, <tt class="docutils literal"><span class="pre">py.test</span></tt> 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.</p> <div class="section" id="installation-of-xdist-plugin"> <h2>Installation of xdist plugin<a class="headerlink" href="#installation-of-xdist-plugin" title="Permalink to this headline">¶</a></h2> <p>Install the plugin with:</p> <div class="highlight-python"><pre>easy_install pytest-xdist # or pip install pytest-xdist</pre> </div> <p>or use the package in develop/in-place mode with a checkout of the <a class="reference external" href="http://bitbucket.org/hpk42/pytest-xdist">pytest-xdist repository</a></p> <div class="highlight-python"><pre>python setup.py develop</pre> </div> </div> <div class="section" id="usage-examples"> <h2>Usage examples<a class="headerlink" href="#usage-examples" title="Permalink to this headline">¶</a></h2> <div class="section" id="speed-up-test-runs-by-sending-tests-to-multiple-cpus"> <span id="xdistcpu"></span><h3>Speed up test runs by sending tests to multiple CPUs<a class="headerlink" href="#speed-up-test-runs-by-sending-tests-to-multiple-cpus" title="Permalink to this headline">¶</a></h3> <p>To send tests to multiple CPUs, type:</p> <div class="highlight-python"><pre>py.test -n NUM</pre> </div> <p>Especially for longer running tests or tests requiring a lot of I/O this can lead to considerable speed ups.</p> </div> <div class="section" id="running-tests-in-a-python-subprocess"> <h3>Running tests in a Python subprocess<a class="headerlink" href="#running-tests-in-a-python-subprocess" title="Permalink to this headline">¶</a></h3> <p>To instantiate a Python-2.4 subprocess and send tests to it, you may type:</p> <div class="highlight-python"><pre>py.test -d --tx popen//python=python2.4</pre> </div> <p>This will start a subprocess which is run with the “python2.4” Python interpreter, found in your system binary lookup path.</p> <p>If you prefix the –tx option value like this:</p> <div class="highlight-python"><pre>py.test -d --tx 3*popen//python=python2.4</pre> </div> <p>then three subprocesses would be created and the tests will be distributed to three subprocesses and run simultanously.</p> </div> <div class="section" id="running-tests-in-looponfailing-mode"> <span id="looponfailing"></span><h3>Running tests in looponfailing mode<a class="headerlink" href="#running-tests-in-looponfailing-mode" title="Permalink to this headline">¶</a></h3> <p>For refactoring a project with a medium or large test suite you can use the looponfailing mode. Simply add the <tt class="docutils literal"><span class="pre">--f</span></tt> option:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">py</span><span class="o">.</span><span class="n">test</span> <span class="o">-</span><span class="n">f</span> </pre></div> </div> <p>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 <tt class="docutils literal"><span class="pre">looponfailingroots</span></tt> 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:</p> <div class="highlight-python"><pre># content of a pytest.ini, setup.cfg or tox.ini file [pytest] looponfailroots = mypkg testdir</pre> </div> <p>This would lead to only looking for file changes in the respective directories, specified relatively to the ini-file’s directory.</p> </div> <div class="section" id="sending-tests-to-remote-ssh-accounts"> <h3>Sending tests to remote SSH accounts<a class="headerlink" href="#sending-tests-to-remote-ssh-accounts" title="Permalink to this headline">¶</a></h3> <p>Suppose you have a package <tt class="docutils literal"><span class="pre">mypkg</span></tt> which contains some tests that you can successfully run locally. And you also have a ssh-reachable machine <tt class="docutils literal"><span class="pre">myhost</span></tt>. Then you can ad-hoc distribute your tests by typing:</p> <div class="highlight-python"><pre>py.test -d --tx ssh=myhostpopen --rsyncdir mypkg mypkg</pre> </div> <p>This will synchronize your <tt class="docutils literal"><span class="pre">mypkg</span></tt> package directory with a remote ssh account and then collect and run your tests at the remote side.</p> <p>You can specify multiple <tt class="docutils literal"><span class="pre">--rsyncdir</span></tt> directories to be sent to the remote side.</p> </div> <div class="section" id="sending-tests-to-remote-socket-servers"> <h3>Sending tests to remote Socket Servers<a class="headerlink" href="#sending-tests-to-remote-socket-servers" title="Permalink to this headline">¶</a></h3> <p>Download the single-module <a class="reference external" href="http://bitbucket.org/hpk42/execnet/raw/2af991418160/execnet/script/socketserver.py">socketserver.py</a> Python program and run it like this:</p> <div class="highlight-python"><pre>python socketserver.py</pre> </div> <p>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:</p> <div class="highlight-python"><pre>py.test -d --tx socket=192.168.1.102:8888 --rsyncdir mypkg mypkg</pre> </div> </div> <div class="section" id="running-tests-on-many-platforms-at-once"> <span id="atonce"></span><h3>Running tests on many platforms at once<a class="headerlink" href="#running-tests-on-many-platforms-at-once" title="Permalink to this headline">¶</a></h3> <p>The basic command to run tests on multiple platforms is:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">py</span><span class="o">.</span><span class="n">test</span> <span class="o">--</span><span class="n">dist</span><span class="o">=</span><span class="n">each</span> <span class="o">--</span><span class="n">tx</span><span class="o">=</span><span class="n">spec1</span> <span class="o">--</span><span class="n">tx</span><span class="o">=</span><span class="n">spec2</span> </pre></div> </div> <p>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 <a class="reference external" href="http://codespeak.net/execnet/basics.html#xspec">xspec syntax</a>.</p> </div> <div class="section" id="specifying-test-exec-environments-in-an-ini-file"> <h3>Specifying test exec environments in an ini file<a class="headerlink" href="#specifying-test-exec-environments-in-an-ini-file" title="Permalink to this headline">¶</a></h3> <p>pytest (since version 2.0) supports ini-style configuration. For example, you could make running with three subprocesses your default:</p> <div class="highlight-python"><div class="highlight"><pre><span class="p">[</span><span class="n">pytest</span><span class="p">]</span> <span class="n">addopts</span> <span class="o">=</span> <span class="o">-</span><span class="n">n3</span> </pre></div> </div> <p>You can also add default environments like this:</p> <div class="highlight-python"><pre>[pytest] addopts = --tx ssh=myhost//python=python2.5 --tx ssh=myhost//python=python2.6</pre> </div> <p>and then just type:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">py</span><span class="o">.</span><span class="n">test</span> <span class="o">--</span><span class="n">dist</span><span class="o">=</span><span class="n">each</span> </pre></div> </div> <p>to run tests in each of the environments.</p> </div> <div class="section" id="specifying-rsync-dirs-in-an-ini-file"> <h3>Specifying “rsync” dirs in an ini-file<a class="headerlink" href="#specifying-rsync-dirs-in-an-ini-file" title="Permalink to this headline">¶</a></h3> <p>In a <tt class="docutils literal"><span class="pre">tox.ini</span></tt> or <tt class="docutils literal"><span class="pre">setup.cfg</span></tt> file in your root project directory you may specify directories to include or to exclude in synchronisation:</p> <div class="highlight-python"><pre>[pytest] rsyncdirs = . mypkg helperpkg rsyncignore = .hg</pre> </div> <p>These directory specifications are relative to the directory where the configuration file was found.</p> </div> </div> </div> </div> </div> </div> <div class="clearer"></div> </div> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="tmpdir.html" title="Temporary directories and files" >next</a></li> <li class="right" > <a href="monkeypatch.html" title="Monkeypatching/mocking modules and environments" >previous</a> |</li> <li><a href="contents.html">pytest-2.3.4.1</a> »</li> <li><a href="apiref.html" >py.test reference documentation</a> »</li> <g:plusone></g:plusone> </ul> </div> <div class="footer"> © Copyright 2012, holger krekel. Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3. </div> <script type="text/javascript"> var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-7597274-13']); _gaq.push(['_trackPageview']); (function() { var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })(); </script> <script type="text/javascript" src="https://apis.google.com/js/plusone.js"></script> </body> </html>